Skip to content

Commit

Permalink
append the offset to the query string of the page URL when paginati…
Browse files Browse the repository at this point in the history
…ng (#811)

Append the offset to the query string of the page URL.
  • Loading branch information
Kiran K authored Jan 5, 2023
1 parent 617b919 commit 36ac681
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/ui/hooks/usePaginate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import { useState } from 'react';
import { useState, useEffect } from 'react';
import { useRouter } from 'next/router';

const usePaginate = () => {
const [paginate, setPaginate] = useState({ offset: 0 });
const router = useRouter();

const offset = router.query.offset ? Number(router.query.offset) : 0;

const [paginate, setPaginate] = useState({ offset });

useEffect(() => {
// Prevent pushing the same URL to the history
if (offset === paginate.offset) {
return;
}

const path = router.asPath.split('?')[0];

router.push(`${path}?offset=${paginate.offset}`, undefined, { shallow: true });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [paginate]);

useEffect(() => {
setPaginate({ offset });
}, [offset]);

return {
paginate,
Expand Down

0 comments on commit 36ac681

Please sign in to comment.