Skip to content

Commit 4a18ea1

Browse files
committed
Fetch new task when old one was snoozed/completed/deleted
1 parent ff16280 commit 4a18ea1

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

assets/js/suggested-task.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ prplSuggestedTask = {
162162
post.destroy( { url } ).then( () => {
163163
// Remove the task from the todo list.
164164
prplSuggestedTask.removeTaskElement( postId );
165+
166+
// Fetch and inject a replacement task
167+
prplSuggestedTask.fetchAndInjectReplacementTask();
168+
165169
setTimeout(
166170
() =>
167171
window.dispatchEvent(
@@ -280,6 +284,9 @@ prplSuggestedTask = {
280284
)
281285
);
282286

287+
// Fetch and inject a replacement task for non-user tasks
288+
prplSuggestedTask.fetchAndInjectReplacementTask();
289+
283290
// Resolve immediately for non-user tasks
284291
resolve( {
285292
postId,
@@ -376,6 +383,9 @@ prplSuggestedTask = {
376383
} );
377384
postModelToSave.save().then( () => {
378385
prplSuggestedTask.removeTaskElement( postId );
386+
387+
// Fetch and inject a replacement task
388+
prplSuggestedTask.fetchAndInjectReplacementTask();
379389
} );
380390
},
381391

@@ -502,6 +512,42 @@ prplSuggestedTask = {
502512
*/
503513
removeTaskElement: ( postId ) =>
504514
prplSuggestedTask.getTaskElement( postId )?.remove(),
515+
516+
/**
517+
* Fetch and inject a replacement task after one is removed.
518+
*
519+
* Replacement tasks are always fetched for the suggested-tasks-list,
520+
* which excludes user tasks (user tasks have their own todo list).
521+
*/
522+
fetchAndInjectReplacementTask: () => {
523+
// Collect all currently visible task IDs from the DOM
524+
const visibleTaskIds = Array.from(
525+
document.querySelectorAll( '.prpl-suggested-task[data-post-id]' )
526+
).map( ( el ) => parseInt( el.getAttribute( 'data-post-id' ) ) );
527+
528+
// Combine with injectedItemIds to ensure we have a complete exclusion list
529+
const allTaskIds = [
530+
...new Set( [
531+
...prplSuggestedTask.injectedItemIds,
532+
...visibleTaskIds,
533+
] ),
534+
];
535+
536+
// Update injectedItemIds to include any tasks that might have been missed
537+
prplSuggestedTask.injectedItemIds = allTaskIds;
538+
539+
const fetchArgs = {
540+
status: 'publish',
541+
per_page: 1,
542+
exclude_provider: 'user', // Always exclude user tasks from suggested-tasks-list
543+
};
544+
545+
prplSuggestedTask.fetchItems( fetchArgs ).then( ( items ) => {
546+
if ( items && items.length > 0 ) {
547+
prplSuggestedTask.injectItems( items );
548+
}
549+
} );
550+
},
505551
};
506552

507553
/**

0 commit comments

Comments
 (0)