-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientFactory.js
158 lines (148 loc) · 5.35 KB
/
clientFactory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import cuid from 'cuid'
import * as ActionTypes from './actionTypes'
export default function clientFactory (config, fetch, FormData) {
const service = config.service
const cloudinary = {
url: `https://api.cloudinary.com/v1_1/${config.cloudinary.cloud}/image/upload`,
preset: config.cloudinary.preset
}
// TODO: change to hydra:collection pattern
const collections = {
people: { type: 'Person' },
projects: { type: 'Project' },
places: { type: 'Place' },
intents: { type: 'Intent' },
sessions: { type: 'Session' },
followings: { type: 'Following' },
favorings: { type: 'Favoring' },
matchings: { type: 'Matching' },
conversations: { type: 'Conversation' },
messages: { type: 'Message' },
reviews: { type: 'Review' },
collaborations: { type: 'Collaboration' },
subscriptions: { type: 'Subscription' },
notifications: { type: 'Notification' },
categories: { type: 'Category' }
}
function collectionUrl (actionType) {
let key = actionType.replace('FETCH_', '').replace('_REQUESTED', '').toLowerCase()
return service + '/' + key
}
function mintUrl (resource) {
let path = Object.keys(collections)
.find(key => collections[key].type === resource.type)
return `${service}/${path}/${cuid()}`
}
function channelUrl (personId) {
if (personId) return `${service}/people/${personId.split('/').pop()}/updates`
else return `${service}/updates`
}
async function get (url) {
let response = await fetch(url, {
credentials: 'include'
})
let json = await response.json()
if (response.ok) return json
else throw json
}
async function put (resource) {
if (!resource._id) resource._id = mintUrl(resource)
let response = await fetch(resource._id, {
method: 'PUT',
credentials: 'include',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(resource)
})
let json = await response.json()
if (response.ok) return json
else throw json
}
async function del (resource) {
let response = await fetch(resource._id, {
method: 'DELETE',
credentials: 'include'
})
if (!response.ok) {
let json = await response.json()
throw json
}
}
async function uploadImageAndSave (entity, image) {
if (!image) return put(entity)
let data = new FormData()
data.append('file', image)
data.append('upload_preset', cloudinary.preset)
let response = await fetch(cloudinary.url, {
method: 'POST',
body: data
})
if (!response.ok) { throw new Error() }
let cloudinaryData = await response.json()
let updated = Object.assign({}, entity, {
logo: cloudinaryData.secure_url
})
return put(updated)
}
return {
mintUrl,
channelUrl,
handleAction (action) {
switch (action.type) {
case ActionTypes.HELLO_REQUESTED:
return get(service)
case ActionTypes.FETCH_CATEGORIES_REQUESTED:
case ActionTypes.FETCH_PROJECTS_REQUESTED:
case ActionTypes.FETCH_PLACES_REQUESTED:
case ActionTypes.FETCH_PEOPLE_REQUESTED:
case ActionTypes.FETCH_INTENTS_REQUESTED:
case ActionTypes.FETCH_REVIEWS_REQUESTED:
case ActionTypes.FETCH_MATCHINGS_REQUESTED:
return get(collectionUrl(action.type))
case ActionTypes.FETCH_STATES_REQUESTED:
return get(service + '/places/states')
case ActionTypes.FETCH_MUNICIPALITIES_REQUESTED:
let stateCuid = action.stateId.split('/').pop()
return get(`${service}/places/states/${stateCuid}/municipalities`)
case ActionTypes.FETCH_PERSON_REQUESTED:
return get(action.id)
// TODO: change to hydra:collection pattern
case ActionTypes.FETCH_MY_CONVERSATIONS_REQUESTED:
return get(action.person._id + '/conversations')
case ActionTypes.FETCH_NOTIFICATIONS_REQUESTED:
return get(action.person._id + '/notifications')
case ActionTypes.SAVE_INTENT_REQUESTED:
return uploadImageAndSave(action.intent, action.image)
case ActionTypes.SAVE_PROJECT_REQUESTED:
return uploadImageAndSave(action.project, action.image)
case ActionTypes.SAVE_PERSON_REQUESTED:
return uploadImageAndSave(action.person, action.image)
case ActionTypes.FOLLOW_REQUESTED:
return put(action.following)
case ActionTypes.FAVOR_REQUESTED:
return put(action.favoring)
case ActionTypes.MATCH_REQUESTED:
return put(action.matching)
case ActionTypes.SAVE_SUBSCRIPTION_REQUESTED:
return put(action.subscription)
case ActionTypes.SAVE_CONVERSATION_REQUESTED:
return put(action.conversation)
case ActionTypes.ADD_MESSAGE_REQUESTED:
return put(action.message)
case ActionTypes.ADD_REVIEW_REQUESTED:
return put(action.review)
case ActionTypes.SAVE_NOTIFICATION_REQUESTED:
return put(action.notification)
case ActionTypes.SAVE_PLACE_REQUESTED:
return put(action.place)
case ActionTypes.BYE_REQUESTED:
return del(action.session)
case ActionTypes.UNFOLLOW_REQUESTED:
return del(action.following)
case ActionTypes.UNFAVOR_REQUESTED:
return del(action.favoring)
default:
throw new Error('unknown action: ' + action.type)
}
}
}
}