1- import React , { useCallback , useEffect , useRef } from 'react' ;
1+ import { useEffect , useState } from 'react' ;
22import * as S from './index.styled' ;
33import Card from './Card' ;
44import { useNavigate } from 'react-router' ;
55import useMediaQueries from '@/hooks/useMediaQueries' ;
66import WhiteContentContainer from '@/layouts/WhiteContentContainer' ;
7- import { useFarmingLogsInfiniteQuery } from '@/services/query/useFarmingLogInfiniteQuery ' ;
7+ import { useFarmingLogQuery } from '@/services/query/useFarmingLogInQuery ' ;
88import useFarmingLogStore from '@/stores/farminglogStore' ;
99import CardSkeleton from './CardSkeleton' ;
1010
11+
12+ import jumpArrow_left from '@/assets/Icons/pagenation_1.png' ;
13+ import jumpArrow_right from '@/assets/Icons/pagenation_1.png' ;
14+ import nextArrow_left from '@/assets/Icons/pagenation_2.png' ;
15+ import nextArrow_right from '@/assets/Icons/pagenation_2.png' ;
16+
1117import EditImage from '@/assets/Icons/edit-3.png' ;
1218
1319export default function View ( ) {
1420 const navigate = useNavigate ( ) ;
15- const { isApp, isMobile, isDesktop } = useMediaQueries ( ) ;
21+ const { isApp, isMobile, isDesktop, isTablet } = useMediaQueries ( ) ;
22+ const [ currentPage , setCurrentPage ] = useState < number > ( 0 ) ;
1623
1724 const {
1825 data,
19- fetchNextPage,
20- hasNextPage,
21- isFetchingNextPage,
22- isFetching,
2326 isLoading,
2427 error,
2528 refetch
26- } = useFarmingLogsInfiniteQuery ( ) ;
29+ } = useFarmingLogQuery ( currentPage , 10 ) ; // 10개씩 페이지네이션
2730 const { isNeedRefresh, setIsNeedRefresh } = useFarmingLogStore ( ) ;
2831
2932 useEffect ( ( ) => {
@@ -33,22 +36,48 @@ export default function View() {
3336 }
3437 } , [ isNeedRefresh , refetch , setIsNeedRefresh ] ) ;
3538
36- // 마지막 카드 요소를 관찰하여 다음 페이지를 불러오기 위한 IntersectionObserver
37- const observerRef = useRef < IntersectionObserver | null > ( null ) ;
38- const lastLogRef = useCallback (
39- ( node : HTMLDivElement ) => {
40- if ( isFetchingNextPage ) return ;
41- if ( observerRef . current ) observerRef . current . disconnect ( ) ;
4239
43- observerRef . current = new IntersectionObserver ( ( entries ) => {
44- if ( entries [ 0 ] . isIntersecting && hasNextPage ) {
45- fetchNextPage ( ) ;
46- }
47- } ) ;
48- if ( node ) observerRef . current . observe ( node ) ;
49- } ,
50- [ isFetchingNextPage , fetchNextPage , hasNextPage ]
51- ) ;
40+ // 페이지 번호 배열 생성
41+ const generatePageNumbers = ( ) => {
42+ if ( ! data ) return [ ] ;
43+
44+ const totalPages = data . totalPages ;
45+ const current = data . number ; // 현재 페이지 번호 (0시작)
46+ const pages : number [ ] = [ ] ;
47+
48+ // 최대 5개의 페이지 번호만 표시
49+ const maxVisiblePages = 5 ;
50+ let startPage = Math . max ( 0 , current - Math . floor ( maxVisiblePages / 2 ) ) ;
51+ const endPage = Math . min ( totalPages - 1 , startPage + maxVisiblePages - 1 ) ;
52+
53+ // 시작 페이지 조정
54+ if ( endPage - startPage < maxVisiblePages - 1 ) {
55+ startPage = Math . max ( 0 , endPage - maxVisiblePages + 1 ) ;
56+ }
57+
58+ for ( let i = startPage ; i <= endPage ; i ++ ) {
59+ pages . push ( i ) ;
60+ }
61+
62+ return pages ;
63+ } ;
64+
65+ // 페이지네이션 핸들러
66+ const handlePageChange = ( page : number ) => {
67+ setCurrentPage ( page ) ;
68+ } ;
69+
70+ const handlePreviousPage = ( ) => {
71+ if ( data && ! data . first ) {
72+ setCurrentPage ( currentPage - 1 ) ;
73+ }
74+ } ;
75+
76+ const handleNextPage = ( ) => {
77+ if ( data && ! data . last ) {
78+ setCurrentPage ( currentPage + 1 ) ;
79+ }
80+ } ;
5281
5382 // 로딩 또는 에러 상태 처리
5483 if ( isLoading ) return (
@@ -80,37 +109,64 @@ export default function View() {
80109 $isMobile = { isMobile }
81110 $isDesktop = { isDesktop }
82111 >
83- { data . pages . map ( ( page , pageIndex ) => (
84- < React . Fragment key = { pageIndex } >
85- { page . content . map ( ( log , idx ) => {
86- // 마지막 페이지의 마지막 요소에 ref 적용
87- const isLastItem =
88- pageIndex === data . pages . length - 1 &&
89- idx === page . content . length - 1 ;
90- return (
91- < div
92- key = { log . farmingLogId }
93- ref = { isLastItem ? lastLogRef : null }
94- >
95- < Card data = { log } />
96- </ div >
97- ) ;
98- } ) }
99- </ React . Fragment >
112+ { data ?. content . map ( ( log ) => (
113+ < div key = { log . farmingLogId } >
114+ < Card data = { log } />
115+ </ div >
100116 ) ) }
117+ { /* 페이지네이션 */ }
118+ { data && data . content . length > 0 && (
119+ < S . PaginationContainer >
120+ < S . PaginationButton >
121+ < S . PaginationButtonText
122+ onClick = { ( ) => setCurrentPage ( 0 ) }
123+ $disabled = { data ?. first }
124+ $isMobile = { isMobile }
125+ $isTablet = { isTablet }
126+ >
127+ < img src = { jumpArrow_left } alt = "jumpArrow" />
128+ </ S . PaginationButtonText >
129+ < S . PaginationButtonText
130+ onClick = { handlePreviousPage }
131+ $disabled = { data ?. first }
132+ $isMobile = { isMobile }
133+ $isTablet = { isTablet }
134+ >
135+ < img src = { nextArrow_left } alt = "nextArrow" />
136+ </ S . PaginationButtonText >
137+
138+ { generatePageNumbers ( ) . map ( ( pageNum ) => (
139+ < S . PaginationPageButton
140+ key = { pageNum }
141+ onClick = { ( ) => handlePageChange ( pageNum ) }
142+ $active = { pageNum === currentPage }
143+ $isMobile = { isMobile }
144+ $isTablet = { isTablet }
145+ >
146+ { pageNum + 1 }
147+ </ S . PaginationPageButton >
148+ ) ) }
149+
150+ < S . PaginationButtonText
151+ onClick = { handleNextPage }
152+ $disabled = { data ?. last }
153+ $isMobile = { isMobile }
154+ $isTablet = { isTablet }
155+ >
156+ < img src = { nextArrow_right } alt = "nextArrow_right" />
157+ </ S . PaginationButtonText >
158+ < S . PaginationButtonText
159+ onClick = { ( ) => setCurrentPage ( data . totalPages - 1 ) }
160+ $disabled = { data ?. last }
161+ $isMobile = { isMobile }
162+ $isTablet = { isTablet }
163+ >
164+ < img src = { jumpArrow_right } alt = "jumpArrow_right" />
165+ </ S . PaginationButtonText >
166+ </ S . PaginationButton >
167+ </ S . PaginationContainer >
168+ ) }
101169 </ S . FarmingLogCardContainer >
102- { isFetchingNextPage && < div > 로딩중...</ div > }
103- { ! hasNextPage && ! isFetching && (
104- < S . EndOfList >
105- < S . EndOfListText
106- $isApp = { isApp }
107- $isMobile = { isMobile }
108- $isDesktop = { isDesktop }
109- >
110- 더 이상 글이 없습니다.
111- </ S . EndOfListText >
112- </ S . EndOfList >
113- ) }
114170 < S . FarmingLogWriteButton
115171 $isApp = { isApp }
116172 $isMobile = { isMobile }
0 commit comments