Skip to content

Commit edc8d09

Browse files
authored
Refactor pagination component to use useTransition
refactor `pagination.tsx` by: - adding a cleaner delta logic suggested by @max-programming - making sure the state resets after page is loaded
1 parent e4dbddc commit edc8d09

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

src/app/(public)/repos/_components/pagination.tsx

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Button } from '@/app/(public)/_components/button';
44
import { ArrowLeft, ArrowRight, Loader2 } from 'lucide-react';
55
import { useRouter } from 'next/navigation';
6-
import { useState } from 'react';
6+
import { useTransition } from 'react';
77
import type { SearchParams } from '@/types';
88

99
const MAX_PER_PAGE = 21;
@@ -18,38 +18,29 @@ export function Pagination({
1818
totalCount,
1919
searchParams
2020
}: PaginationProps) {
21-
const [isLoading, setIsLoading] = useState(false);
2221
const router = useRouter();
22+
const [isPending, startTransition] = useTransition();
2323

24-
const handlePrevPage = (e: React.MouseEvent<HTMLButtonElement>) => {
25-
e.preventDefault();
26-
setIsLoading(true);
27-
const newParams = { ...searchParams, p: page - 1 };
28-
const queryString = new URLSearchParams(
29-
Object.entries(newParams).map(([k, v]) => [k, String(v)])
30-
).toString();
31-
router.push(`?${queryString}`);
32-
};
24+
function changePage(delta: number) {
25+
const params = new URLSearchParams(
26+
Object.entries(searchParams).map(([k, v]) => [k, String(v)])
27+
);
28+
params.set('p', String(page + delta));
3329

34-
const handleNextPage = (e: React.MouseEvent<HTMLButtonElement>) => {
35-
e.preventDefault();
36-
setIsLoading(true);
37-
const newParams = { ...searchParams, p: page + 1 };
38-
const queryString = new URLSearchParams(
39-
Object.entries(newParams).map(([k, v]) => [k, String(v)])
40-
).toString();
41-
router.push(`?${queryString}`);
42-
};
30+
startTransition(() => {
31+
router.push(`?${params.toString()}`);
32+
});
33+
}
4334

4435
return (
4536
<div className="flex flex-col items-center gap-4 my-6 justify-evenly sm:gap-0 sm:flex-row">
4637
{page > 1 && (
4738
<Button
48-
onClick={handlePrevPage}
49-
disabled={isLoading}
39+
onClick={() => changePage(-1)}
40+
disabled={isPending}
5041
className="btn-wide hover:bg-primary-btn-hover-gradient hover:text-hacktoberfest-dark-green disabled:opacity-50 disabled:cursor-not-allowed"
5142
>
52-
{isLoading ? (
43+
{isPending ? (
5344
<>
5445
<Loader2 className="animate-spin" />
5546
<span className="ml-2">Loading...</span>
@@ -65,11 +56,11 @@ export function Pagination({
6556
{totalCount >= MAX_PER_PAGE &&
6657
page < Math.ceil(totalCount / MAX_PER_PAGE) && (
6758
<Button
68-
onClick={handleNextPage}
69-
disabled={isLoading}
59+
onClick={() => changePage(1)}
60+
disabled={isPending}
7061
className="btn-wide hover:bg-primary-btn-hover-gradient hover:text-hacktoberfest-dark-green disabled:opacity-50 disabled:cursor-not-allowed"
7162
>
72-
{isLoading ? (
63+
{isPending ? (
7364
<>
7465
<span className="mr-2">Loading...</span>
7566
<Loader2 className="animate-spin" />

0 commit comments

Comments
 (0)