1+ import useViewport from "@/hooks/useViewport" ;
12import Image from "next/image" ;
2- import Link from "next/link" ;
33import { useRouter } from "next/router" ;
44import { useEffect , useState } from "react" ;
55
@@ -8,32 +8,36 @@ interface PaginationProps {
88}
99
1010const Pagination : React . FC < PaginationProps > = ( { totalCount } ) => {
11- console . log ( totalCount ) ;
1211 const router = useRouter ( ) ;
1312 const LiStyle = "relative w-12 h-12 rounded-lg bg-gray900" ;
14- const buttonStyle = "flex justify-center items-center h-full text-black400" ;
13+ const buttonStyle =
14+ "flex justify-center items-center w-full h-full text-black400" ;
1515 const { page, pageSize } = router . query ;
1616
1717 const currentPage = Number ( page ) || 1 ;
1818 const currentPageSize = Number ( pageSize ) || 6 ;
1919 const totalPages = Math . ceil ( totalCount / currentPageSize ) ;
2020 const [ maxPagesToShow , setMaxPagesToShow ] = useState ( 2 ) ;
21+ const { isPC } = useViewport ( ) ;
2122
22- // 화면 크기 변화에 따라 pageSize와 maxPagesToShow를 설정
23- useEffect ( ( ) => {
24- const handleResize = ( ) => {
25- const width = window . innerWidth ;
26- setMaxPagesToShow ( width > 1024 ? 5 : 3 ) ;
27- } ;
28-
29- // 초기 설정 및 리사이즈 이벤트 리스너 추가
30- handleResize ( ) ;
31- window . addEventListener ( "resize" , handleResize ) ;
23+ const handlePageChange = ( newPage : number ) => {
24+ if ( newPage !== currentPage ) {
25+ router . push (
26+ `/link?page=${ newPage } &pageSize=${ currentPageSize } ` ,
27+ undefined ,
28+ { shallow : true }
29+ ) ;
30+ }
31+ } ;
3232
33- return ( ) => {
34- window . removeEventListener ( "resize" , handleResize ) ;
35- } ;
36- } , [ ] ) ;
33+ // 화면 크기 변화에 따라 maxPagesToShow를 설정
34+ useEffect ( ( ) => {
35+ if ( isPC ) {
36+ setMaxPagesToShow ( 5 ) ;
37+ } else {
38+ setMaxPagesToShow ( 3 ) ;
39+ }
40+ } , [ isPC ] ) ;
3741
3842 // 페이지 리스트 생성 함수
3943 const getPageNumbers = ( ) => {
@@ -60,9 +64,10 @@ const Pagination: React.FC<PaginationProps> = ({ totalCount }) => {
6064 return (
6165 < ul className = "flex justify-center gap-[10px] my-10" >
6266 < li className = { LiStyle } >
63- < Link
64- href = { `/link?page= ${ currentPage - 1 } &pageSize= ${ currentPageSize } ` }
67+ < button
68+ onClick = { ( ) => handlePageChange ( currentPage - 1 ) }
6569 className = { `${ buttonStyle } ${ currentPage > 1 ? "text-black500" : "pointer-events-none" } ` }
70+ disabled = { currentPage <= 1 }
6671 >
6772 < Image
6873 src = {
@@ -74,19 +79,19 @@ const Pagination: React.FC<PaginationProps> = ({ totalCount }) => {
7479 width = { 24 }
7580 alt = "prev"
7681 />
77- </ Link >
82+ </ button >
7883 </ li >
7984
8085 { /* 페이지 번호와 생략 표시 */ }
8186 { getPageNumbers ( ) . map ( ( pageNum , index ) =>
8287 typeof pageNum === "number" ? (
8388 < li key = { index } className = { LiStyle } >
84- < Link
85- href = { `/link?page= ${ pageNum } &pageSize= ${ currentPageSize } ` }
89+ < button
90+ onClick = { ( ) => handlePageChange ( pageNum ) }
8691 className = { `${ buttonStyle } ${ pageNum === currentPage ? "text-black500" : "text-black400" } ` }
8792 >
8893 { pageNum }
89- </ Link >
94+ </ button >
9095 </ li >
9196 ) : (
9297 < li
@@ -99,9 +104,10 @@ const Pagination: React.FC<PaginationProps> = ({ totalCount }) => {
99104 ) }
100105
101106 < li className = { LiStyle } >
102- < Link
103- href = { `/link?page= ${ currentPage + 1 } &pageSize= ${ currentPageSize } ` }
107+ < button
108+ onClick = { ( ) => handlePageChange ( currentPage + 1 ) }
104109 className = { `${ buttonStyle } ${ currentPage < totalPages ? "text-black500" : "pointer-events-none" } ` }
110+ disabled = { currentPage >= totalPages }
105111 >
106112 < Image
107113 src = {
@@ -113,7 +119,7 @@ const Pagination: React.FC<PaginationProps> = ({ totalCount }) => {
113119 height = { 24 }
114120 alt = "next"
115121 />
116- </ Link >
122+ </ button >
117123 </ li >
118124 </ ul >
119125 ) ;
0 commit comments