@@ -29,59 +29,45 @@ const prplSuggestedTasksWidget = {
2929 return ;
3030 }
3131
32- const celebrationPromises = [ ] ;
33-
34- // Loop through each provider and inject items.
35- for ( const category in prplSuggestedTask . maxItemsPerCategory ) {
36- if ( 'user' === category ) {
37- continue ;
38- }
39-
40- // Inject published tasks.
41- prplSuggestedTask . injectItemsFromCategory ( {
42- category,
43- status : [ 'publish' ] ,
44- per_page : prplSuggestedTask . maxItemsPerCategory [ category ] ,
45- } ) ;
46-
47- // We trigger celebration only on Progress Planner dashboard page.
48- if ( ! prplSuggestedTask . delayCelebration ) {
49- // Inject pending celebration tasks.
50- celebrationPromises . push (
51- prplSuggestedTask
52- . injectItemsFromCategory ( {
53- category,
54- status : [ 'pending' ] ,
55- per_page : 100 ,
56- } )
57- . then ( ( data ) => {
58- // If there were pending tasks.
59- if ( data . length ) {
60- // Set post status to trash.
61- data . forEach ( ( task ) => {
62- const post =
63- new wp . api . models . Prpl_recommendations (
64- {
65- id : task . id ,
66- }
67- ) ;
68- // Destroy the post, without the force parameter.
69- post . destroy ( { url : post . url ( ) } ) ;
70- } ) ;
71- }
72- } )
32+ // If preloaded tasks are available, inject them.
33+ if ( 'undefined' !== typeof prplSuggestedTask . tasks ) {
34+ // Inject the tasks.
35+ if ( Object . keys ( prplSuggestedTask . tasks . pendingTasks ) . length ) {
36+ Object . keys ( prplSuggestedTask . tasks . pendingTasks ) . forEach (
37+ ( category ) => {
38+ prplSuggestedTask . injectItems (
39+ prplSuggestedTask . tasks . pendingTasks [ category ]
40+ ) ;
41+ }
7342 ) ;
7443 }
75- }
7644
77- // Trigger celebration once, for all categories.
78- Promise . all ( celebrationPromises ) . then ( ( ) => {
45+ // Inject the pending celebration tasks.
7946 if (
80- 0 <
81- document . querySelectorAll (
82- '.prpl-suggested-tasks-list [data-task-action="celebrate"]'
83- ) . length
47+ Object . keys ( prplSuggestedTask . tasks . pendingCelebrationTasks )
48+ . length
8449 ) {
50+ Object . keys (
51+ prplSuggestedTask . tasks . pendingCelebrationTasks
52+ ) . forEach ( ( category ) => {
53+ prplSuggestedTask . injectItems (
54+ prplSuggestedTask . tasks . pendingCelebrationTasks [
55+ category
56+ ]
57+ ) ;
58+
59+ // Set post status to trash.
60+ prplSuggestedTask . tasks . pendingCelebrationTasks [
61+ category
62+ ] . forEach ( ( task ) => {
63+ const post = new wp . api . models . Prpl_recommendations ( {
64+ id : task . id ,
65+ } ) ;
66+ // Destroy the post, without the force parameter.
67+ post . destroy ( { url : post . url ( ) } ) ;
68+ } ) ;
69+ } ) ;
70+
8571 // Trigger the celebration event (trigger confetti, strike through tasks, remove from DOM).
8672 setTimeout ( ( ) => {
8773 // Trigger the celebration event.
@@ -102,7 +88,86 @@ const prplSuggestedTasksWidget = {
10288 ) ;
10389 } , 3000 ) ;
10490 }
105- } ) ;
91+
92+ // Toggle the "Loading..." text.
93+ prplSuggestedTasksWidget . removeLoadingItems ( ) ;
94+ } else {
95+ // Otherwise, inject tasks from the API.
96+ const celebrationPromises = [ ] ;
97+
98+ // Loop through each provider and inject items.
99+ for ( const category in prplSuggestedTask . maxItemsPerCategory ) {
100+ if ( 'user' === category ) {
101+ continue ;
102+ }
103+
104+ // Inject published tasks.
105+ prplSuggestedTask . injectItemsFromCategory ( {
106+ category,
107+ status : [ 'publish' ] ,
108+ per_page : prplSuggestedTask . maxItemsPerCategory [ category ] ,
109+ } ) ;
110+
111+ // We trigger celebration only on Progress Planner dashboard page.
112+ if ( ! prplSuggestedTask . delayCelebration ) {
113+ // Inject pending celebration tasks.
114+ celebrationPromises . push (
115+ prplSuggestedTask
116+ . injectItemsFromCategory ( {
117+ category,
118+ status : [ 'pending' ] ,
119+ per_page : 100 ,
120+ } )
121+ . then ( ( data ) => {
122+ // If there were pending tasks.
123+ if ( data . length ) {
124+ // Set post status to trash.
125+ data . forEach ( ( task ) => {
126+ const post =
127+ new wp . api . models . Prpl_recommendations (
128+ {
129+ id : task . id ,
130+ }
131+ ) ;
132+ // Destroy the post, without the force parameter.
133+ post . destroy ( { url : post . url ( ) } ) ;
134+ } ) ;
135+ }
136+ } )
137+ ) ;
138+ }
139+ }
140+
141+ // Trigger celebration once, for all categories.
142+ Promise . all ( celebrationPromises ) . then ( ( ) => {
143+ if (
144+ 0 <
145+ document . querySelectorAll (
146+ '.prpl-suggested-tasks-list [data-task-action="celebrate"]'
147+ ) . length
148+ ) {
149+ // Trigger the celebration event (trigger confetti, strike through tasks, remove from DOM).
150+ setTimeout ( ( ) => {
151+ // Trigger the celebration event.
152+ document . dispatchEvent (
153+ new CustomEvent ( 'prpl/celebrateTasks' )
154+ ) ;
155+
156+ /**
157+ * Strike completed tasks and remove them from the DOM.
158+ */
159+ document . dispatchEvent (
160+ new CustomEvent ( 'prpl/removeCelebratedTasks' )
161+ ) ;
162+
163+ // Trigger the grid resize event.
164+ window . dispatchEvent (
165+ new CustomEvent ( 'prpl/grid/resize' )
166+ ) ;
167+ } , 3000 ) ;
168+ }
169+ } ) ;
170+ }
106171 } ,
107172} ;
108173
0 commit comments