Skip to content

Commit a2badae

Browse files
committed
Optimistically update only for inbox
1 parent 8358a75 commit a2badae

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

projects/packages/forms/src/dashboard/inbox/dataviews/actions.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,16 @@ export const markAsReadAction = {
312312
async callback( items, { registry } ) {
313313
const { editEntityRecord } = registry.dispatch( coreStore );
314314
const { createSuccessNotice, createErrorNotice } = registry.dispatch( noticesStore );
315+
315316
const promises = await Promise.allSettled(
316-
items.map( async ( { id } ) => {
317+
items.map( async ( { id, status } ) => {
317318
// Update entity in store
318319
editEntityRecord( 'postType', 'feedback', id, { is_unread: false } );
319320

320-
// Immediately update menu counters optimistically to avoid delays
321-
updateMenuCounterOptimistically( -items.length );
321+
// Immediately update menu counters optimistically to avoid delays, but only for inbox
322+
if ( status === 'publish' ) {
323+
updateMenuCounterOptimistically( -1 );
324+
}
322325

323326
// Update on server
324327
return apiFetch( {
@@ -327,15 +330,17 @@ export const markAsReadAction = {
327330
data: { is_unread: false },
328331
} )
329332
.then( ( { count } ) => {
330-
// Update the unread count in the menu.
333+
// Update menu counter with accurate count from server.
331334
updateMenuCounter( count );
332335
} )
333336
.catch( () => {
334337
// Revert the change in the store if the server update fails.
335338
editEntityRecord( 'postType', 'feedback', id, { is_unread: true } );
336339

337-
// Revert the change in the sidebar
338-
updateMenuCounterOptimistically( items.length );
340+
// Revert the optimistic change in the sidebar.
341+
if ( status === 'publish' ) {
342+
updateMenuCounterOptimistically( 1 );
343+
}
339344

340345
throw new Error( 'Failed to mark as read' );
341346
} );
@@ -386,12 +391,14 @@ export const markAsUnreadAction = {
386391
const { editEntityRecord } = registry.dispatch( coreStore );
387392
const { createSuccessNotice, createErrorNotice } = registry.dispatch( noticesStore );
388393
const promises = await Promise.allSettled(
389-
items.map( async ( { id } ) => {
394+
items.map( async ( { id, status } ) => {
390395
// Update entity in store
391396
editEntityRecord( 'postType', 'feedback', id, { is_unread: true } );
392397

393-
// Immediately update menu counters optimistically to avoid delays
394-
updateMenuCounterOptimistically( items.length );
398+
// Immediately update menu counters optimistically to avoid delays, but only for inbox
399+
if ( status === 'publish' ) {
400+
updateMenuCounterOptimistically( 1 );
401+
}
395402

396403
// Update on server
397404
return apiFetch( {
@@ -400,15 +407,17 @@ export const markAsUnreadAction = {
400407
data: { is_unread: true },
401408
} )
402409
.then( ( { count } ) => {
403-
// Update the unread count in the menu.
410+
// Update menu counter with accurate count from server.
404411
updateMenuCounter( count );
405412
} )
406413
.catch( () => {
407414
// Revert the change in the store if the server update fails.
408415
editEntityRecord( 'postType', 'feedback', id, { is_unread: false } );
409416

410-
// Revert the change in the sidebar
411-
updateMenuCounterOptimistically( -items.length );
417+
// Revert the optimistic change in the sidebar.
418+
if ( status === 'publish' ) {
419+
updateMenuCounterOptimistically( -1 );
420+
}
412421

413422
throw new Error( 'Failed to mark as unread' );
414423
} );

projects/packages/forms/src/dashboard/inbox/response.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,9 @@ const InboxResponse = ( {
547547
} );
548548

549549
// Immediately update menu counters optimistically to avoid delays
550-
updateMenuCounterOptimistically( -1 );
550+
if ( response.status === 'publish' ) {
551+
updateMenuCounterOptimistically( -1 );
552+
}
551553

552554
// Then update on server
553555
apiFetch( {
@@ -566,7 +568,9 @@ const InboxResponse = ( {
566568
} );
567569

568570
// Revert the change in the sidebar
569-
updateMenuCounterOptimistically( 1 );
571+
if ( response.status === 'publish' ) {
572+
updateMenuCounterOptimistically( 1 );
573+
}
570574
} );
571575
}, [ response, editEntityRecord, hasMarkedSelfAsRead ] );
572576

0 commit comments

Comments
 (0)