Skip to content

Commit 1cc1979

Browse files
committed
fix useMutationWithMutationMode implementation
1 parent c33c6f6 commit 1cc1979

File tree

7 files changed

+53
-125
lines changed

7 files changed

+53
-125
lines changed

packages/ra-core/src/dataProvider/useCreate.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export const useCreate = <
160160

161161
return clonedData;
162162
},
163-
getSnapshot: ({ resource, ...params }, { mutationMode }) => {
163+
getQueryKeys: ({ resource, ...params }, { mutationMode }) => {
164164
const queryKeys: any[] = [
165165
[resource, 'getList'],
166166
[resource, 'getInfiniteList'],
@@ -176,27 +176,7 @@ export const useCreate = <
176176
]);
177177
}
178178

179-
/**
180-
* Snapshot the previous values via queryClient.getQueriesData()
181-
*
182-
* The snapshotData ref will contain an array of tuples [query key, associated data]
183-
*
184-
* @example
185-
* [
186-
* [['posts', 'getOne', { id: '1' }], { id: 1, title: 'Hello' }],
187-
* [['posts', 'getList'], { data: [{ id: 1, title: 'Hello' }], total: 1 }],
188-
* [['posts', 'getMany'], [{ id: 1, title: 'Hello' }]],
189-
* ]
190-
*
191-
* @see https://react-query-v3.tanstack.com/reference/QueryClient#queryclientgetqueriesdata
192-
*/
193-
const snapshot = queryKeys.reduce(
194-
(prev, queryKey) =>
195-
prev.concat(queryClient.getQueriesData({ queryKey })),
196-
[] as Snapshot
197-
);
198-
199-
return snapshot;
179+
return queryKeys;
200180
},
201181
getMutateWithMiddlewares: mutationFn => {
202182
if (getMutateWithMiddlewares) {

packages/ra-core/src/dataProvider/useDelete.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -211,34 +211,14 @@ export const useDelete = <
211211

212212
return params.previousData;
213213
},
214-
getSnapshot: ({ resource }) => {
214+
getQueryKeys: ({ resource }) => {
215215
const queryKeys = [
216216
[resource, 'getList'],
217217
[resource, 'getInfiniteList'],
218218
[resource, 'getMany'],
219219
[resource, 'getManyReference'],
220220
];
221-
222-
/**
223-
* Snapshot the previous values via queryClient.getQueriesData()
224-
*
225-
* The snapshotData ref will contain an array of tuples [query key, associated data]
226-
*
227-
* @example
228-
* [
229-
* [['posts', 'getList'], { data: [{ id: 1, title: 'Hello' }], total: 1 }],
230-
* [['posts', 'getMany'], [{ id: 1, title: 'Hello' }]],
231-
* ]
232-
*
233-
* @see https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientgetqueriesdata
234-
*/
235-
const snapshot = queryKeys.reduce(
236-
(prev, queryKey) =>
237-
prev.concat(queryClient.getQueriesData({ queryKey })),
238-
[] as Snapshot
239-
);
240-
241-
return snapshot;
221+
return queryKeys;
242222
},
243223
onSettled: (
244224
result,

packages/ra-core/src/dataProvider/useDeleteMany.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -238,33 +238,14 @@ export const useDeleteMany = <
238238

239239
return params.ids;
240240
},
241-
getSnapshot: ({ resource }) => {
241+
getQueryKeys: ({ resource }) => {
242242
const queryKeys = [
243243
[resource, 'getList'],
244244
[resource, 'getInfiniteList'],
245245
[resource, 'getMany'],
246246
[resource, 'getManyReference'],
247247
];
248-
249-
/**
250-
* Snapshot the previous values via queryClient.getQueriesData()
251-
*
252-
* The snapshotData ref will contain an array of tuples [query key, associated data]
253-
*
254-
* @example
255-
* [
256-
* [['posts', 'getList'], { data: [{ id: 1, title: 'Hello' }], total: 1 }],
257-
* [['posts', 'getMany'], [{ id: 1, title: 'Hello' }]],
258-
* ]
259-
*
260-
* @see https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientgetqueriesdata
261-
*/
262-
const snapshot = queryKeys.reduce(
263-
(prev, queryKey) =>
264-
prev.concat(queryClient.getQueriesData({ queryKey })),
265-
[] as Snapshot
266-
);
267-
return snapshot;
248+
return queryKeys;
268249
},
269250
onSettled: (
270251
result,

packages/ra-core/src/dataProvider/useMutationWithMutationMode.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('useMutationWithMutationMode', () => {
4343
updateCache: ({ data }) => {
4444
return data;
4545
},
46-
getSnapshot: () => {
46+
getQueryKeys: () => {
4747
return [];
4848
},
4949
...options,

packages/ra-core/src/dataProvider/useMutationWithMutationMode.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const useMutationWithMutationMode = <
2828
mutationFn,
2929
getMutateWithMiddlewares,
3030
updateCache,
31-
getSnapshot,
31+
getQueryKeys,
3232
onUndo,
3333
...mutationOptions
3434
} = options;
@@ -41,7 +41,28 @@ export const useMutationWithMutationMode = <
4141

4242
const mutationFnEvent = useEvent(mutationFn);
4343
const updateCacheEvent = useEvent(updateCache);
44-
const getSnapshotEvent = useEvent(getSnapshot);
44+
const getQueryKeysEvent = useEvent(getQueryKeys);
45+
const getSnapshotEvent = useEvent(
46+
/**
47+
* Snapshot the previous values via queryClient.getQueriesData()
48+
*
49+
* The snapshotData ref will contain an array of tuples [query key, associated data]
50+
*
51+
* @example
52+
* [
53+
* [['posts', 'getList'], { data: [{ id: 1, title: 'Hello' }], total: 1 }],
54+
* [['posts', 'getMany'], [{ id: 1, title: 'Hello' }]],
55+
* ]
56+
*
57+
* @see https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientgetqueriesdata
58+
*/
59+
(queryKeys: Array<QueryKey>) =>
60+
queryKeys.reduce(
61+
(prev, queryKey) =>
62+
prev.concat(queryClient.getQueriesData({ queryKey })),
63+
[] as Snapshot
64+
) as Snapshot
65+
);
4566
const onUndoEvent = useEvent(onUndo ?? noop);
4667
const getMutateWithMiddlewaresEvent = useEvent(
4768
getMutateWithMiddlewares ??
@@ -171,9 +192,17 @@ export const useMutationWithMutationMode = <
171192
mode.current === 'optimistic' ||
172193
mode.current === 'undoable'
173194
) {
174-
const [, , , onMutateResult] = args;
195+
const [, , variables, onMutateResult] = args;
175196

176197
// Always refetch after error or success:
198+
getQueryKeysEvent(
199+
{ ...paramsRef.current, ...variables },
200+
{
201+
mutationMode: mode.current,
202+
}
203+
).forEach(queryKey => {
204+
queryClient.invalidateQueries({ queryKey });
205+
});
177206
(onMutateResult as { snapshot: Snapshot }).snapshot.forEach(
178207
([queryKey]) => {
179208
queryClient.invalidateQueries({ queryKey });
@@ -243,10 +272,12 @@ export const useMutationWithMutationMode = <
243272
}
244273

245274
snapshot.current = getSnapshotEvent(
246-
{ ...paramsRef.current, ...callTimeParams },
247-
{
248-
mutationMode: mode.current,
249-
}
275+
getQueryKeysEvent(
276+
{ ...paramsRef.current, ...callTimeParams },
277+
{
278+
mutationMode: mode.current,
279+
}
280+
)
250281
);
251282

252283
if (mode.current === 'pessimistic') {
@@ -390,10 +421,10 @@ export type UseMutationWithMutationModeOptions<
390421
options: OptionsType,
391422
mutationResult: TData['data'] | undefined
392423
) => TData['data'];
393-
getSnapshot: <OptionsType extends { mutationMode: MutationMode }>(
424+
getQueryKeys: <OptionsType extends { mutationMode: MutationMode }>(
394425
params: Partial<TVariables>,
395426
options: OptionsType
396-
) => Snapshot;
427+
) => Array<QueryKey>;
397428
onUndo?: <OptionsType extends { mutationMode: MutationMode }>(
398429
params: Partial<TVariables>,
399430
options: OptionsType

packages/ra-core/src/dataProvider/useUpdate.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import type {
1717
DataProvider,
1818
UpdateResult,
1919
} from '../types';
20-
import {
21-
type Snapshot,
22-
useMutationWithMutationMode,
23-
} from './useMutationWithMutationMode';
20+
import { useMutationWithMutationMode } from './useMutationWithMutationMode';
2421
import { useEvent } from '../util';
2522

2623
/**
@@ -236,20 +233,7 @@ export const useUpdate = <RecordType extends RaRecord = any, ErrorType = Error>(
236233
};
237234
return optimisticResult;
238235
},
239-
getSnapshot: ({ resource, ...params }) => {
240-
/**
241-
* Snapshot the previous values via queryClient.getQueriesData()
242-
*
243-
* The snapshotData ref will contain an array of tuples [query key, associated data]
244-
*
245-
* @example
246-
* [
247-
* [['posts', 'getList'], { data: [{ id: 1, title: 'Hello' }], total: 1 }],
248-
* [['posts', 'getMany'], [{ id: 1, title: 'Hello' }]],
249-
* ]
250-
*
251-
* @see https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientgetqueriesdata
252-
*/
236+
getQueryKeys: ({ resource, ...params }) => {
253237
const queryKeys = [
254238
[
255239
resource,
@@ -261,13 +245,7 @@ export const useUpdate = <RecordType extends RaRecord = any, ErrorType = Error>(
261245
[resource, 'getMany'],
262246
[resource, 'getManyReference'],
263247
];
264-
265-
const snapshot = queryKeys.reduce(
266-
(prev, queryKey) =>
267-
prev.concat(queryClient.getQueriesData({ queryKey })),
268-
[] as Snapshot
269-
);
270-
return snapshot;
248+
return queryKeys;
271249
},
272250
getMutateWithMiddlewares: mutationFn => {
273251
if (getMutateWithMiddlewares) {

packages/ra-core/src/dataProvider/useUpdateMany.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ import type {
1717
DataProvider,
1818
UpdateManyResult,
1919
} from '../types';
20-
import {
21-
type Snapshot,
22-
useMutationWithMutationMode,
23-
} from './useMutationWithMutationMode';
20+
import { useMutationWithMutationMode } from './useMutationWithMutationMode';
2421
import { useEvent } from '../util';
2522

2623
/**
@@ -223,34 +220,15 @@ export const useUpdateMany = <
223220

224221
return params?.ids;
225222
},
226-
getSnapshot: ({ resource }) => {
227-
/**
228-
* Snapshot the previous values via queryClient.getQueriesData()
229-
*
230-
* The snapshotData ref will contain an array of tuples [query key, associated data]
231-
*
232-
* @example
233-
* [
234-
* [['posts', 'getList'], { data: [{ id: 1, title: 'Hello' }], total: 1 }],
235-
* [['posts', 'getMany'], [{ id: 1, title: 'Hello' }]],
236-
* ]
237-
*
238-
* @see https://tanstack.com/query/v5/docs/react/reference/QueryClient#queryclientgetqueriesdata
239-
*/
223+
getQueryKeys: ({ resource }) => {
240224
const queryKeys = [
241225
[resource, 'getOne'],
242226
[resource, 'getList'],
243227
[resource, 'getInfiniteList'],
244228
[resource, 'getMany'],
245229
[resource, 'getManyReference'],
246230
];
247-
248-
const snapshot = queryKeys.reduce(
249-
(prev, queryKey) =>
250-
prev.concat(queryClient.getQueriesData({ queryKey })),
251-
[] as Snapshot
252-
);
253-
return snapshot;
231+
return queryKeys;
254232
},
255233
getMutateWithMiddlewares: mutationFn => {
256234
if (getMutateWithMiddlewares) {

0 commit comments

Comments
 (0)