@@ -49,6 +49,7 @@ import {
4949 LOAD_CHALLENGE_RESOURCES
5050} from '../config/constants'
5151import { loadProject } from './projects'
52+ import { removeChallengeFromPhaseProduct , saveChallengeAsPhaseProduct } from '../services/projects'
5253
5354/**
5455 * Member challenges related redux actions
@@ -203,16 +204,34 @@ export function loadGroupDetails (groupIds) {
203204 *
204205 * @param {String } challengeId challenge id
205206 * @param {Object } challengeDetails challenge data
206- *
207+ * @param { String } projectId project id
207208 * @returns {Promise<{ type: string, challengeDetails: object }> } action object
208209 */
209- export function updateChallengeDetails ( challengeId , challengeDetails ) {
210+ export function updateChallengeDetails ( challengeId , challengeDetails , projectId ) {
210211 return async ( dispatch ) => {
211212 dispatch ( {
212213 type : UPDATE_CHALLENGE_DETAILS_PENDING
213214 } )
214215
215- return updateChallenge ( challengeId , challengeDetails ) . then ( ( challenge ) => {
216+ const milestoneId = challengeDetails . milestoneId
217+ // Check if milestone is deleted or updated
218+ const hasMilestone = _ . has ( challengeDetails , 'milestoneId' )
219+
220+ if ( hasMilestone ) {
221+ delete challengeDetails . milestoneId
222+ }
223+ return updateChallenge ( challengeId , challengeDetails ) . then ( async challenge => {
224+ if ( hasMilestone ) {
225+ if ( milestoneId && milestoneId !== - 1 ) {
226+ await saveChallengeAsPhaseProduct ( projectId , milestoneId , challengeId )
227+ challenge . milestoneId = milestoneId
228+ } else {
229+ await removeChallengeFromPhaseProduct ( projectId , challengeId )
230+ challenge . milestoneId = milestoneId
231+ }
232+ }
233+ return challenge
234+ } ) . then ( ( challenge ) => {
216235 return dispatch ( {
217236 type : UPDATE_CHALLENGE_DETAILS_SUCCESS ,
218237 challengeDetails : challenge
@@ -231,26 +250,39 @@ export function updateChallengeDetails (challengeId, challengeDetails) {
231250 * Create a new challenge
232251 *
233252 * @param {Object } challengeDetails challenge data
253+ * @param {String } projectId project id
234254 *
235255 * @returns {Promise<{ type: string, challengeDetails: object }> } action object
236256 */
237- export function createChallenge ( challengeDetails ) {
257+ export function createChallenge ( challengeDetails , projectId ) {
258+ console . log ( challengeDetails )
238259 return async ( dispatch ) => {
239260 dispatch ( {
240261 type : CREATE_CHALLENGE_PENDING
241262 } )
242-
243- return createChallengeAPI ( challengeDetails ) . then ( ( challenge ) => {
244- return dispatch ( {
245- type : CREATE_CHALLENGE_SUCCESS ,
246- challengeDetails : challenge
263+ const milestoneId = challengeDetails . milestoneId
264+ if ( milestoneId ) {
265+ delete challengeDetails . milestoneId
266+ }
267+ return createChallengeAPI ( challengeDetails )
268+ . then ( async challenge => {
269+ if ( milestoneId && milestoneId !== - 1 ) {
270+ await saveChallengeAsPhaseProduct ( projectId , milestoneId , challenge . id , true )
271+ challenge . milestoneId = milestoneId
272+ }
273+ return challenge
247274 } )
248- } ) . catch ( ( e ) => {
249- dispatch ( {
250- type : CREATE_CHALLENGE_FAILURE ,
251- error : e
275+ . then ( ( challenge ) => {
276+ return dispatch ( {
277+ type : CREATE_CHALLENGE_SUCCESS ,
278+ challengeDetails : challenge
279+ } )
280+ } ) . catch ( ( e ) => {
281+ dispatch ( {
282+ type : CREATE_CHALLENGE_FAILURE ,
283+ error : e
284+ } )
252285 } )
253- } )
254286 }
255287}
256288
@@ -261,16 +293,32 @@ export function createChallenge (challengeDetails) {
261293 *
262294 * @param {String } challengeId challenge id
263295 * @param {Object } partialChallengeDetails partial challenge data
264- *
296+ * @param { String } projectId project id
265297 * @returns {Promise<{ type: string, challengeDetails: object }> } action object
266298 */
267- export function partiallyUpdateChallengeDetails ( challengeId , partialChallengeDetails ) {
299+ export function partiallyUpdateChallengeDetails ( challengeId , partialChallengeDetails , projectId ) {
268300 return async ( dispatch ) => {
269301 dispatch ( {
270302 type : UPDATE_CHALLENGE_DETAILS_PENDING
271303 } )
272-
273- return patchChallenge ( challengeId , partialChallengeDetails ) . then ( ( challenge ) => {
304+ const milestoneId = partialChallengeDetails . milestoneId
305+ // Check if milestone is deleted or updated
306+ const hasMilestone = _ . has ( partialChallengeDetails , 'milestoneId' )
307+ if ( hasMilestone ) {
308+ delete partialChallengeDetails . milestoneId
309+ }
310+ return patchChallenge ( challengeId , partialChallengeDetails ) . then ( async challenge => {
311+ if ( hasMilestone ) {
312+ if ( milestoneId && milestoneId !== - 1 ) {
313+ await saveChallengeAsPhaseProduct ( projectId , milestoneId , challenge . id )
314+ challenge . milestoneId = milestoneId
315+ } else {
316+ await removeChallengeFromPhaseProduct ( projectId , challengeId )
317+ challenge . milestoneId = milestoneId
318+ }
319+ }
320+ return challenge
321+ } ) . then ( ( challenge ) => {
274322 return dispatch ( {
275323 type : UPDATE_CHALLENGE_DETAILS_SUCCESS ,
276324 challengeDetails : challenge
@@ -284,13 +332,15 @@ export function partiallyUpdateChallengeDetails (challengeId, partialChallengeDe
284332 }
285333}
286334
287- export function deleteChallenge ( challengeId ) {
335+ export function deleteChallenge ( challengeId , projectId ) {
288336 return async ( dispatch ) => {
289337 dispatch ( {
290338 type : DELETE_CHALLENGE_PENDING
291339 } )
292-
293- return deleteChallengeAPI ( challengeId ) . then ( ( challenge ) => {
340+ return deleteChallengeAPI ( challengeId ) . then ( async challenge => {
341+ await removeChallengeFromPhaseProduct ( projectId , challengeId )
342+ return challenge
343+ } ) . then ( ( challenge ) => {
294344 return dispatch ( {
295345 type : DELETE_CHALLENGE_SUCCESS ,
296346 challengeDetails : challenge
0 commit comments