Skip to content

Commit

Permalink
Fix useDelete does not invalidate cache in pessimistic mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Jan 14, 2025
1 parent 4bdf7b5 commit d179c21
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions packages/ra-core/src/dataProvider/useDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,6 @@ export const useDelete = <
context: unknown
) => {
if (mode.current === 'pessimistic') {
// update the getOne and getList query cache with the new result
const {
resource: callTimeResource = resource,
id: callTimeId = id,
} = variables;
updateCache({
resource: callTimeResource,
id: callTimeId,
});

if (
mutationOptions.onSuccess &&
!hasCallTimeOnSuccess.current
Expand All @@ -281,12 +271,10 @@ export const useDelete = <
variables: Partial<UseDeleteMutateParams<RecordType>> = {},
context: { snapshot: Snapshot }
) => {
if (mode.current === 'optimistic' || mode.current === 'undoable') {
// Always refetch after error or success:
context.snapshot.forEach(([queryKey]) => {
queryClient.invalidateQueries({ queryKey });
});
}
// Always refetch after error or success:
context.snapshot.forEach(([queryKey]) => {
queryClient.invalidateQueries({ queryKey });
});

if (mutationOptions.onSettled && !hasCallTimeOnSettled.current) {
return mutationOptions.onSettled(
Expand Down Expand Up @@ -330,13 +318,6 @@ export const useDelete = <
mode.current = mutationMode;
}

if (mode.current === 'pessimistic') {
return mutation.mutate(
{ resource: callTimeResource, ...callTimeParams },
otherCallTimeOptions
);
}

const {
id: callTimeId = id,
previousData: callTimePreviousData = previousData,
Expand Down Expand Up @@ -372,6 +353,13 @@ export const useDelete = <
[] as Snapshot
);

if (mode.current === 'pessimistic') {
return mutation.mutate(
{ resource: callTimeResource, ...callTimeParams },
otherCallTimeOptions
);
}

// Cancel any outgoing re-fetches (so they don't overwrite our optimistic update)
await Promise.all(
snapshot.current.map(([queryKey]) =>
Expand Down

0 comments on commit d179c21

Please sign in to comment.