Skip to content

Commit 0f86265

Browse files
committed
Add a warning about different query options and mutation options meta.
1 parent a794229 commit 0f86265

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

packages/ra-core/src/controller/edit/useEditController.spec.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,35 @@ describe('useEditController', () => {
217217
});
218218
});
219219

220+
it('should emit a warning when providing a different meta in query options and mutation options without redirecting', async () => {
221+
const warnFn = jest.spyOn(console, 'warn').mockImplementation(() => {});
222+
223+
const getOne = jest
224+
.fn()
225+
.mockImplementationOnce(() =>
226+
Promise.resolve({ data: { id: 0, title: 'hello' } })
227+
);
228+
const dataProvider = { getOne } as unknown as DataProvider;
229+
230+
render(
231+
<CoreAdminContext dataProvider={dataProvider}>
232+
<EditController
233+
{...defaultProps}
234+
resource="posts"
235+
queryOptions={{ meta: { foo: 'bar' } }}
236+
mutationOptions={{ meta: { foo: 'baz' } }}
237+
redirect={false}
238+
>
239+
{() => <div />}
240+
</EditController>
241+
</CoreAdminContext>
242+
);
243+
244+
expect(warnFn).toHaveBeenCalledWith(
245+
'When not redirecting after editing, query meta and mutation meta should be the same, or you will have data update issues.'
246+
);
247+
});
248+
220249
it('should call the dataProvider.update() function on save', async () => {
221250
const update = jest
222251
.fn()

packages/ra-core/src/controller/edit/useEditController.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,25 @@ export const useEditController = <
8989
);
9090
}
9191
const id = propsId ?? routeId;
92+
9293
const { meta: queryMeta, ...otherQueryOptions } = queryOptions;
9394
const {
9495
meta: mutationMeta,
9596
onSuccess,
9697
onError,
9798
...otherMutationOptions
9899
} = mutationOptions;
100+
101+
if (
102+
(queryMeta || mutationMeta) &&
103+
JSON.stringify(queryMeta) !== JSON.stringify(mutationMeta) &&
104+
redirectTo === false
105+
) {
106+
console.warn(
107+
'When not redirecting after editing, query meta and mutation meta should be the same, or you will have data update issues.'
108+
);
109+
}
110+
99111
const {
100112
registerMutationMiddleware,
101113
getMutateWithMiddlewares,
@@ -302,6 +314,7 @@ export interface EditControllerProps<
302314
redirect?: RedirectionSideEffect;
303315
resource?: string;
304316
transform?: TransformData;
317+
305318
[key: string]: any;
306319
}
307320

@@ -322,6 +335,7 @@ export interface EditControllerLoadingResult<RecordType extends RaRecord = any>
322335
error: null;
323336
isPending: true;
324337
}
338+
325339
export interface EditControllerLoadingErrorResult<
326340
RecordType extends RaRecord = any,
327341
TError = Error,
@@ -330,6 +344,7 @@ export interface EditControllerLoadingErrorResult<
330344
error: TError;
331345
isPending: false;
332346
}
347+
333348
export interface EditControllerRefetchErrorResult<
334349
RecordType extends RaRecord = any,
335350
TError = Error,
@@ -338,6 +353,7 @@ export interface EditControllerRefetchErrorResult<
338353
error: TError;
339354
isPending: false;
340355
}
356+
341357
export interface EditControllerSuccessResult<RecordType extends RaRecord = any>
342358
extends EditControllerBaseResult<RecordType> {
343359
record: RecordType;

0 commit comments

Comments
 (0)