Skip to content
Snippets Groups Projects
Unverified Commit 1ab2eeb7 authored by Emmad Usmani's avatar Emmad Usmani Committed by GitHub
Browse files

fix pagination state shared between collections (#28618)

parent 7b8675fb
No related branches found
No related tags found
No related merge requests found
...@@ -80,7 +80,8 @@ function CollectionContent({ ...@@ -80,7 +80,8 @@ function CollectionContent({
sort_column: "name", sort_column: "name",
sort_direction: "asc", sort_direction: "asc",
}); });
const { handleNextPage, handlePreviousPage, setPage, page } = usePagination(); const { handleNextPage, handlePreviousPage, setPage, page, resetPage } =
usePagination();
const { selected, toggleItem, toggleAll, getIsSelected, clear } = const { selected, toggleItem, toggleAll, getIsSelected, clear } =
useListSelect(itemKeyFn); useListSelect(itemKeyFn);
const previousCollection = usePrevious(collection); const previousCollection = usePrevious(collection);
...@@ -94,8 +95,9 @@ function CollectionContent({ ...@@ -94,8 +95,9 @@ function CollectionContent({
useEffect(() => { useEffect(() => {
if (previousCollection && previousCollection.id !== collection.id) { if (previousCollection && previousCollection.id !== collection.id) {
clear(); clear();
resetPage();
} }
}, [previousCollection, collection, clear]); }, [previousCollection, collection, clear, resetPage]);
useEffect(() => { useEffect(() => {
const shouldBeBookmarked = bookmarks.some( const shouldBeBookmarked = bookmarks.some(
......
...@@ -12,10 +12,16 @@ export const usePagination = (initialPage = 0) => { ...@@ -12,10 +12,16 @@ export const usePagination = (initialPage = 0) => {
[setPage], [setPage],
); );
const resetPage = useCallback(
() => setPage(initialPage),
[setPage, initialPage],
);
return { return {
handleNextPage, handleNextPage,
handlePreviousPage, handlePreviousPage,
setPage, setPage,
page, page,
resetPage,
}; };
}; };
import { renderHook, act } from "@testing-library/react-hooks";
import { usePagination } from "./use-pagination";
describe("usePagination", () => {
it("should set 'page' to 'initialPage' upon calling 'resetPage'", () => {
const initialPage = 3;
const { result } = renderHook(() => usePagination(initialPage));
act(() => result.current.handleNextPage());
expect(result.current.page).toEqual(initialPage + 1);
act(() => result.current.resetPage());
expect(result.current.page).toEqual(initialPage);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment