@@ -39,6 +39,7 @@ import { TaskLoadMethod } from "../../../services/Task/TaskLoadMethod/TaskLoadMe
3939import { fetchTaskForReview } from "../../../services/Task/TaskReview/TaskReview" ;
4040import { fetchUser } from "../../../services/User/User" ;
4141import { renewVirtualChallenge } from "../../../services/VirtualChallenge/VirtualChallenge" ;
42+ import { waitFor } from "@testing-library/react" ;
4243
4344const CHALLENGE_STALE = 300000 ; // 5 minutes
4445const PROJECT_STALE = 300000 ; // 5 minutes
@@ -165,7 +166,7 @@ export const mapDispatchToProps = (dispatch, ownProps) => {
165166 /**
166167 * Invoke to mark a task as complete with the given status
167168 */
168- completeTask : (
169+ completeTask : async (
169170 task ,
170171 challengeId ,
171172 taskStatus ,
@@ -183,77 +184,79 @@ export const mapDispatchToProps = (dispatch, ownProps) => {
183184 const taskId = task . id ;
184185
185186 // Work to be done after the status is set
186- const doAfter = ( ) => {
187- if ( _isString ( comment ) && comment . length > 0 ) {
188- if ( taskBundle ) {
189- dispatch (
190- addTaskBundleComment (
191- taskBundle . bundleId ,
192- AsMappableBundle ( taskBundle ) . primaryTaskId ( ) || taskId ,
193- comment ,
194- taskStatus ,
195- ) ,
196- ) ;
197- } else {
198- dispatch ( addTaskComment ( taskId , comment , taskStatus ) ) ;
187+ const doAfter = ( ) =>
188+ new Promise ( async ( resolve ) => {
189+ if ( _isString ( comment ) && comment . length > 0 ) {
190+ if ( taskBundle ) {
191+ await dispatch (
192+ addTaskBundleComment (
193+ taskBundle . bundleId ,
194+ AsMappableBundle ( taskBundle ) . primaryTaskId ( ) || taskId ,
195+ comment ,
196+ taskStatus ,
197+ ) ,
198+ ) ;
199+ } else {
200+ await dispatch ( addTaskComment ( taskId , comment , taskStatus ) ) ;
201+ }
199202 }
200- }
201203
202- // Update the user in the background to get their latest score
203- setTimeout ( ( ) => dispatch ( fetchUser ( userId ) ) , 100 ) ;
204+ // Update the user in the background to get their latest score
205+ await dispatch ( fetchUser ( userId ) ) ;
204206
205- // Updating the challenge actions will allow us to show more accurate
206- // completion progress, but this can be done in the background
207- setTimeout ( ( ) => dispatch ( fetchChallengeActions ( challengeId ) ) , 500 ) ;
207+ // Updating the challenge actions will allow us to show more accurate
208+ // completion progress
209+ await dispatch ( fetchChallengeActions ( challengeId ) ) ;
208210
209- // If working on a virtual challenge, renew it (extend its expiration)
210- // since we've seen some activity, but this can be done in the
211- // background
212- if ( _isFinite ( ownProps . virtualChallengeId ) ) {
213- setTimeout ( ( ) => dispatch ( renewVirtualChallenge ( ownProps . virtualChallengeId ) ) , 1000 ) ;
214- }
211+ // If working on a virtual challenge, renew it (extend its expiration)
212+ if ( _isFinite ( ownProps . virtualChallengeId ) ) {
213+ await dispatch ( renewVirtualChallenge ( ownProps . virtualChallengeId ) ) ;
214+ }
215215
216- if ( taskLoadBy ) {
217- // Start loading the next task from the challenge.
218- const loadNextTask = _isFinite ( requestedNextTask )
219- ? nextRequestedTask ( dispatch , ownProps , requestedNextTask )
220- : nextRandomTask ( dispatch , ownProps , taskId , taskLoadBy ) ;
216+ if ( taskLoadBy ) {
217+ // Start loading the next task from the challenge.
218+ const loadNextTask = _isFinite ( requestedNextTask )
219+ ? await nextRequestedTask ( dispatch , ownProps , requestedNextTask )
220+ : await nextRandomTask ( dispatch , ownProps , taskId , taskLoadBy ) ;
221221
222- return loadNextTask
223- . then ( ( newTask ) => visitNewTask ( dispatch , ownProps , taskId , newTask ) )
224- . catch ( ( ) => {
222+ try {
223+ await visitNewTask ( dispatch , ownProps , taskId , loadNextTask ) ;
224+ } catch ( error ) {
225225 ownProps . history . push ( `/browse/challenges/${ challengeId } ` ) ;
226- } ) ;
227- }
228- } ;
226+ }
227+ }
228+ resolve ( ) ;
229+ } ) ;
229230
230231 let cooperativeWorkSummary = null ;
231232 if ( AsCooperativeWork ( task ) . isTagType ( ) ) {
232233 cooperativeWorkSummary = AsCooperativeWork ( task ) . tagChangeSummary ( tagEdits ) ;
233234 }
234235
235- return dispatch (
236- taskBundle
237- ? completeTaskBundle (
238- taskBundle . bundleId ,
239- AsMappableBundle ( taskBundle ) . primaryTaskId ( ) || taskId ,
240- taskStatus ,
241- needsReview ,
242- tags ,
243- cooperativeWorkSummary ,
244- osmComment ,
245- completionResponses ,
246- )
247- : completeTask (
248- taskId ,
249- taskStatus ,
250- needsReview ,
251- tags ,
252- cooperativeWorkSummary ,
253- osmComment ,
254- completionResponses ,
255- ) ,
256- ) . then ( ( ) => doAfter ( ) ) ;
236+ const completeAction = taskBundle
237+ ? await completeTaskBundle (
238+ taskBundle . bundleId ,
239+ AsMappableBundle ( taskBundle ) . primaryTaskId ( ) || taskId ,
240+ taskStatus ,
241+ needsReview ,
242+ tags ,
243+ cooperativeWorkSummary ,
244+ osmComment ,
245+ completionResponses ,
246+ )
247+ : await completeTask (
248+ taskId ,
249+ taskStatus ,
250+ needsReview ,
251+ tags ,
252+ cooperativeWorkSummary ,
253+ osmComment ,
254+ completionResponses ,
255+ ) ;
256+
257+ await dispatch ( completeAction ) ;
258+ const afterResult = await doAfter ( ) ;
259+ return afterResult ;
257260 } ,
258261
259262 /**
@@ -393,16 +396,25 @@ export const visitNewTask = function (dispatch, props, currentTaskId, newTask) {
393396 // If challenge is complete, redirect home with note to congratulate user
394397 if ( _isFinite ( props . virtualChallengeId ) ) {
395398 // We don't get a status for virtual challenges, so just assume we're done
396- props . history . push ( "/browse/challenges" , { congratulate : true , warn : false } ) ;
399+ props . history . push ( "/browse/challenges" , {
400+ congratulate : true ,
401+ warn : false ,
402+ } ) ;
397403 return Promise . resolve ( ) ;
398404 } else {
399405 const challengeId = challengeIdFromRoute ( props , props . challengeId ) ;
400406 return dispatch ( fetchChallenge ( challengeId ) ) . then ( ( normalizedResults ) => {
401407 const challenge = normalizedResults . entities . challenges [ normalizedResults . result ] ;
402408 if ( challenge . status === CHALLENGE_STATUS_FINISHED ) {
403- props . history . push ( "/browse/challenges" , { congratulate : true , warn : false } ) ;
409+ props . history . push ( "/browse/challenges" , {
410+ congratulate : true ,
411+ warn : false ,
412+ } ) ;
404413 } else {
405- props . history . push ( "/browse/challenges" , { warn : true , congratulate : false } ) ;
414+ props . history . push ( "/browse/challenges" , {
415+ warn : true ,
416+ congratulate : false ,
417+ } ) ;
406418 }
407419 } ) ;
408420 }
0 commit comments