Skip to content

Commit 322cb4c

Browse files
Merge pull request #248 from offbeatjs/patch-1
Replace links with buttons in pagination component
2 parents 3065bee + 0bccc1f commit 322cb4c

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

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

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
'use client';
2+
13
import { Button } from '@/app/(public)/_components/button';
2-
import { ArrowLeft, ArrowRight } from 'lucide-react';
3-
import Link from 'next/link';
4+
import { ArrowLeft, ArrowRight, Loader2 } from 'lucide-react';
5+
import { useRouter } from 'next/navigation';
6+
import { useTransition } from 'react';
47
import type { SearchParams } from '@/types';
58

69
const MAX_PER_PAGE = 21;
@@ -15,24 +18,60 @@ export function Pagination({
1518
totalCount,
1619
searchParams
1720
}: PaginationProps) {
21+
const router = useRouter();
22+
const [isPending, startTransition] = useTransition();
23+
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));
29+
30+
startTransition(() => {
31+
router.push(`?${params.toString()}`);
32+
});
33+
}
34+
1835
return (
1936
<div className="flex flex-col items-center gap-4 my-6 justify-evenly sm:gap-0 sm:flex-row">
2037
{page > 1 && (
21-
<Link href={{ query: { ...searchParams, p: page - 1 } }}>
22-
<Button className="btn-wide hover:bg-primary-btn-hover-gradient hover:text-hacktoberfest-dark-green">
23-
<ArrowLeft />
24-
<span className="ml-2">Previous Page</span>
25-
</Button>
26-
</Link>
38+
<Button
39+
onClick={() => changePage(-1)}
40+
disabled={isPending}
41+
className="btn-wide hover:bg-primary-btn-hover-gradient hover:text-hacktoberfest-dark-green disabled:opacity-50 disabled:cursor-not-allowed"
42+
>
43+
{isPending ? (
44+
<>
45+
<Loader2 className="animate-spin" />
46+
<span className="ml-2">Loading...</span>
47+
</>
48+
) : (
49+
<>
50+
<ArrowLeft />
51+
<span className="ml-2">Previous Page</span>
52+
</>
53+
)}
54+
</Button>
2755
)}
2856
{totalCount >= MAX_PER_PAGE &&
2957
page < Math.ceil(totalCount / MAX_PER_PAGE) && (
30-
<Link href={{ query: { ...searchParams, p: page + 1 } }}>
31-
<Button className="btn-wide hover:bg-primary-btn-hover-gradient hover:text-hacktoberfest-dark-green">
32-
<span className="mr-2">Next Page</span>
33-
<ArrowRight />
34-
</Button>
35-
</Link>
58+
<Button
59+
onClick={() => changePage(1)}
60+
disabled={isPending}
61+
className="btn-wide hover:bg-primary-btn-hover-gradient hover:text-hacktoberfest-dark-green disabled:opacity-50 disabled:cursor-not-allowed"
62+
>
63+
{isPending ? (
64+
<>
65+
<span className="mr-2">Loading...</span>
66+
<Loader2 className="animate-spin" />
67+
</>
68+
) : (
69+
<>
70+
<span className="mr-2">Next Page</span>
71+
<ArrowRight />
72+
</>
73+
)}
74+
</Button>
3675
)}
3776
</div>
3877
);

0 commit comments

Comments
 (0)