forked from mongodb/docs-sample-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagination.tsx
More file actions
81 lines (72 loc) · 2.11 KB
/
Pagination.tsx
File metadata and controls
81 lines (72 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use client';
import Link from 'next/link';
import { useSearchParams } from 'next/navigation';
import paginationStyles from './Pagination.module.css';
interface PaginationProps {
currentPage: number;
hasNextPage: boolean;
hasPrevPage: boolean;
limit: number;
}
export default function Pagination({
currentPage,
hasNextPage,
hasPrevPage,
limit
}: PaginationProps) {
const searchParams = useSearchParams();
const createPageURL = (page: number) => {
const params = new URLSearchParams(searchParams);
params.set('page', page.toString());
if (limit !== 20) { // Only include limit if it's not the default
params.set('limit', limit.toString());
}
return `/movies?${params.toString()}`;
};
// Don't show pagination if there's no navigation possible
if (!hasNextPage && !hasPrevPage) {
return null;
}
return (
<nav className={paginationStyles.pagination} aria-label="Movies pagination">
<div className={paginationStyles.paginationContainer}>
{/* Previous Button */}
{hasPrevPage ? (
<Link
href={createPageURL(currentPage - 1)}
className={paginationStyles.pageButton}
aria-label="Go to previous page"
>
← Previous
</Link>
) : (
<span className={`${paginationStyles.pageButton} ${paginationStyles.disabled}`}>
← Previous
</span>
)}
{/* Current Page Info */}
<div className={paginationStyles.pageInfo}>
Page {currentPage}
</div>
{/* Next Button */}
{hasNextPage ? (
<Link
href={createPageURL(currentPage + 1)}
className={paginationStyles.pageButton}
aria-label="Go to next page"
>
Next →
</Link>
) : (
<span className={`${paginationStyles.pageButton} ${paginationStyles.disabled}`}>
Next →
</span>
)}
</div>
{/* Additional Info */}
<div className={paginationStyles.additionalInfo}>
{limit} movies per page
</div>
</nav>
);
}