11import Image from "next/image" ;
22import Link from "next/link" ;
3+ import { useRouter } from "next/router" ;
34import { useEffect , useState } from "react" ;
45
56interface PaginationProps {
6- page : number ;
7- pageSize : number ;
87 totalCount : number ;
98}
109
11- const Pagination : React . FC < PaginationProps > = ( {
12- page,
13- pageSize,
14- totalCount,
15- } ) => {
10+ const Pagination : React . FC < PaginationProps > = ( { totalCount } ) => {
11+ const router = useRouter ( ) ;
1612 const LiStyle = "relative w-12 h-12 rounded-lg bg-gray900" ;
1713 const buttonStyle = "flex justify-center items-center h-full text-black400" ;
14+ const { page, pageSize } = router . query ;
1815
19- const totalPages = Math . ceil ( totalCount / pageSize ) ;
16+ const currentPage = Number ( page ) ;
17+ const currentPageSize = Number ( pageSize ) ;
18+ const totalPages = Math . ceil ( totalCount / currentPageSize ) ;
2019 const [ maxPagesToShow , setMaxPagesToShow ] = useState ( 2 ) ;
2120
2221 // 화면 크기 변화에 따라 pageSize와 maxPagesToShow를 설정
@@ -45,12 +44,12 @@ const Pagination: React.FC<PaginationProps> = ({
4544 } else {
4645 // 첫 페이지와 마지막 페이지는 항상 표시
4746 pages . push ( 1 ) ;
48- let start = Math . max ( 2 , page - 1 ) ;
49- let end = Math . min ( totalPages - 1 , page + 1 ) ;
47+ let start = Math . max ( 2 , currentPage - 1 ) ;
48+ let end = Math . min ( totalPages - 1 , currentPage + 1 ) ;
5049
51- if ( page > 3 ) pages . push ( "..." ) ;
50+ if ( currentPage > 3 ) pages . push ( "..." ) ;
5251 for ( let i = start ; i <= end ; i ++ ) pages . push ( i ) ;
53- if ( page < totalPages - 2 ) pages . push ( "..." ) ;
52+ if ( currentPage < totalPages - 2 ) pages . push ( "..." ) ;
5453 pages . push ( totalPages ) ;
5554 }
5655
@@ -61,12 +60,12 @@ const Pagination: React.FC<PaginationProps> = ({
6160 < ul className = "flex justify-center gap-[10px] my-10" >
6261 < li className = { LiStyle } >
6362 < Link
64- href = { `/link?page=${ page - 1 } &pageSize=${ pageSize } ` }
65- className = { `${ buttonStyle } ${ page > 1 ? "text-black500" : "pointer-events-none" } ` }
63+ href = { `/link?page=${ currentPage - 1 } &pageSize=${ currentPageSize } ` }
64+ className = { `${ buttonStyle } ${ currentPage > 1 ? "text-black500" : "pointer-events-none" } ` }
6665 >
6766 < Image
6867 src = {
69- page > 1
68+ currentPage > 1
7069 ? "/icons/pagination-left-active.png"
7170 : "/icons/pagination-left.png"
7271 }
@@ -83,7 +82,7 @@ const Pagination: React.FC<PaginationProps> = ({
8382 < li key = { index } className = { LiStyle } >
8483 < Link
8584 href = { `/link?page=${ pageNum } &pageSize=${ pageSize } ` }
86- className = { `${ buttonStyle } ${ pageNum === page ? "text-black500" : "text-black400" } ` }
85+ className = { `${ buttonStyle } ${ pageNum === currentPage ? "text-black500" : "text-black400" } ` }
8786 >
8887 { pageNum }
8988 </ Link >
@@ -100,12 +99,12 @@ const Pagination: React.FC<PaginationProps> = ({
10099
101100 < li className = { LiStyle } >
102101 < Link
103- href = { `/link?page=${ page + 1 } &pageSize=${ pageSize } ` }
104- className = { `${ buttonStyle } ${ page < totalPages ? "text-black500" : "pointer-events-none" } ` }
102+ href = { `/link?page=${ currentPage + 1 } &pageSize=${ pageSize } ` }
103+ className = { `${ buttonStyle } ${ currentPage < totalPages ? "text-black500" : "pointer-events-none" } ` }
105104 >
106105 < Image
107106 src = {
108- page < totalPages
107+ currentPage < totalPages
109108 ? "/icons/pagination-right-active.png"
110109 : "/icons/pagination-right.png"
111110 }
0 commit comments