This repository has been archived by the owner on Nov 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.js
389 lines (342 loc) · 10.5 KB
/
actions.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import fuzzy from 'fuzzy';
export const TOGGLE_PROFILE_VIEW = 'TOGGLE_PROFILE_VIEW';
export const TOGGLE_EVENT = 'TOGGLE_EVENT';
export const REQUEST_LOGIN = 'REQUEST_LOGIN';
export const RECEIVE_LOGIN = 'RECEIVE_LOGIN';
export const FAIL_LOGIN = 'FAIL_LOGIN';
export const REQUEST_EVENTS = 'REQUEST_EVENTS';
export const RECEIVE_EVENTS = 'RECEIVE_EVENTS';
export const FAIL_TO_GET_EVENTS = 'FAIL_TO_GET_EVENTS';
export const REQUEST_VENUE = 'REQUEST_VENUE';
export const RECEIVE_VENUE = 'RECEIVE_VENUE';
export const RECEIVE_ALL_VENUES = 'RECEIVE_ALL_VENUES';
export const FAIL_TO_RECEIVE_VENUE = 'FAIL_TO_RECEIVE_VENUE';
export const REQUEST_SKILLS = 'REQUEST_SKILLS';
export const RECEIVE_SKILLS = 'RECEIVE_SKILLS';
export const FAIL_TO_GET_SKILLS = 'FAIL_TO_GET_SKILLS';
export const RSVPD_TO_EVENT = 'RSVPD_TO_EVENT';
export const UPDATE_EMAIL = 'UPDATE_EMAIL';
export const UPDATE_NNUMBER = 'UPDATE_NNUMBER';
export const SEND_PERSON = 'SEND_PERSON';
export const FILTER_SKILLS = 'FILTER_SKILLS';
export const SKILL_ROLLOVER = 'SKILL_ROLLOVER';
export const SELECT_SKILL_FIELD = 'SELECT_SKILL_FIELD';
export const DELETE_SKILL_SELECTION = 'DELETE_SKILL_SELECTION';
export const REQUEST_SCHOOLS = 'REQUEST_SCHOOLS';
export const FAIL_TO_GET_SCHOOLS = 'FAIL_TO_GET_SCHOOLS';
export const RECEIVE_SCHOOLS = 'RECEIVE_SCHOOLS';
const BASESTRING = 'https://api.tnyu.org';
export function fetchAll() {
return (dispatch, getState) => {
dispatch(fetchPerson())
.then( _ => dispatch(fetchEvents())
.then( _ => Promise.all(
getState().eventActions.events.map((event, i) => dispatch(fetchVenue(event.relationships.venue.data.id, i))))
.then(() => dispatch(fetchSkills())
)
))
}
}
function validate(str, reg) {
// '^[N][1-9]{8}'
const re = new RegExp(reg);
return re.test(str);
}
export function updateEmail(email) {
return {
type: UPDATE_EMAIL,
email
}
}
export function updateNNumber(nNumber) {
const valid = validate(nNumber, '^[N][0-9]{8}');
return {
type: UPDATE_NNUMBER,
nNumber,
valid
}
}
function sendPerson() {
return {
type: SEND_PERSON
}
}
function mapSkillsToPerson(skills) {
return {
data: skills.map( skill => {
return {
type: 'skill',
id: skill.id }
})
}
}
export function postPerson() {
return (dispatch, getState) => {
dispatch(sendPerson());
const nNumber = getState().loginActions.person.attributes.nNumber;
const nNumberValid = getState().loginActions.person.attributes.nNumberValid;
const skillsPersonHas = getState().skillActions['skillsPersonHas'].selected;
const wantsToLearn = getState().skillActions['wantsToLearn'].selected;
const wantsToHire = getState().skillActions['wantsToHire'].selected;
const contact = getState().loginActions.person.attributes.contact;
const person = Object.assign({}, getState().loginActions.person, {
type: 'people',
id: getState().loginActions.person.id,
attributes: {
contact: {}
},
relationships: {}
});
if (nNumber && nNumberValid) if (nNumber.length > 0) person.attributes.nNumber = nNumber;
if (contact) {
if (contact.email) person.attributes.contact.email = contact.email;
}
if (skillsPersonHas.length > 0) person.relationships.skills = mapSkillsToPerson(skillsPersonHas);
if (wantsToLearn.length > 0) person.relationships.wantsToLearn = mapSkillsToPerson(wantsToLearn);
if (wantsToHire.length > 0) person.relationships.wantsToHire = mapSkillsToPerson(wantsToHire);
const data = {
data: person,
};
$.ajax({
type: 'PATCH',
acccepts: 'application/vnd.api+json',
contentType: 'application/vnd.api+json',
url: `${BASESTRING}/${window.API_VERSION}/people/me`,
crossDomain: true,
dataType: 'json',
data: JSON.stringify(data),
})
.done( response => {
dispatch(receiveLogin(response.data));
dispatch(toggleProfile());
})
.fail( e => console.log(e.responseText));
}
}
function updateFilteredSkills(filtered, fieldType, word) {
return {
type: FILTER_SKILLS,
filtered,
fieldType
}
}
/*
Using `fuzzy` wordfilter to do fuzzy string matching on skill typeahead.
returns filtered names only
*/
export function filterSkills(word, fieldType) {
return (dispatch, getState) => {
const options = { extract: el => el.attributes.name };
const results = fuzzy.filter(word, getState().skillActions.skills, options).map( el => el.string);
dispatch(updateFilteredSkills(results, fieldType, word));
}
}
function selectTypeaheadField(fieldType) {
return {
type: SELECT_SKILL_FIELD,
fieldType
}
}
function moveTypeaheadPointer(move, fieldType) {
return {
type: SKILL_ROLLOVER,
move,
fieldType
}
}
export function updateActiveTypeaheadField(keyCode, fieldType) {
// up 38, down 40, left 37, right 39, enter 13
return dispatch => {
if (keyCode === 38) dispatch(moveTypeaheadPointer(-1, fieldType));
if (keyCode === 40) dispatch(moveTypeaheadPointer(1, fieldType));
if (keyCode === 13) dispatch(selectTypeaheadField(fieldType));
}
}
export function onHoverTypeahead(index, fieldType) {
return (dispatch, getState) => {
dispatch(moveTypeaheadPointer(index - getState().skillActions[fieldType].currentIdx, fieldType));
}
}
export function deleteTypeaheadSelection(index, fieldType) {
return {
type: DELETE_SKILL_SELECTION,
index,
fieldType
}
}
export function toggleProfile() {
return {
type: TOGGLE_PROFILE_VIEW
}
}
export function toggleEvent(index) {
return {
type: TOGGLE_EVENT,
index
}
}
export function requestLogin() {
return {
type: REQUEST_LOGIN
};
}
export function receiveLogin(json) {
return {
type: RECEIVE_LOGIN,
receivedAt: Date.now(),
json
};
}
export function failLogin() {
return {
type: FAIL_LOGIN
};
}
export function shouldFetchFb() {
window.location.href = `${BASESTRING}/${window.API_VERSION}/auth/facebook?success=${window.location}`;
}
export function fetchPerson() {
return dispatch => {
dispatch(requestLogin);
return $.get(`${BASESTRING}/${window.API_VERSION}/people/me`)
.done( response => dispatch(receiveLogin(response.data)))
.fail( _ => dispatch(failLogin()));
}
}
export function requestEvents() {
return {
type: REQUEST_EVENTS
};
}
export function receiveEvents(upcomingEvents, getState) {
let personId = getState().loginActions.person.id;
let json = checkIfRsvpd(upcomingEvents, personId);
return {
type: RECEIVE_EVENTS,
receivedAt: Date.now(),
json
}
}
function checkIfRsvpd(json, personId) {
return json.map((event, i) => {
if (event.relationships.rsvps.data.filter( person => person.id === personId).length > 0)
event.rsvp = true;
return event;
});
}
export function failToGetEvents() {
return {
type: FAIL_TO_GET_EVENTS
}
}
export function fetchEvents() {
return function(dispatch, getState) {
dispatch(requestEvents);
return $.get(`${BASESTRING}/${window.API_VERSION}/events/upcoming-publicly-live?sort=startDateTime?`)
.done( response => dispatch(receiveEvents(response.data, getState)))
.fail( _ => dispatch(failToGetEvents()));
}
}
export function requestVenue(index) {
return {
type: REQUEST_VENUE,
index
};
}
export function receiveVenue(index, json) {
return {
type: RECEIVE_VENUE,
index,
json
}
}
export function receivedAllVenues() {
return {
type: RECEIVE_ALL_VENUES
}
}
export function failToGetVenue(index) {
return {
type: FAIL_TO_RECEIVE_VENUE,
index
};
}
export function fetchVenue(id, index) {
return dispatch => {
dispatch(requestVenue);
return $.get('https://api.tnyu.org/' + window.API_VERSION + '/venues/' + id)
.done( response => dispatch(receiveVenue(index, response.data)))
.fail( _ => dispatch(failToGetVenue(index)));
}
}
function requestSkills() {
return {
type: REQUEST_SKILLS
};
}
function receiveSkills(allSkills, personSkills, wantsToLearn, wantsToHire) {
return {
type: RECEIVE_SKILLS,
allSkills,
personSkills,
wantsToLearn,
wantsToHire
};
}
function failToGetSkills() {
return {
type: FAIL_TO_GET_SKILLS
};
}
export function fetchSkills() {
return (dispatch, getState) => {
dispatch(requestSkills);
const personSkills = getState().loginActions.person.relationships.skills.data;
const wantsToLearn = getState().loginActions.person.relationships.wantsToLearn.data;
const wantsToHire = getState().loginActions.person.relationships.wantsToHire.data;
return $.get(`${BASESTRING}/${window.API_VERSION}/skills`)
.done( response => dispatch(
receiveSkills(response.data, personSkills, wantsToLearn, wantsToHire)
))
.fail( _ => dispatch(failToGetSkills()));
}
}
function requestSchools() {
return {
type: REQUEST_SCHOOLS
}
}
function failToGetSchools() {
return {
type: FAIL_TO_GET_SCHOOLS
}
}
function receiveSchools(schools) {
return {
type: RECEIVE_SCHOOLS,
schools
}
}
export function fetchSchools() {
return dispatch => {
dispatch(requestSchools);
return $.get(`${BASESTRING}/${window.API_VERSION}/school-attendances`)
.done( response => dispatch(receiveSchools(response.data)))
.fail( _ => dispatch(failToGetSchools()));
}
}
export function rsvpd(index) {
return {
type: RSVPD_TO_EVENT,
index
};
}
export function rsvpToEvents() {
return (dispatch, getState) => {
getState().eventActions.events.map((event, i) => {
if (event.selected) $.get(`${BASESTRING}/${window.API_VERSION}/events/${event.id}/rsvp`)
.done(() => dispatch(rsvpd(i)))
.fail(() => console.log('RSVP to ' + event.attributes.title + ' failed. Try again later.'));
})
}
}