Skip to content

Commit

Permalink
Add optimistic update revert, remove async from sync function (#41878)
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweller committed Feb 20, 2025
1 parent bd7bd61 commit 271839a
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function useToggleAccountProtectionMutation(): UseMutationResult

return useMutation( {
mutationFn: API.toggleAccountProtection,
onMutate: async () => {
onMutate: () => {
showSavingNotice();

// Get the current cached data
Expand All @@ -37,7 +37,15 @@ export default function useToggleAccountProtectionMutation(): UseMutationResult
onSuccess: () => {
showSuccessNotice( __( 'Changes saved.', 'jetpack-protect' ) );
},
onError: () => {
onError: ( error, variables, context ) => {
// If the request failed, revert the optimistic update.
if ( context?.previousData ) {
queryClient.setQueryData< AccountProtectionStatus >(
[ QUERY_ACCOUNT_PROTECTION_KEY ],
context.previousData
);
}

showErrorNotice( __( 'An error occurred.', 'jetpack-protect' ) );
},
onSettled: () => {
Expand Down

0 comments on commit 271839a

Please sign in to comment.