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 List page display on dataProvider error #8319

Merged
merged 7 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 24 additions & 0 deletions packages/ra-ui-materialui/src/list/List.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,28 @@ describe('<List />', () => {
).toHaveLength(1);
});
});

it('should render a list page with an error message when there is an error', async () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
const Datagrid = () => <div>datagrid</div>;
const dataProvider = {
getList: jest.fn(() =>
Promise.reject({ error: { key: 'error.unknown' } })
),
} as any;
render(
<CoreAdminContext dataProvider={dataProvider}>
<ThemeProvider theme={theme}>
<List resource="posts">
<Datagrid />
</List>
</ThemeProvider>
</CoreAdminContext>
);
await waitFor(() => {
expect(
screen.getByText('ra.notification.data_provider_error')
).not.toBeNull();
WiXSL marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
12 changes: 6 additions & 6 deletions packages/ra-ui-materialui/src/list/ListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ComponentPropType, useListContext, RaRecord } from 'ra-core';

import { Title, TitlePropType } from '../layout/Title';
import { ListToolbar } from './ListToolbar';
import { Pagination as DefaultPagination } from './pagination';
import { Pagination as DefaultPagination, PaginationLimit } from './pagination';
import { ListActions as DefaultActions } from './ListActions';
import { Empty } from './Empty';

Expand Down Expand Up @@ -50,10 +50,6 @@ export const ListView = <RecordType extends RaRecord = any>(
return null;
}

if (error) {
return null;
}

const renderList = () => (
<div className={ListClasses.main}>
{(filters || actions) && (
Expand All @@ -70,7 +66,11 @@ export const ListView = <RecordType extends RaRecord = any>(
})
: children}
</Content>
{pagination !== false && pagination}
{error ? (
<PaginationLimit message="ra.notification.data_provider_error" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels weird to me to render a component named 'pagination' here, since we don't even know if the data is supposed to be paginated or not.

I understand that the code of the component you want to display is very similar to the code of PaginationLimit, but to me they have different purposes, so they should be two different components (even if the code is duplicated).

Copy link
Contributor Author

@WiXSL WiXSL Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding an Error component:
<Error error={error} resetErrorBoundary={null} />

error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too found of this solution either, cause we don't do it for other dataProvider errors, so why only do it for the list?

To me, the content of the PaginationLimit was enough for our needs (provided we change the message of course), but I just thought we should create a separate component for this use.

However, this Error component makes me realize it could be a welcome addition to add a 'Retry' button, that would call refresh from useRefresh. This could be useful in case it's only a temporary error.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of the Error page. Displaying an empty page with a warning is less user friendly IMHO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright then 😅

) : (
pagination !== false && pagination
)}
</div>
);

Expand Down
25 changes: 15 additions & 10 deletions packages/ra-ui-materialui/src/list/pagination/PaginationLimit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import CardContent from '@mui/material/CardContent';
import Typography from '@mui/material/Typography';
import { useTranslate } from 'ra-core';

export const PaginationLimit = memo(() => {
const translate = useTranslate();
return (
<CardContent>
<Typography variant="body2">
{translate('ra.navigation.no_results')}
</Typography>
</CardContent>
);
});
export const PaginationLimit = memo(
({ message = 'ra.navigation.no_results' }: PaginationLimitProps) => {
const translate = useTranslate();

return (
<CardContent>
<Typography variant="body2">{translate(message)}</Typography>
</CardContent>
);
}
);

export interface PaginationLimitProps {
message?: string;
}