Skip to content

Commit ad5973a

Browse files
committed
refactor(emails): drop optimistic update from WC email toggle (NPPD-1527)
Per dkoo's review of #4727, the optimistic status update on toggleWcEmail added specialized fetch + manual rollback logic that didn't match how status updates work in the rest of the wizards. Fall back to the same pattern as updateStatus in this file: fire the API call, refetch on success, surface a notice on failure.
1 parent c5e7984 commit ad5973a

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

  • src/wizards/newspack/views/settings/emails

src/wizards/newspack/views/settings/emails/emails.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,21 @@ const Emails = () => {
115115
[ postType, fetchData ]
116116
);
117117

118-
const toggleWcEmail = useCallback( ( wcPostId: string, enabled: boolean ) => {
119-
setError( null );
120-
const previousStatus = enabled ? 'draft' : 'publish';
121-
setData( prev => prev.map( email => ( email.post_id === wcPostId ? { ...email, status: enabled ? 'publish' : 'draft' } : email ) ) );
122-
apiFetch( {
123-
path: `/newspack/v1/wizard/newspack-settings/emails/${ wcPostId.replace( 'wc:', '' ) }/toggle`,
124-
method: 'POST',
125-
data: { enabled },
126-
} ).catch( () => {
127-
setData( prev => prev.map( email => ( email.post_id === wcPostId ? { ...email, status: previousStatus } : email ) ) );
128-
setError( __( 'Failed to update email status.', 'newspack-plugin' ) );
129-
} );
130-
}, [] );
118+
const toggleWcEmail = useCallback(
119+
( wcPostId: string, enabled: boolean ) => {
120+
setError( null );
121+
apiFetch( {
122+
path: `/newspack/v1/wizard/newspack-settings/emails/${ wcPostId.replace( 'wc:', '' ) }/toggle`,
123+
method: 'POST',
124+
data: { enabled },
125+
} )
126+
.then( () => fetchData() )
127+
.catch( () => {
128+
setError( __( 'Failed to update email status.', 'newspack-plugin' ) );
129+
} );
130+
},
131+
[ fetchData ]
132+
);
131133

132134
const resetEmail = useCallback(
133135
( postId: number ) => {

0 commit comments

Comments
 (0)