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 canGoBackwards #1409

Merged
merged 1 commit into from
Dec 18, 2023
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
2 changes: 1 addition & 1 deletion ui/shared/pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Pagination = ({ page, onNextPageClick, onPrevPageClick, resetPage, hasPage
aria-label="Prev page"
w="36px"
icon={ <Icon as={ arrowIcon } w={ 5 } h={ 5 }/> }
isDisabled={ !canGoBackwards || page === 1 || isLoading }
isDisabled={ !canGoBackwards || isLoading }
/>
</Skeleton>
<Skeleton isLoaded={ !showSkeleton } display="inline-block" borderRadius="base">
Expand Down
14 changes: 7 additions & 7 deletions ui/shared/pagination/useQueryWithPages.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ it('returns correct data if there is only one page', async() => {
expect(result.current.data).toEqual(responses.page_empty);
expect(result.current.pagination).toMatchObject({
page: 1,
canGoBackwards: true,
canGoBackwards: false,
hasNextPage: false,
isLoading: false,
isVisible: false,
Expand All @@ -91,7 +91,7 @@ describe('if there are multiple pages', () => {
expect(result.current.data).toEqual(responses.page_1);
expect(result.current.pagination).toMatchObject({
page: 1,
canGoBackwards: true,
canGoBackwards: false,
hasNextPage: true,
isLoading: false,
isVisible: true,
Expand Down Expand Up @@ -258,7 +258,7 @@ describe('if there are multiple pages', () => {
expect(result.current.data).toEqual(responses.page_1);
expect(result.current.pagination).toMatchObject({
page: 1,
canGoBackwards: true,
canGoBackwards: false,
hasNextPage: true,
isLoading: false,
isVisible: true,
Expand Down Expand Up @@ -310,7 +310,7 @@ describe('if there are multiple pages', () => {
expect(result.current.data).toEqual(responses.page_1);
expect(result.current.pagination).toMatchObject({
page: 1,
canGoBackwards: true,
canGoBackwards: false,
hasNextPage: true,
isLoading: false,
isVisible: true,
Expand Down Expand Up @@ -404,7 +404,7 @@ describe('if there is page query param in URL', () => {
expect(result.current.data).toEqual(responses.page_3);
expect(result.current.pagination).toMatchObject({
page: 3,
canGoBackwards: false,
canGoBackwards: true,
hasNextPage: false,
isLoading: false,
isVisible: true,
Expand Down Expand Up @@ -458,7 +458,7 @@ describe('queries with filters', () => {
expect(result.current.data).toEqual(responses.page_filtered);
expect(result.current.pagination).toMatchObject({
page: 1,
canGoBackwards: true,
canGoBackwards: false,
hasNextPage: true,
isLoading: false,
isVisible: true,
Expand Down Expand Up @@ -547,7 +547,7 @@ describe('queries with sorting', () => {
expect(result.current.data).toEqual(responses.page_sorted);
expect(result.current.pagination).toMatchObject({
page: 1,
canGoBackwards: true,
canGoBackwards: false,
hasNextPage: false,
isLoading: false,
isVisible: false,
Expand Down
5 changes: 1 addition & 4 deletions ui/shared/pagination/useQueryWithPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export default function useQueryWithPages<Resource extends PaginatedResources>({
const [ hasPages, setHasPages ] = React.useState(page > 1);

const isMounted = React.useRef(false);
const canGoBackwards = React.useRef(!router.query.page);
const queryParams = { ...pageParams[page], ...filters, ...sorting };

const scrollToTop = useCallback(() => {
Expand Down Expand Up @@ -107,7 +106,6 @@ export default function useQueryWithPages<Resource extends PaginatedResources>({
let nextPageQuery: typeof router.query = { ...router.query };
if (page === 2) {
nextPageQuery = omit(router.query, [ 'next_page_params', 'page' ]);
canGoBackwards.current = true;
} else {
nextPageQuery.next_page_params = encodeURIComponent(JSON.stringify(pageParams[page - 1]));
nextPageQuery.page = String(page - 1);
Expand All @@ -130,7 +128,6 @@ export default function useQueryWithPages<Resource extends PaginatedResources>({
queryClient.removeQueries({ queryKey: [ resourceName ] });
setPage(1);
setPageParams({});
canGoBackwards.current = true;
window.setTimeout(() => {
// FIXME after router is updated we still have inactive queries for previously visited page (e.g third), where we came from
// so have to remove it but with some delay :)
Expand Down Expand Up @@ -193,7 +190,7 @@ export default function useQueryWithPages<Resource extends PaginatedResources>({
resetPage,
hasPages,
hasNextPage,
canGoBackwards: canGoBackwards.current,
canGoBackwards: Boolean(pageParams[page - 1]),
isLoading: queryResult.isPlaceholderData,
isVisible: hasPages || hasNextPage,
};
Expand Down
Loading