@@ -19,13 +19,17 @@ import PlanSection from '@/components/plans/PlanSection';
1919import SearchBar from '@/components/plans/SearchBar' ;
2020import InlineAddPlanForm from '@/components/plans/InlineAddPlanForm' ;
2121import { Target , Calendar , CheckCircle2 } from 'lucide-react' ;
22+ import Pagination from '@/components/common/Pagination' ;
2223
2324const Page = ( ) => {
2425 const [ user , setUser ] = useState < User | null > ( null ) ;
2526 const [ plans , setPlans ] = useState < Plan [ ] > ( [ ] ) ;
2627 const [ isAdding , setIsAdding ] = useState ( false ) ;
2728 const [ isLoading , setIsLoading ] = useState ( true ) ;
2829
30+ const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
31+ const itemsPerPage = 5 ;
32+
2933 // 사용자 인증 상태 리스너 및 초기 플랜 목록 로드
3034 useEffect ( ( ) => {
3135 const unsubscribe = onAuthStateChanged ( auth , async ( currentUser ) => {
@@ -35,9 +39,13 @@ const Page = () => {
3539 try {
3640 const fetchedPlans = await fetchPlans ( currentUser . uid ) ;
3741 setPlans ( fetchedPlans ) ;
42+
43+ setCurrentPage ( 1 ) ;
3844 } catch ( err ) {
3945 console . error ( '플랜 목록 로딩 실패:' , err ) ;
4046 setPlans ( [ ] ) ;
47+
48+ setCurrentPage ( 1 ) ;
4149 }
4250 } else {
4351 setPlans ( [ ] ) ;
@@ -59,6 +67,7 @@ const Page = () => {
5967 // 목록 새로고침
6068 const fetchedPlans = await fetchPlans ( user . uid ) ;
6169 setPlans ( fetchedPlans ) ;
70+ setCurrentPage ( 1 ) ;
6271
6372 setIsAdding ( false ) ; // 폼 닫기
6473 } catch ( err ) {
@@ -89,6 +98,16 @@ const Page = () => {
8998 // 목록 새로고침
9099 const fetchedPlans = await fetchPlans ( user . uid ) ;
91100 setPlans ( fetchedPlans ) ;
101+
102+ // 페이지 조절
103+ if (
104+ currentPage > Math . ceil ( ( plans . length - 1 ) / itemsPerPage ) &&
105+ Math . ceil ( ( plans . length - 1 ) / itemsPerPage ) > 0
106+ ) {
107+ setCurrentPage ( Math . ceil ( ( plans . length - 1 ) / itemsPerPage ) ) ;
108+ } else if ( Math . ceil ( ( plans . length - 1 ) / itemsPerPage ) === 0 ) {
109+ setCurrentPage ( 1 ) ;
110+ }
92111 } catch ( err ) {
93112 console . error ( err ) ;
94113 }
@@ -117,6 +136,19 @@ const Page = () => {
117136 }
118137 } ;
119138
139+ const totalPages = Math . ceil ( plans . length / itemsPerPage ) ;
140+ const startIndex = ( currentPage - 1 ) * itemsPerPage ;
141+ const endIndex = startIndex + itemsPerPage ;
142+ const paginatedPlans = plans . slice ( startIndex , endIndex ) ;
143+
144+ useEffect ( ( ) => {
145+ if ( currentPage > totalPages && totalPages > 0 ) {
146+ setCurrentPage ( totalPages ) ;
147+ } else if ( totalPages === 0 && currentPage !== 1 ) {
148+ setCurrentPage ( 1 ) ;
149+ }
150+ } , [ plans . length , totalPages , currentPage ] ) ;
151+
120152 return (
121153 < div className = "bg-background min-h-screen p-11" >
122154 < PageHeader
@@ -176,13 +208,13 @@ const Page = () => {
176208 { /* 검색 바 */ }
177209 < SearchBar />
178210
179- { isLoading ? (
180- < p > 플랜을 불러오는 중... </ p >
181- ) : (
182- < div >
183- < section className = "space-y-6" >
184- { plans . length > 0 && user ? (
185- plans . map ( ( plan ) => (
211+ < section className = "space-y-6" >
212+ { isLoading ? (
213+ < p > 플랜을 불러오는 중... </ p >
214+ ) : (
215+ < div >
216+ { paginatedPlans . length > 0 && user ? (
217+ paginatedPlans . map ( ( plan ) => (
186218 < PlanSection
187219 key = { plan . id }
188220 userId = { user . uid }
@@ -196,23 +228,32 @@ const Page = () => {
196228 ) : (
197229 < p > 아직 생성된 플랜이 없습니다. 첫 플랜을 추가해보세요!</ p >
198230 ) }
199- </ section >
200-
201- { /* 하단 추가 버튼 or 인라인 폼 */ }
202- < div className = "mt-6" >
203- { isAdding ? (
204- < InlineAddPlanForm
205- onSave = { handleSavePlan }
206- onCancel = { handleCancelAdd }
207- />
208- ) : (
209- < div onClick = { ( ) => setIsAdding ( true ) } >
210- < AddPlanButton />
211- </ div >
212- ) }
213231 </ div >
214- </ div >
232+ ) }
233+ </ section >
234+
235+ { /* 페이지네이션 컴포넌트 */ }
236+ { totalPages > 1 && (
237+ < Pagination
238+ totalPages = { totalPages }
239+ currentPage = { currentPage }
240+ onPageChange = { setCurrentPage }
241+ />
215242 ) }
243+
244+ { /* 하단 추가 버튼 or 인라인 폼 */ }
245+ < div className = "mt-6" >
246+ { isAdding ? (
247+ < InlineAddPlanForm
248+ onSave = { handleSavePlan }
249+ onCancel = { handleCancelAdd }
250+ />
251+ ) : (
252+ < div onClick = { ( ) => setIsAdding ( true ) } >
253+ < AddPlanButton />
254+ </ div >
255+ ) }
256+ </ div >
216257 </ div >
217258 ) ;
218259} ;
0 commit comments