Skip to content

Commit

Permalink
prepopulate cache with newly added links
Browse files Browse the repository at this point in the history
  • Loading branch information
flootr committed Nov 14, 2023
1 parent 7e349d8 commit 441292c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions client/me/profile-links/data/use-add-profile-link-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
AddProfileLinksPayload,
AddProfileLinksResponse,
AddProfileLinksVariables,
ProfileLink,
} from './types';

export const useAddProfileLinkMutation = ( options: AddProfileLinksMutationOptions = {} ) => {
Expand All @@ -16,6 +17,18 @@ export const useAddProfileLinkMutation = ( options: AddProfileLinksMutationOptio
},
...options,
onSuccess: ( ...args ) => {
const [ data ] = args;

queryClient.setQueryData(
[ 'profile-links' ],
( cachedProfileLinks: ProfileLink[] | undefined ) => {
if ( ! cachedProfileLinks || ! data.added ) {
return cachedProfileLinks;
}
return [ ...cachedProfileLinks, ...data.added ];
}
);

queryClient.invalidateQueries( [ 'profile-links' ] );
options.onSuccess?.( ...args );
},
Expand Down

0 comments on commit 441292c

Please sign in to comment.