Skip to content

Commit

Permalink
Fix unexpected list page change
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemangeon committed Sep 17, 2020
1 parent 5379985 commit 09013a2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
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

0 comments on commit 09013a2

Please sign in to comment.