Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix <ReferenceField> sometimes doesn't load references correctly and gets stuck in the loading animation #7708

Merged
merged 2 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 68 additions & 2 deletions packages/ra-core/src/dataProvider/useGetManyAggregate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ describe('useGetManyAggregate', () => {
});

it('should use data from query cache on mount', async () => {
const FecthGetMany = () => {
const FetchGetMany = () => {
useGetManyAggregate('posts', { ids: ['1'] });
return <span>dummy</span>;
};
const hookValue = jest.fn();
const { rerender } = render(
<CoreAdminContext dataProvider={dataProvider}>
<FecthGetMany />
<FetchGetMany />
</CoreAdminContext>
);
await waitFor(() => {
Expand Down Expand Up @@ -302,4 +302,70 @@ describe('useGetManyAggregate', () => {
});
});
});

it('should aggregate multiple calls for the same resource into one even if one of the calls requests all the aggregated ids', async () => {
const firstCallback = jest.fn();
const secondCallback = jest.fn();
const thirdCallback = jest.fn();
const dataProvider = testDataProvider({
getMany: jest.fn().mockResolvedValue({
data: [
{ id: 1, title: 'one' },
{ id: 2, title: 'two' },
{ id: 3, title: 'three' },
],
}),
});
render(
<CoreAdminContext dataProvider={dataProvider}>
<UseGetManyAggregate
resource="posts"
ids={[1]}
callback={firstCallback}
/>
<UseGetManyAggregate
resource="posts"
ids={[1, 2]}
callback={secondCallback}
/>
<UseGetManyAggregate
resource="posts"
ids={[1, 2, 3]}
callback={thirdCallback}
/>
</CoreAdminContext>
);
await waitFor(() => {
expect(dataProvider.getMany).toHaveBeenCalledTimes(1);
expect(dataProvider.getMany).toHaveBeenCalledWith('posts', {
ids: [1, 2, 3],
});
});

await waitFor(() => {
expect(firstCallback).toHaveBeenCalledWith(
expect.objectContaining({
data: [{ id: 1, title: 'one' }],
})
);
});

expect(secondCallback).toHaveBeenCalledWith(
expect.objectContaining({
data: [
{ id: 1, title: 'one' },
{ id: 2, title: 'two' },
],
})
);
expect(thirdCallback).toHaveBeenCalledWith(
expect.objectContaining({
data: [
{ id: 1, title: 'one' },
{ id: 2, title: 'two' },
{ id: 3, title: 'three' },
],
})
);
});
});
45 changes: 34 additions & 11 deletions packages/ra-core/src/dataProvider/useGetManyAggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ const callGetManyQueries = batch((calls: GetManyCallArgs[]) => {
const aggregatedIds = callsForResource
.reduce((acc, { ids }) => union(acc, ids), []) // concat + unique
.filter(v => v != null && v !== ''); // remove null values

const uniqueMeta = callsForResource.reduce(
(acc, { meta }) => meta || acc,
undefined
Expand All @@ -215,12 +216,10 @@ const callGetManyQueries = batch((calls: GetManyCallArgs[]) => {
return;
}

if (
callsForResource.find(
({ ids }) =>
JSON.stringify(ids) === JSON.stringify(aggregatedIds)
)
) {
const callThatHasAllAggregatedIds = callsForResource.find(
({ ids }) => JSON.stringify(ids) === JSON.stringify(aggregatedIds)
);
if (callThatHasAllAggregatedIds) {
// There is only one call (no aggregation), or one of the calls has the same ids as the sum of all calls.
// Either way, we can't trigger a new fetchQuery with the same signature, as it's already pending.
// Therefore, we reply with the dataProvider
Expand All @@ -229,13 +228,31 @@ const callGetManyQueries = batch((calls: GetManyCallArgs[]) => {
resource,
ids,
meta,
resolve,
reject,
} = callsForResource[0];
} = callThatHasAllAggregatedIds;

dataProvider
.getMany<any>(resource, { ids, meta })
.then(({ data }) => data)
.then(resolve, reject);
.then(
data => {
// We must then resolve all the pending calls with the data they requested
callsForResource.forEach(({ ids, resolve }) => {
resolve(
data.filter(record =>
ids
.map(id => String(id))
.includes(String(record.id))
)
);
});
},
error => {
// All pending calls must also receive the error
callsForResource.forEach(({ reject }) => {
reject(error);
});
}
);
return;
}

Expand Down Expand Up @@ -263,7 +280,13 @@ const callGetManyQueries = batch((calls: GetManyCallArgs[]) => {
)
.then(data => {
callsForResource.forEach(({ ids, resolve }) => {
resolve(data.filter(record => ids.includes(record.id)));
resolve(
data.filter(record =>
ids
.map(id => String(id))
.includes(String(record.id))
)
);
});
})
.catch(error =>
Expand Down