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

compound order by #510

Merged
merged 2 commits into from
Mar 6, 2024
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
41 changes: 41 additions & 0 deletions src/hydra/dataProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,4 +603,45 @@ describe('Transform a React Admin request to an Hydra request', () => {
'http://localhost/entrypoint/comments?order%5Bid%5D=ASC&page=1&itemsPerPage=30',
);
});

test('React Admin get list with compound order', async () => {
mockFetchHydra.mockClear();
mockFetchHydra.mockReturnValueOnce(
Promise.resolve({
status: 200,
headers: new Headers(),
json: {
'hydra:member': [
{ '@id': '/comments/423' },
{ '@id': '/comments/976' },
],
'hydra:totalItems': 2,
'hydra:view': {
'@id': '/comments?page=1',
'@type': 'hydra:PartialCollectionView',
'hydra:first': '/comments?page=1',
'hydra:last': '/comments?page=1',
'hydra:next': '/comments?page=1',
},
},
}),
);
const result = await dataProvider.current.getList('comments', {
pagination: { page: 1, perPage: 30 },
sort: { field: 'text, id', order: 'DESC' },
filter: false,
});
expect(result).toEqual({
data: [
{ '@id': '/comments/423', id: '/comments/423' },
{ '@id': '/comments/976', id: '/comments/976' },
],
total: 2,
});
const url = mockFetchHydra.mock.calls?.[0]?.[0] ?? new URL('https://foo');
expect(url).toBeInstanceOf(URL);
expect(url.toString()).toEqual(
'http://localhost/entrypoint/comments?order%5Btext%5D=DESC&order%5Bid%5D=DESC&page=1&itemsPerPage=30',
);
});
});
4 changes: 3 additions & 1 deletion src/hydra/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,9 @@ function dataProvider(
} = params as GetListParams | GetManyReferenceParams;

if (order && field) {
url.searchParams.set(`order[${field}]`, order);
field.split(',').forEach((fieldName) => {
url.searchParams.set(`order[${fieldName.trim()}]`, order);
});
}
if (page) url.searchParams.set('page', page.toString());
if (perPage) url.searchParams.set('itemsPerPage', perPage.toString());
Expand Down
Loading