File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed
Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change 22
33import { useCallback } from "react" ;
44import PaginationButton from "./pagination-button" ;
5+ import { useRouter , useSearchParams } from "next/navigation" ;
56
67interface PaginationProps {
78 searchParams : Record < string , string > ;
@@ -18,10 +19,11 @@ export default function Pagination({
1819 itemsPerPage,
1920 parameterName = "p" ,
2021 basePath = "" ,
21- currentPage = 1 ,
22+ currentPage,
2223} : PaginationProps ) {
2324 const totalPages = Math . ceil ( ( totalItems || 0 ) / itemsPerPage ) ;
2425 const pageNumber = currentPage || Number ( searchParams [ parameterName ] ) || 1 ;
26+ const { push } = useRouter ( ) ;
2527
2628 const getPageHref = useCallback (
2729 ( page : number ) => {
@@ -59,6 +61,11 @@ export default function Pagination({
5961
6062 if ( totalPages <= 1 ) return null ;
6163
64+ if ( pageNumber > totalPages ) {
65+ // Redirect to last page if the current page is greater than the total number of pages
66+ push ( getPageHref ( totalPages ) ) ;
67+ }
68+
6269 return (
6370 < div className = "flex flex-col sm:flex-row gap-4 items-center justify-between w-full mt-4" >
6471 < div className = "flex items-center justify-start gap-2" >
You can’t perform that action at this time.
0 commit comments