Skip to content

Commit 0867923

Browse files
committed
fix:issue-986
1 parent 6a1758e commit 0867923

File tree

5 files changed

+154
-80
lines changed

5 files changed

+154
-80
lines changed

src/actions/challenges.js

Lines changed: 130 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import {
1313
fetchResourceRoles,
1414
fetchChallengeTimelines,
1515
fetchChallengeTracks,
16+
fetchGroupDetail,
1617
updateChallenge,
1718
patchChallenge,
1819
createChallenge as createChallengeAPI,
1920
createResource as createResourceAPI,
2021
deleteResource as deleteResourceAPI
2122
} from '../services/challenges'
2223
import {
24+
LOAD_GROUP_DETAIL,
2325
LOAD_CHALLENGE_DETAILS,
2426
LOAD_CHALLENGE_METADATA_SUCCESS,
2527
LOAD_CHALLENGES_FAILURE,
@@ -49,7 +51,12 @@ import { loadProject } from './projects'
4951
/**
5052
* Loads active challenges of project by page
5153
*/
52-
export function loadChallengesByPage (page, projectId, status, filterChallengeName = null) {
54+
export function loadChallengesByPage (
55+
page,
56+
projectId,
57+
status,
58+
filterChallengeName = null
59+
) {
5360
return (dispatch, getState) => {
5461
dispatch({
5562
type: LOAD_CHALLENGES_PENDING,
@@ -72,7 +79,8 @@ export function loadChallengesByPage (page, projectId, status, filterChallengeNa
7279
filters['projectId'] = projectId
7380
}
7481
if (!_.isEmpty(status)) {
75-
filters['status'] = status === '' ? undefined : _.startCase(status.toLowerCase())
82+
filters['status'] =
83+
status === '' ? undefined : _.startCase(status.toLowerCase())
7684
} else if (!(_.isInteger(projectId) && projectId > 0)) {
7785
filters['status'] = 'Active'
7886
}
@@ -81,16 +89,20 @@ export function loadChallengesByPage (page, projectId, status, filterChallengeNa
8189
page,
8290
perPage: PAGE_SIZE
8391
// 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+
})
8999
})
90-
}).catch(() => dispatch({
91-
type: LOAD_CHALLENGES_FAILURE,
92-
challenges: []
93-
}))
100+
.catch(() =>
101+
dispatch({
102+
type: LOAD_CHALLENGES_FAILURE,
103+
challenges: []
104+
})
105+
)
94106
}
95107
}
96108

@@ -115,46 +127,51 @@ export function loadChallenges (projectId, status, filterChallengeName = null) {
115127
filters['projectId'] = projectId
116128
}
117129
if (!_.isEmpty(status)) {
118-
filters['status'] = status === '' ? undefined : _.startCase(status.toLowerCase())
130+
filters['status'] =
131+
status === '' ? undefined : _.startCase(status.toLowerCase())
119132
} else if (!(_.isInteger(projectId) && projectId > 0)) {
120133
filters['status'] = 'Active'
121134
}
122135

123136
let fetchedChallenges = []
124137

125138
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 }
127143
dispatch({
128144
type: LOAD_CHALLENGES_PENDING
129145
})
130146
return fetchChallenges(filters, {
131147
page,
132148
perPage: PAGE_SIZE,
133149
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+
})
147167
}
148-
} else {
168+
})
169+
.catch(() =>
149170
dispatch({
150-
type: LOAD_CHALLENGES_SUCCESS,
151-
challenges: fetchedChallenges
171+
type: LOAD_CHALLENGES_FAILURE,
172+
challenges: []
152173
})
153-
}
154-
}).catch(() => dispatch({
155-
type: LOAD_CHALLENGES_FAILURE,
156-
challenges: []
157-
}))
174+
)
158175
}
159176
let page = 1
160177
getChallengesByPage(filters, page)
@@ -169,19 +186,39 @@ export function loadChallengeDetails (projectId, challengeId) {
169186
if (challengeId) {
170187
return dispatch({
171188
type: LOAD_CHALLENGE_DETAILS,
172-
payload: fetchChallenge(challengeId).then((challenge) => {
189+
payload: fetchChallenge(challengeId).then(challenge => {
173190
// TODO remove this unncessary check, or better utilize the the case when given project id
174191
// 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
176194
dispatch(loadProject(projectId))
177195
}
196+
dispatch(loadGroupDetails(challenge.groups))
178197
return challenge
179198
})
180199
})
181200
}
182201
}
183202
}
184203

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+
185222
/**
186223
* Update challenge details
187224
*
@@ -191,23 +228,25 @@ export function loadChallengeDetails (projectId, challengeId) {
191228
* @returns {Promise<{ type: string, challengeDetails: object }>} action object
192229
*/
193230
export function updateChallengeDetails (challengeId, challengeDetails) {
194-
return async (dispatch) => {
231+
return async dispatch => {
195232
dispatch({
196233
type: UPDATE_CHALLENGE_DETAILS_PENDING
197234
})
198235

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+
})
203242
})
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)
208249
})
209-
return Promise.reject(error)
210-
})
211250
}
212251
}
213252

@@ -219,21 +258,23 @@ export function updateChallengeDetails (challengeId, challengeDetails) {
219258
* @returns {Promise<{ type: string, challengeDetails: object }>} action object
220259
*/
221260
export function createChallenge (challengeDetails) {
222-
return async (dispatch) => {
261+
return async dispatch => {
223262
dispatch({
224263
type: CREATE_CHALLENGE_PENDING
225264
})
226265

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+
})
231272
})
232-
}).catch(() => {
233-
dispatch({
234-
type: CREATE_CHALLENGE_FAILURE
273+
.catch(() => {
274+
dispatch({
275+
type: CREATE_CHALLENGE_FAILURE
276+
})
235277
})
236-
})
237278
}
238279
}
239280

@@ -247,28 +288,33 @@ export function createChallenge (challengeDetails) {
247288
*
248289
* @returns {Promise<{ type: string, challengeDetails: object }>} action object
249290
*/
250-
export function partiallyUpdateChallengeDetails (challengeId, partialChallengeDetails) {
251-
return async (dispatch) => {
291+
export function partiallyUpdateChallengeDetails (
292+
challengeId,
293+
partialChallengeDetails
294+
) {
295+
return async dispatch => {
252296
dispatch({
253297
type: UPDATE_CHALLENGE_DETAILS_PENDING
254298
})
255299

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+
})
260306
})
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
264312
})
265-
throw error
266-
})
267313
}
268314
}
269315

270316
export function loadTimelineTemplates () {
271-
return async (dispatch) => {
317+
return async dispatch => {
272318
const timelineTemplates = await fetchTimelineTemplates()
273319
dispatch({
274320
type: LOAD_CHALLENGE_METADATA_SUCCESS,
@@ -279,7 +325,7 @@ export function loadTimelineTemplates () {
279325
}
280326

281327
export function loadChallengePhases () {
282-
return async (dispatch) => {
328+
return async dispatch => {
283329
const challengePhases = await fetchChallengePhases()
284330
dispatch({
285331
type: LOAD_CHALLENGE_METADATA_SUCCESS,
@@ -290,7 +336,7 @@ export function loadChallengePhases () {
290336
}
291337

292338
export function loadChallengeTypes () {
293-
return async (dispatch) => {
339+
return async dispatch => {
294340
const challengeTypes = await fetchChallengeTypes()
295341
dispatch({
296342
type: LOAD_CHALLENGE_METADATA_SUCCESS,
@@ -301,7 +347,7 @@ export function loadChallengeTypes () {
301347
}
302348

303349
export function loadChallengeTracks () {
304-
return async (dispatch) => {
350+
return async dispatch => {
305351
const challengeTracks = await fetchChallengeTracks()
306352
dispatch({
307353
type: LOAD_CHALLENGE_METADATA_SUCCESS,
@@ -312,7 +358,7 @@ export function loadChallengeTracks () {
312358
}
313359

314360
export function loadChallengeTimelines () {
315-
return async (dispatch) => {
361+
return async dispatch => {
316362
const challengeTimelines = await fetchChallengeTimelines()
317363
dispatch({
318364
type: LOAD_CHALLENGE_METADATA_SUCCESS,
@@ -323,7 +369,7 @@ export function loadChallengeTimelines () {
323369
}
324370

325371
export function loadChallengeTags () {
326-
return async (dispatch) => {
372+
return async dispatch => {
327373
const challengeTags = await fetchChallengeTags()
328374
dispatch({
329375
type: LOAD_CHALLENGE_METADATA_SUCCESS,
@@ -375,7 +421,7 @@ export function createAttachment (challengeId, file) {
375421
}
376422

377423
export function removeAttachment (attachmentId) {
378-
return (dispatch) => {
424+
return dispatch => {
379425
dispatch({
380426
type: REMOVE_ATTACHMENT,
381427
attachmentId
@@ -384,7 +430,7 @@ export function removeAttachment (attachmentId) {
384430
}
385431

386432
export function loadChallengeTerms () {
387-
return async (dispatch) => {
433+
return async dispatch => {
388434
const challengeTerms = await fetchChallengeTerms()
389435
dispatch({
390436
type: LOAD_CHALLENGE_METADATA_SUCCESS,
@@ -406,7 +452,7 @@ export function loadResources (challengeId) {
406452
}
407453

408454
export function loadResourceRoles () {
409-
return async (dispatch) => {
455+
return async dispatch => {
410456
const resourceRoles = await fetchResourceRoles()
411457
dispatch({
412458
type: LOAD_CHALLENGE_METADATA_SUCCESS,
@@ -463,8 +509,13 @@ export function createResource (challengeId, roleId, memberHandle) {
463509
* @param {String} newMember handle of the new resource
464510
* @param {String} oldMember handle of the existing resource
465511
*/
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 => {
468519
if (newMember === oldMember) {
469520
return
470521
}
@@ -474,7 +525,7 @@ export function replaceResourceInRole (challengeId, roleId, newMember, oldMember
474525
} catch (error) {
475526
const errorMessage = _.get(error, 'response.data.message')
476527
// 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) {
478529
return Promise.reject(new Error('Unable to delete resource'))
479530
}
480531
}

0 commit comments

Comments
 (0)