Skip to content

Commit 31b2780

Browse files
authored
Merge pull request #10975 from jvasseur/fix-update-overriding-meta-property
Fix update removing meta and pageInfo properties from getManyReference result
2 parents c35c773 + bc19c1f commit 31b2780

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,58 @@ describe('useUpdate', () => {
552552
});
553553
});
554554
});
555+
it('updates getManyReference query cache with pageInfo when dataProvider promise resolves', async () => {
556+
const queryClient = new QueryClient();
557+
queryClient.setQueryData(['foo', 'getManyReference'], {
558+
data: [{ id: 1, bar: 'bar' }],
559+
pageInfo: {
560+
hasPreviousPage: false,
561+
hasNextPage: true,
562+
},
563+
});
564+
const dataProvider = {
565+
update: jest.fn(() =>
566+
Promise.resolve({ data: { id: 1, bar: 'baz' } } as any)
567+
),
568+
} as any;
569+
let localUpdate;
570+
const Dummy = () => {
571+
const [update] = useUpdate();
572+
localUpdate = update;
573+
return <span />;
574+
};
575+
render(
576+
<CoreAdminContext
577+
dataProvider={dataProvider}
578+
queryClient={queryClient}
579+
>
580+
<Dummy />
581+
</CoreAdminContext>
582+
);
583+
localUpdate('foo', {
584+
id: 1,
585+
data: { bar: 'baz' },
586+
previousData: { id: 1, bar: 'bar' },
587+
});
588+
await waitFor(() => {
589+
expect(dataProvider.update).toHaveBeenCalledWith('foo', {
590+
id: 1,
591+
data: { bar: 'baz' },
592+
previousData: { id: 1, bar: 'bar' },
593+
});
594+
});
595+
await waitFor(() => {
596+
expect(
597+
queryClient.getQueryData(['foo', 'getManyReference'])
598+
).toEqual({
599+
data: [{ id: 1, bar: 'baz' }],
600+
pageInfo: {
601+
hasPreviousPage: false,
602+
hasNextPage: true,
603+
},
604+
});
605+
});
606+
});
555607
it('updates getInfiniteList query cache when dataProvider promise resolves', async () => {
556608
const queryClient = new QueryClient();
557609
queryClient.setQueryData(['foo', 'getInfiniteList'], {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ export const useUpdate = <RecordType extends RaRecord = any, ErrorType = Error>(
193193
queryClient.setQueriesData(
194194
{ queryKey: [resource, 'getManyReference'] },
195195
(res: GetListResult) =>
196-
res && res.data
197-
? { data: updateColl(res.data), total: res.total }
198-
: res,
196+
res && res.data ? { ...res, data: updateColl(res.data) } : res,
199197
{ updatedAt }
200198
);
201199
};

0 commit comments

Comments
 (0)