@@ -13,13 +13,15 @@ import {
13
13
fetchResourceRoles ,
14
14
fetchChallengeTimelines ,
15
15
fetchChallengeTracks ,
16
+ fetchGroupDetail ,
16
17
updateChallenge ,
17
18
patchChallenge ,
18
19
createChallenge as createChallengeAPI ,
19
20
createResource as createResourceAPI ,
20
21
deleteResource as deleteResourceAPI
21
22
} from '../services/challenges'
22
23
import {
24
+ LOAD_GROUP_DETAIL ,
23
25
LOAD_CHALLENGE_DETAILS ,
24
26
LOAD_CHALLENGE_METADATA_SUCCESS ,
25
27
LOAD_CHALLENGES_FAILURE ,
@@ -49,7 +51,12 @@ import { loadProject } from './projects'
49
51
/**
50
52
* Loads active challenges of project by page
51
53
*/
52
- export function loadChallengesByPage ( page , projectId , status , filterChallengeName = null ) {
54
+ export function loadChallengesByPage (
55
+ page ,
56
+ projectId ,
57
+ status ,
58
+ filterChallengeName = null
59
+ ) {
53
60
return ( dispatch , getState ) => {
54
61
dispatch ( {
55
62
type : LOAD_CHALLENGES_PENDING ,
@@ -72,7 +79,8 @@ export function loadChallengesByPage (page, projectId, status, filterChallengeNa
72
79
filters [ 'projectId' ] = projectId
73
80
}
74
81
if ( ! _ . isEmpty ( status ) ) {
75
- filters [ 'status' ] = status === '' ? undefined : _ . startCase ( status . toLowerCase ( ) )
82
+ filters [ 'status' ] =
83
+ status === '' ? undefined : _ . startCase ( status . toLowerCase ( ) )
76
84
} else if ( ! ( _ . isInteger ( projectId ) && projectId > 0 ) ) {
77
85
filters [ 'status' ] = 'Active'
78
86
}
@@ -81,16 +89,20 @@ export function loadChallengesByPage (page, projectId, status, filterChallengeNa
81
89
page,
82
90
perPage : PAGE_SIZE
83
91
// memberId: getState().auth.user ? getState().auth.user.userId : null
84
- } ) . then ( ( res ) => {
85
- dispatch ( {
86
- type : LOAD_CHALLENGES_SUCCESS ,
87
- challenges : res . data ,
88
- totalChallenges : parseInt ( _ . get ( res , 'headers.x-total' , '0' ) )
92
+ } )
93
+ . then ( res => {
94
+ dispatch ( {
95
+ type : LOAD_CHALLENGES_SUCCESS ,
96
+ challenges : res . data ,
97
+ totalChallenges : parseInt ( _ . get ( res , 'headers.x-total' , '0' ) )
98
+ } )
89
99
} )
90
- } ) . catch ( ( ) => dispatch ( {
91
- type : LOAD_CHALLENGES_FAILURE ,
92
- challenges : [ ]
93
- } ) )
100
+ . catch ( ( ) =>
101
+ dispatch ( {
102
+ type : LOAD_CHALLENGES_FAILURE ,
103
+ challenges : [ ]
104
+ } )
105
+ )
94
106
}
95
107
}
96
108
@@ -115,46 +127,51 @@ export function loadChallenges (projectId, status, filterChallengeName = null) {
115
127
filters [ 'projectId' ] = projectId
116
128
}
117
129
if ( ! _ . isEmpty ( status ) ) {
118
- filters [ 'status' ] = status === '' ? undefined : _ . startCase ( status . toLowerCase ( ) )
130
+ filters [ 'status' ] =
131
+ status === '' ? undefined : _ . startCase ( status . toLowerCase ( ) )
119
132
} else if ( ! ( _ . isInteger ( projectId ) && projectId > 0 ) ) {
120
133
filters [ 'status' ] = 'Active'
121
134
}
122
135
123
136
let fetchedChallenges = [ ]
124
137
125
138
function getChallengesByPage ( filters , page ) {
126
- if ( ! _ . isEmpty ( projectId ) && getState ( ) . challenges . projectId !== `${ projectId } ` ) return
139
+ if (
140
+ ! _ . isEmpty ( projectId ) &&
141
+ getState ( ) . challenges . projectId !== `${ projectId } `
142
+ ) { return }
127
143
dispatch ( {
128
144
type : LOAD_CHALLENGES_PENDING
129
145
} )
130
146
return fetchChallenges ( filters , {
131
147
page,
132
148
perPage : PAGE_SIZE ,
133
149
memberId : getState ( ) . auth . user ? getState ( ) . auth . user . userId : null
134
- } ) . then ( ( res ) => {
135
- if ( res . data . length > 0 ) {
136
- fetchedChallenges = [
137
- ...fetchedChallenges ,
138
- ...res . data
139
- ]
140
- dispatch ( {
141
- type : LOAD_CHALLENGES_SUCCESS ,
142
- challenges : fetchedChallenges
143
- } )
144
- // recurse until no further challenges are found
145
- if ( _ . get ( res , 'headers.x-total-pages' , 0 ) > page ) {
146
- return getChallengesByPage ( filters , page + 1 )
150
+ } )
151
+ . then ( res => {
152
+ if ( res . data . length > 0 ) {
153
+ fetchedChallenges = [ ...fetchedChallenges , ...res . data ]
154
+ dispatch ( {
155
+ type : LOAD_CHALLENGES_SUCCESS ,
156
+ challenges : fetchedChallenges
157
+ } )
158
+ // recurse until no further challenges are found
159
+ if ( _ . get ( res , 'headers.x-total-pages' , 0 ) > page ) {
160
+ return getChallengesByPage ( filters , page + 1 )
161
+ }
162
+ } else {
163
+ dispatch ( {
164
+ type : LOAD_CHALLENGES_SUCCESS ,
165
+ challenges : fetchedChallenges
166
+ } )
147
167
}
148
- } else {
168
+ } )
169
+ . catch ( ( ) =>
149
170
dispatch ( {
150
- type : LOAD_CHALLENGES_SUCCESS ,
151
- challenges : fetchedChallenges
171
+ type : LOAD_CHALLENGES_FAILURE ,
172
+ challenges : [ ]
152
173
} )
153
- }
154
- } ) . catch ( ( ) => dispatch ( {
155
- type : LOAD_CHALLENGES_FAILURE ,
156
- challenges : [ ]
157
- } ) )
174
+ )
158
175
}
159
176
let page = 1
160
177
getChallengesByPage ( filters , page )
@@ -169,19 +186,39 @@ export function loadChallengeDetails (projectId, challengeId) {
169
186
if ( challengeId ) {
170
187
return dispatch ( {
171
188
type : LOAD_CHALLENGE_DETAILS ,
172
- payload : fetchChallenge ( challengeId ) . then ( ( challenge ) => {
189
+ payload : fetchChallenge ( challengeId ) . then ( challenge => {
173
190
// TODO remove this unncessary check, or better utilize the the case when given project id
174
191
// does not match with challenge's project id
175
- if ( challenge . projectId == projectId ) { // eslint-disable-line
192
+ if ( challenge . projectId === projectId ) {
193
+ // eslint-disable-line
176
194
dispatch ( loadProject ( projectId ) )
177
195
}
196
+ dispatch ( loadGroupDetails ( challenge . groups ) )
178
197
return challenge
179
198
} )
180
199
} )
181
200
}
182
201
}
183
202
}
184
203
204
+ /**
205
+ * Loads group details
206
+ */
207
+ export function loadGroupDetails ( groupIds ) {
208
+ return ( dispatch , getState ) => {
209
+ const promiseAll = [ ]
210
+ groupIds . forEach ( id => {
211
+ promiseAll . push ( fetchGroupDetail ( id ) )
212
+ } )
213
+ return dispatch ( {
214
+ type : LOAD_GROUP_DETAIL ,
215
+ payload : Promise . all ( promiseAll ) . then ( groups => {
216
+ return groups
217
+ } )
218
+ } )
219
+ }
220
+ }
221
+
185
222
/**
186
223
* Update challenge details
187
224
*
@@ -191,23 +228,25 @@ export function loadChallengeDetails (projectId, challengeId) {
191
228
* @returns {Promise<{ type: string, challengeDetails: object }> } action object
192
229
*/
193
230
export function updateChallengeDetails ( challengeId , challengeDetails ) {
194
- return async ( dispatch ) => {
231
+ return async dispatch => {
195
232
dispatch ( {
196
233
type : UPDATE_CHALLENGE_DETAILS_PENDING
197
234
} )
198
235
199
- return updateChallenge ( challengeId , challengeDetails ) . then ( ( challenge ) => {
200
- return dispatch ( {
201
- type : UPDATE_CHALLENGE_DETAILS_SUCCESS ,
202
- challengeDetails : challenge
236
+ return updateChallenge ( challengeId , challengeDetails )
237
+ . then ( challenge => {
238
+ return dispatch ( {
239
+ type : UPDATE_CHALLENGE_DETAILS_SUCCESS ,
240
+ challengeDetails : challenge
241
+ } )
203
242
} )
204
- } ) . catch ( ( error ) => {
205
- dispatch ( {
206
- type : UPDATE_CHALLENGE_DETAILS_FAILURE ,
207
- error
243
+ . catch ( error => {
244
+ dispatch ( {
245
+ type : UPDATE_CHALLENGE_DETAILS_FAILURE ,
246
+ error
247
+ } )
248
+ return Promise . reject ( error )
208
249
} )
209
- return Promise . reject ( error )
210
- } )
211
250
}
212
251
}
213
252
@@ -219,21 +258,23 @@ export function updateChallengeDetails (challengeId, challengeDetails) {
219
258
* @returns {Promise<{ type: string, challengeDetails: object }> } action object
220
259
*/
221
260
export function createChallenge ( challengeDetails ) {
222
- return async ( dispatch ) => {
261
+ return async dispatch => {
223
262
dispatch ( {
224
263
type : CREATE_CHALLENGE_PENDING
225
264
} )
226
265
227
- return createChallengeAPI ( challengeDetails ) . then ( ( challenge ) => {
228
- return dispatch ( {
229
- type : CREATE_CHALLENGE_SUCCESS ,
230
- challengeDetails : challenge
266
+ return createChallengeAPI ( challengeDetails )
267
+ . then ( challenge => {
268
+ return dispatch ( {
269
+ type : CREATE_CHALLENGE_SUCCESS ,
270
+ challengeDetails : challenge
271
+ } )
231
272
} )
232
- } ) . catch ( ( ) => {
233
- dispatch ( {
234
- type : CREATE_CHALLENGE_FAILURE
273
+ . catch ( ( ) => {
274
+ dispatch ( {
275
+ type : CREATE_CHALLENGE_FAILURE
276
+ } )
235
277
} )
236
- } )
237
278
}
238
279
}
239
280
@@ -247,28 +288,33 @@ export function createChallenge (challengeDetails) {
247
288
*
248
289
* @returns {Promise<{ type: string, challengeDetails: object }> } action object
249
290
*/
250
- export function partiallyUpdateChallengeDetails ( challengeId , partialChallengeDetails ) {
251
- return async ( dispatch ) => {
291
+ export function partiallyUpdateChallengeDetails (
292
+ challengeId ,
293
+ partialChallengeDetails
294
+ ) {
295
+ return async dispatch => {
252
296
dispatch ( {
253
297
type : UPDATE_CHALLENGE_DETAILS_PENDING
254
298
} )
255
299
256
- return patchChallenge ( challengeId , partialChallengeDetails ) . then ( ( challenge ) => {
257
- return dispatch ( {
258
- type : UPDATE_CHALLENGE_DETAILS_SUCCESS ,
259
- challengeDetails : challenge
300
+ return patchChallenge ( challengeId , partialChallengeDetails )
301
+ . then ( challenge => {
302
+ return dispatch ( {
303
+ type : UPDATE_CHALLENGE_DETAILS_SUCCESS ,
304
+ challengeDetails : challenge
305
+ } )
260
306
} )
261
- } ) . catch ( ( error ) => {
262
- dispatch ( {
263
- type : UPDATE_CHALLENGE_DETAILS_FAILURE
307
+ . catch ( error => {
308
+ dispatch ( {
309
+ type : UPDATE_CHALLENGE_DETAILS_FAILURE
310
+ } )
311
+ throw error
264
312
} )
265
- throw error
266
- } )
267
313
}
268
314
}
269
315
270
316
export function loadTimelineTemplates ( ) {
271
- return async ( dispatch ) => {
317
+ return async dispatch => {
272
318
const timelineTemplates = await fetchTimelineTemplates ( )
273
319
dispatch ( {
274
320
type : LOAD_CHALLENGE_METADATA_SUCCESS ,
@@ -279,7 +325,7 @@ export function loadTimelineTemplates () {
279
325
}
280
326
281
327
export function loadChallengePhases ( ) {
282
- return async ( dispatch ) => {
328
+ return async dispatch => {
283
329
const challengePhases = await fetchChallengePhases ( )
284
330
dispatch ( {
285
331
type : LOAD_CHALLENGE_METADATA_SUCCESS ,
@@ -290,7 +336,7 @@ export function loadChallengePhases () {
290
336
}
291
337
292
338
export function loadChallengeTypes ( ) {
293
- return async ( dispatch ) => {
339
+ return async dispatch => {
294
340
const challengeTypes = await fetchChallengeTypes ( )
295
341
dispatch ( {
296
342
type : LOAD_CHALLENGE_METADATA_SUCCESS ,
@@ -301,7 +347,7 @@ export function loadChallengeTypes () {
301
347
}
302
348
303
349
export function loadChallengeTracks ( ) {
304
- return async ( dispatch ) => {
350
+ return async dispatch => {
305
351
const challengeTracks = await fetchChallengeTracks ( )
306
352
dispatch ( {
307
353
type : LOAD_CHALLENGE_METADATA_SUCCESS ,
@@ -312,7 +358,7 @@ export function loadChallengeTracks () {
312
358
}
313
359
314
360
export function loadChallengeTimelines ( ) {
315
- return async ( dispatch ) => {
361
+ return async dispatch => {
316
362
const challengeTimelines = await fetchChallengeTimelines ( )
317
363
dispatch ( {
318
364
type : LOAD_CHALLENGE_METADATA_SUCCESS ,
@@ -323,7 +369,7 @@ export function loadChallengeTimelines () {
323
369
}
324
370
325
371
export function loadChallengeTags ( ) {
326
- return async ( dispatch ) => {
372
+ return async dispatch => {
327
373
const challengeTags = await fetchChallengeTags ( )
328
374
dispatch ( {
329
375
type : LOAD_CHALLENGE_METADATA_SUCCESS ,
@@ -375,7 +421,7 @@ export function createAttachment (challengeId, file) {
375
421
}
376
422
377
423
export function removeAttachment ( attachmentId ) {
378
- return ( dispatch ) => {
424
+ return dispatch => {
379
425
dispatch ( {
380
426
type : REMOVE_ATTACHMENT ,
381
427
attachmentId
@@ -384,7 +430,7 @@ export function removeAttachment (attachmentId) {
384
430
}
385
431
386
432
export function loadChallengeTerms ( ) {
387
- return async ( dispatch ) => {
433
+ return async dispatch => {
388
434
const challengeTerms = await fetchChallengeTerms ( )
389
435
dispatch ( {
390
436
type : LOAD_CHALLENGE_METADATA_SUCCESS ,
@@ -406,7 +452,7 @@ export function loadResources (challengeId) {
406
452
}
407
453
408
454
export function loadResourceRoles ( ) {
409
- return async ( dispatch ) => {
455
+ return async dispatch => {
410
456
const resourceRoles = await fetchResourceRoles ( )
411
457
dispatch ( {
412
458
type : LOAD_CHALLENGE_METADATA_SUCCESS ,
@@ -463,8 +509,13 @@ export function createResource (challengeId, roleId, memberHandle) {
463
509
* @param {String } newMember handle of the new resource
464
510
* @param {String } oldMember handle of the existing resource
465
511
*/
466
- export function replaceResourceInRole ( challengeId , roleId , newMember , oldMember ) {
467
- return async ( dispatch ) => {
512
+ export function replaceResourceInRole (
513
+ challengeId ,
514
+ roleId ,
515
+ newMember ,
516
+ oldMember
517
+ ) {
518
+ return async dispatch => {
468
519
if ( newMember === oldMember ) {
469
520
return
470
521
}
@@ -474,7 +525,7 @@ export function replaceResourceInRole (challengeId, roleId, newMember, oldMember
474
525
} catch ( error ) {
475
526
const errorMessage = _ . get ( error , 'response.data.message' )
476
527
// ignore error where the resource does not exist already
477
- if ( errorMessage . indexOf ( ' doesn\ 't have resource with roleId' ) === - 1 ) {
528
+ if ( errorMessage . indexOf ( " doesn't have resource with roleId" ) === - 1 ) {
478
529
return Promise . reject ( new Error ( 'Unable to delete resource' ) )
479
530
}
480
531
}
0 commit comments