@@ -242,59 +242,148 @@ prplSuggestedTask = {
242242 * @param {number } postId The post ID.
243243 */
244244 maybeComplete : ( postId ) => {
245- // Get the task.
246- const post = new wp . api . models . Prpl_recommendations ( { id : postId } ) ;
247- post . fetch ( ) . then ( ( postData ) => {
248- const taskProviderId = prplTerms . getTerm (
249- postData ?. [ prplTerms . provider ] ,
250- prplTerms . provider
251- ) . slug ;
252- const taskCategorySlug = prplTerms . getTerm (
253- postData ?. [ prplTerms . category ] ,
254- prplTerms . category
255- ) . slug ;
256-
257- const el = prplSuggestedTask . getTaskElement ( postId ) ;
245+ // Return the promise chain so callers can wait for completion
246+ return new Promise ( ( resolve , reject ) => {
247+ // Get the task.
248+ const post = new wp . api . models . Prpl_recommendations ( {
249+ id : postId ,
250+ } ) ;
251+ post . fetch ( )
252+ . then ( ( postData ) => {
253+ const taskProviderId = prplTerms . getTerm (
254+ postData ?. [ prplTerms . provider ] ,
255+ prplTerms . provider
256+ ) . slug ;
257+ const taskCategorySlug = prplTerms . getTerm (
258+ postData ?. [ prplTerms . category ] ,
259+ prplTerms . category
260+ ) . slug ;
261+
262+ const el = prplSuggestedTask . getTaskElement ( postId ) ;
263+
264+ // Dismissable tasks don't have pending status, it's either publish or trash.
265+ const newStatus =
266+ 'publish' === postData . status ? 'trash' : 'publish' ;
267+
268+ // Disable the checkbox for RR tasks, to prevent multiple clicks.
269+ el . querySelector (
270+ '.prpl-suggested-task-checkbox'
271+ ) . setAttribute ( 'disabled' , 'disabled' ) ;
272+
273+ post . set ( 'status' , newStatus )
274+ . save ( )
275+ . then ( ( ) => {
276+ prplSuggestedTask . runTaskAction (
277+ postId ,
278+ 'trash' === newStatus ? 'complete' : 'pending'
279+ ) ;
280+ const eventPoints = parseInt (
281+ postData ?. meta ?. prpl_points
282+ ) ;
258283
259- // Dismissable tasks don't have pending status, it's either publish or trash.
260- const newStatus =
261- 'publish' === postData . status ? 'trash' : 'publish' ;
284+ // Task is trashed, check if we need to celebrate.
285+ if ( 'trash' === newStatus ) {
286+ el . setAttribute (
287+ 'data-task-action' ,
288+ 'celebrate'
289+ ) ;
290+ if ( 'user' === taskProviderId ) {
291+ // Set class to trigger strike through animation.
292+ el . classList . add (
293+ 'prpl-suggested-task-celebrated'
294+ ) ;
295+
296+ setTimeout ( ( ) => {
297+ // Move task from published to trash.
298+ document
299+ . getElementById (
300+ 'todo-list-completed'
301+ )
302+ . insertAdjacentElement (
303+ 'beforeend' ,
304+ el
305+ ) ;
306+
307+ // Remove the class to trigger the strike through animation.
308+ el . classList . remove (
309+ 'prpl-suggested-task-celebrated'
310+ ) ;
311+
312+ window . dispatchEvent (
313+ new CustomEvent (
314+ 'prpl/grid/resize'
315+ )
316+ ) ;
317+
318+ // Remove the disabled attribute for user tasks, so they can be clicked again.
319+ el . querySelector (
320+ '.prpl-suggested-task-checkbox'
321+ ) . removeAttribute ( 'disabled' ) ;
322+
323+ // Resolve the promise after the timeout completes
324+ resolve ( {
325+ postId,
326+ newStatus,
327+ eventPoints,
328+ } ) ;
329+ } , 2000 ) ;
330+ } else {
331+ /**
332+ * Strike completed tasks and remove them from the DOM.
333+ */
334+ document . dispatchEvent (
335+ new CustomEvent (
336+ 'prpl/removeCelebratedTasks'
337+ )
338+ ) ;
339+
340+ // Inject more tasks from the same category.
341+ prplSuggestedTask . injectItemsFromCategory ( {
342+ category : taskCategorySlug ,
343+ status : [ 'publish' ] ,
344+ } ) ;
345+
346+ // Resolve immediately for non-user tasks
347+ resolve ( {
348+ postId,
349+ newStatus,
350+ eventPoints,
351+ } ) ;
352+ }
353+
354+ // We trigger celebration only if the task has points.
355+ if ( 0 < eventPoints ) {
356+ prplUpdateRaviGauge ( eventPoints ) ;
357+
358+ // Trigger the celebration event (confetti).
359+ document . dispatchEvent (
360+ new CustomEvent (
361+ 'prpl/celebrateTasks' ,
362+ {
363+ detail : { element : el } ,
364+ }
365+ )
366+ ) ;
367+ }
368+ } else if (
369+ 'publish' === newStatus &&
370+ 'user' === taskProviderId
371+ ) {
372+ // This is only possible for user tasks.
373+ // Set the task action to publish.
374+ el . setAttribute (
375+ 'data-task-action' ,
376+ 'publish'
377+ ) ;
262378
263- // Disable the checkbox for RR tasks, to prevent multiple clicks.
264- el . querySelector ( '.prpl-suggested-task-checkbox' ) . setAttribute (
265- 'disabled' ,
266- 'disabled'
267- ) ;
379+ // Update the Ravi gauge.
380+ prplUpdateRaviGauge ( 0 - eventPoints ) ;
268381
269- post . set ( 'status' , newStatus )
270- . save ( )
271- . then ( ( ) => {
272- prplSuggestedTask . runTaskAction (
273- postId ,
274- 'trash' === newStatus ? 'complete' : 'pending'
275- ) ;
276- const eventPoints = parseInt ( postData ?. meta ?. prpl_points ) ;
277-
278- // Task is trashed, check if we need to celebrate.
279- if ( 'trash' === newStatus ) {
280- el . setAttribute ( 'data-task-action' , 'celebrate' ) ;
281- if ( 'user' === taskProviderId ) {
282- // Set class to trigger strike through animation.
283- el . classList . add (
284- 'prpl-suggested-task-celebrated'
285- ) ;
286-
287- setTimeout ( ( ) => {
288- // Move task from published to trash.
382+ // Move task from trash to published.
289383 document
290- . getElementById ( 'todo-list-completed ' )
384+ . getElementById ( 'todo-list' )
291385 . insertAdjacentElement ( 'beforeend' , el ) ;
292386
293- // Remove the class to trigger the strike through animation.
294- el . classList . remove (
295- 'prpl-suggested-task-celebrated'
296- ) ;
297-
298387 window . dispatchEvent (
299388 new CustomEvent ( 'prpl/grid/resize' )
300389 ) ;
@@ -303,59 +392,14 @@ prplSuggestedTask = {
303392 el . querySelector (
304393 '.prpl-suggested-task-checkbox'
305394 ) . removeAttribute ( 'disabled' ) ;
306- } , 2000 ) ;
307- } else {
308- /**
309- * Strike completed tasks and remove them from the DOM.
310- */
311- document . dispatchEvent (
312- new CustomEvent ( 'prpl/removeCelebratedTasks' )
313- ) ;
314-
315- // Inject more tasks from the same category.
316- prplSuggestedTask . injectItemsFromCategory ( {
317- category : taskCategorySlug ,
318- status : [ 'publish' ] ,
319- } ) ;
320- }
321-
322- // We trigger celebration only if the task has points.
323- if ( 0 < eventPoints ) {
324- prplUpdateRaviGauge ( eventPoints ) ;
325-
326- // Trigger the celebration event (confetti).
327- document . dispatchEvent (
328- new CustomEvent ( 'prpl/celebrateTasks' , {
329- detail : { element : el } ,
330- } )
331- ) ;
332- }
333- } else if (
334- 'publish' === newStatus &&
335- 'user' === taskProviderId
336- ) {
337- // This is only possible for user tasks.
338- // Set the task action to publish.
339- el . setAttribute ( 'data-task-action' , 'publish' ) ;
340-
341- // Update the Ravi gauge.
342- prplUpdateRaviGauge ( 0 - eventPoints ) ;
343-
344- // Move task from trash to published.
345- document
346- . getElementById ( 'todo-list' )
347- . insertAdjacentElement ( 'beforeend' , el ) ;
348-
349- window . dispatchEvent (
350- new CustomEvent ( 'prpl/grid/resize' )
351- ) ;
352395
353- // Remove the disabled attribute for user tasks, so they can be clicked again.
354- el . querySelector (
355- '.prpl-suggested-task-checkbox'
356- ) . removeAttribute ( 'disabled' ) ;
357- }
358- } ) ;
396+ // Resolve immediately for publish actions
397+ resolve ( { postId, newStatus, eventPoints } ) ;
398+ }
399+ } )
400+ . catch ( reject ) ;
401+ } )
402+ . catch ( reject ) ;
359403 } ) ;
360404 } ,
361405
0 commit comments