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

DataViews Pagination: improve pagination logic to handle empty pages #68709

Closed
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
3 changes: 2 additions & 1 deletion packages/dataviews/src/components/dataviews-footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export default function DataViewsFooter() {
const hasBulkActions =
useSomeItemHasAPossibleBulkAction( actions, data ) &&
[ LAYOUT_TABLE, LAYOUT_GRID ].includes( view.type );
const currentPage = view.page ?? 1;

if (
! totalItems ||
! totalPages ||
( totalPages <= 1 && ! hasBulkActions )
( totalPages <= 1 && currentPage <= totalPages && ! hasBulkActions )
) {
return null;
}
Expand Down
21 changes: 19 additions & 2 deletions packages/dataviews/src/components/dataviews-pagination/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {
__experimentalHStack as HStack,
SelectControl,
} from '@wordpress/components';
import { createInterpolateElement, memo, useContext } from '@wordpress/element';
import {
createInterpolateElement,
memo,
useContext,
useEffect,
} from '@wordpress/element';
import { sprintf, __, _x, isRTL } from '@wordpress/i18n';
import { next, previous } from '@wordpress/icons';

Expand All @@ -20,13 +25,25 @@ function DataViewsPagination() {
view,
onChangeView,
paginationInfo: { totalItems = 0, totalPages },
data,
} = useContext( DataViewsContext );

const currentPage = view.page ?? 1;

// If the current page is not the first page, and there are no items on the current page, go to the previous page.
useEffect( () => {
if ( currentPage !== 1 && ! data.length ) {
onChangeView( {
...view,
page: currentPage - 1,
} );
}
}, [ data.length, currentPage, view, onChangeView ] );
Comment on lines +33 to +41
Copy link
Member

Choose a reason for hiding this comment

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

This effect isn't reliable. What happens when you're not on the first page and data is fetching? The condition will be true, and the user will be redirected to the previous page.

P.S. I know the current page state doesn't persist in the URL, but that will change.


if ( ! totalItems || ! totalPages ) {
return null;
}

const currentPage = view.page ?? 1;
const pageSelectOptions = Array.from( Array( totalPages ) ).map(
( _, i ) => {
const page = i + 1;
Expand Down
Loading