1+ import { useState , useEffect } from 'react' ;
12import useMediaQueries from '@/hooks/useMediaQueries' ;
23import { useNewsList } from '@/hooks/useNews' ;
34// import Logger from '@/utils/Logger';
45import * as S from './index.styled' ;
56import NewsItem from './NewsItem' ;
67
8+ import jumpArrow_left from '@/assets/Icons/pagenation_1.png' ;
9+ import jumpArrow_right from '@/assets/Icons/pagenation_1.png' ;
10+ import nextArrow_left from '@/assets/Icons/pagenation_2.png' ;
11+ import nextArrow_right from '@/assets/Icons/pagenation_2.png' ;
12+
713
814export default function News ( ) {
15+ const [ currentPage , setCurrentPage ] = useState < number > ( 0 ) ;
16+ const pageSize = 12 ; // 12개씩 페이지네이션
917 const { isMobile } = useMediaQueries ( ) ;
1018 const { data : newsData , loading : newsLoading , error : newsError } = useNewsList ( ) ;
1119 const newsDataSorted = newsData ?. sort ( ( a , b ) => new Date ( b . createdAt ) . getTime ( ) - new Date ( a . createdAt ) . getTime ( ) ) ;
12-
20+ const totalPages = Math . ceil ( newsDataSorted ?. length / pageSize ) ;
21+
22+ const currentNews = newsDataSorted ?. slice (
23+ currentPage * pageSize ,
24+ ( currentPage + 1 ) * pageSize
25+ ) ;
26+
27+ const handlePreviousPage = ( ) => {
28+ if ( currentPage > 0 ) setCurrentPage ( currentPage - 1 ) ;
29+ } ;
30+
31+ const handleNextPage = ( ) => {
32+ if ( currentPage < totalPages - 1 ) setCurrentPage ( currentPage + 1 ) ;
33+ } ;
34+
35+ // 페이지 번호 배열 생성
36+ const generatePageNumbers = ( ) => {
37+ const pages : number [ ] = [ ] ;
38+ const maxVisiblePages = 3 ;
39+ let startPage = Math . max ( 0 , currentPage - Math . floor ( maxVisiblePages / 2 ) ) ;
40+ const endPage = Math . min ( totalPages - 1 , startPage + maxVisiblePages - 1 ) ;
41+
42+ if ( endPage - startPage < maxVisiblePages - 1 ) {
43+ startPage = Math . max ( 0 , endPage - maxVisiblePages + 1 ) ;
44+ }
45+
46+ for ( let i = startPage ; i <= endPage ; i ++ ) {
47+ pages . push ( i ) ;
48+ }
49+
50+ return pages ;
51+ } ;
52+
53+ // 페이지 전환시 스크롤을 맨위로 부드럽게 전환
54+ useEffect ( ( ) => {
55+ window . scrollTo ( { top : 0 , behavior : 'smooth' } ) ;
56+ } , [ currentPage ] ) ;
57+
1358 if ( newsLoading ) {
1459 return (
1560 < S . Container >
@@ -27,23 +72,48 @@ export default function News() {
2772 </ S . DescriptionContainer >
2873 </ S . Container >
2974 ) ;
30- // Logger.error(newsError);
31- // return (
32- // <S.Container>
33- // <S.Message $isMobile={isMobile}>뉴스를 불러오는 중 오류가 발생했습니다.</S.Message>
34- // </S.Container>
35- // );
3675 }
3776
3877 return (
3978 < S . Container >
4079 < S . NewsPageTitle > 소식</ S . NewsPageTitle >
4180 { newsDataSorted && newsDataSorted . length > 0 ? (
42- < S . NewsContainer >
43- { newsDataSorted . map ( ( news , index ) => (
44- < NewsItem key = { index } newsListData = { news } />
45- ) ) }
46- </ S . NewsContainer >
81+ < >
82+ < S . NewsContainer >
83+ { currentNews ?. map ( ( news , index ) => (
84+ < NewsItem key = { index } newsListData = { news } />
85+ ) ) }
86+ </ S . NewsContainer >
87+ { totalPages > 1 && (
88+ < S . PaginationContainer >
89+ < S . PaginationButton >
90+ < S . PaginationButtonText onClick = { ( ) => setCurrentPage ( 0 ) } $disabled = { currentPage === 0 } >
91+ < img src = { jumpArrow_left } alt = "jumpArrow" />
92+ </ S . PaginationButtonText >
93+ < S . PaginationButtonText onClick = { handlePreviousPage } $disabled = { currentPage === 0 } >
94+ < img src = { nextArrow_left } alt = "nextArrow" />
95+ </ S . PaginationButtonText >
96+
97+ { generatePageNumbers ( ) . map ( ( pageNum ) => (
98+ < S . PaginationPageButton
99+ key = { pageNum }
100+ onClick = { ( ) => setCurrentPage ( pageNum ) }
101+ $active = { pageNum === currentPage }
102+ >
103+ { pageNum + 1 }
104+ </ S . PaginationPageButton >
105+ ) ) }
106+
107+ < S . PaginationButtonText onClick = { handleNextPage } $disabled = { currentPage >= totalPages - 1 } >
108+ < img src = { nextArrow_right } alt = "nextArrow_right" />
109+ </ S . PaginationButtonText >
110+ < S . PaginationButtonText onClick = { ( ) => setCurrentPage ( totalPages - 1 ) } $disabled = { currentPage >= totalPages - 1 } >
111+ < img src = { jumpArrow_right } alt = "jumpArrow_right" />
112+ </ S . PaginationButtonText >
113+ </ S . PaginationButton >
114+ </ S . PaginationContainer >
115+ ) }
116+ </ >
47117 ) : (
48118 < S . DescriptionContainer >
49119 < S . Message $isMobile = { isMobile } > 아직 등록된 소식이 없어요.</ S . Message >
0 commit comments