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 unexpected List Page Change #5263

Merged
merged 1 commit into from
Sep 18, 2020
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
8 changes: 5 additions & 3 deletions examples/demo/src/reviews/BulkAcceptButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import * as React from 'react';
import { FC } from 'react';
import PropTypes from 'prop-types';
import ThumbUp from '@material-ui/icons/ThumbUp';

import {
Button,
useUpdateMany,
useNotify,
useRedirect,
useRefresh,
useUnselectAll,
CRUD_UPDATE_MANY,
} from 'react-admin';

import { BulkActionProps } from '../types';

const BulkAcceptButton: FC<BulkActionProps> = ({ selectedIds }) => {
const notify = useNotify();
const redirectTo = useRedirect();
const refresh = useRefresh();
const unselectAll = useUnselectAll('reviews');

const [approve, { loading }] = useUpdateMany(
Expand All @@ -31,7 +33,7 @@ const BulkAcceptButton: FC<BulkActionProps> = ({ selectedIds }) => {
{},
true
);
redirectTo('/reviews');
refresh();
unselectAll();
},
onFailure: () => {
Expand Down
8 changes: 5 additions & 3 deletions examples/demo/src/reviews/BulkRejectButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import * as React from 'react';
import { FC } from 'react';
import PropTypes from 'prop-types';
import ThumbDown from '@material-ui/icons/ThumbDown';

import {
Button,
useUpdateMany,
useNotify,
useRedirect,
useRefresh,
useUnselectAll,
CRUD_UPDATE_MANY,
} from 'react-admin';

import { BulkActionProps } from '../types';

const BulkRejectButton: FC<BulkActionProps> = ({ selectedIds }) => {
const notify = useNotify();
const redirectTo = useRedirect();
const refresh = useRefresh();
const unselectAll = useUnselectAll('reviews');

const [reject, { loading }] = useUpdateMany(
Expand All @@ -31,7 +33,7 @@ const BulkRejectButton: FC<BulkActionProps> = ({ selectedIds }) => {
{},
true
);
redirectTo('/reviews');
refresh();
unselectAll();
},
onFailure: () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/ra-core/src/controller/useListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,16 @@ const useListController = <RecordType = Record>(
get(state.admin.resources, [resource, 'list', 'total'], 0)
);

// Since the total can be empty during the loading phase
// We need to override that total with the latest loaded one
// This way, the useEffect bellow won't reset the page to 1
const finalTotal = typeof total === 'undefined' ? defaultTotal : total;

const finalIds = typeof total === 'undefined' ? defaultIds : ids;

const totalPages = useMemo(() => {
return Math.ceil(total / query.perPage) || 1;
}, [query.perPage, total]);
return Math.ceil(finalTotal / query.perPage) || 1;
}, [query.perPage, finalTotal]);

useEffect(() => {
if (
Expand Down