|
1 | | -import FormField from '@/components/auth/FormField'; |
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState, useEffect } from 'react'; |
| 4 | +import { User, onAuthStateChanged } from 'firebase/auth'; |
| 5 | +import { auth } from '@/lib/firebase'; |
| 6 | +import { addPlan, deletePlan, fetchPlans, Plan } from '@/lib/planManageService'; |
| 7 | + |
2 | 8 | import PageHeader from '@/components/common/PageHeader'; |
3 | 9 | import Card from '@/components/home/Card'; |
4 | 10 | import AddPlanButton from '@/components/plans/AddPlanButton'; |
5 | 11 | import PlanSection from '@/components/plans/PlanSection'; |
6 | 12 | import SearchBar from '@/components/plans/SearchBar'; |
7 | | - |
| 13 | +import InlineAddPlanForm from '@/components/plans/InlineAddPlanForm'; |
8 | 14 | import { Target, Calendar, CheckCircle2 } from 'lucide-react'; |
9 | 15 |
|
10 | | -const sampleTasks = [ |
11 | | - { |
12 | | - id: 1, |
13 | | - text: 'useState, useEffect 기초', |
14 | | - date: '2025-01-20', |
15 | | - isChecked: true, |
16 | | - }, |
17 | | - { |
18 | | - id: 2, |
19 | | - text: 'useContext, useReducer', |
20 | | - date: '2025-01-22', |
21 | | - isChecked: false, |
22 | | - }, |
23 | | - { |
24 | | - id: 3, |
25 | | - text: 'Custom Hooks 만들기', |
26 | | - date: '2025-01-22', |
27 | | - isChecked: false, |
28 | | - }, |
29 | | -]; |
30 | | - |
31 | 16 | const Page = () => { |
| 17 | + const [user, setUser] = useState<User | null>(null); |
| 18 | + const [plans, setPlans] = useState<Plan[]>([]); |
| 19 | + const [isAdding, setIsAdding] = useState(false); |
| 20 | + const [isLoading, setIsLoading] = useState(true); |
| 21 | + |
| 22 | + // 사용자 인증 상태 리스너 및 초기 플랜 목록 로드 |
| 23 | + useEffect(() => { |
| 24 | + const unsubscribe = onAuthStateChanged(auth, async (currentUser) => { |
| 25 | + setUser(currentUser); |
| 26 | + |
| 27 | + if (currentUser) { |
| 28 | + try { |
| 29 | + const fetchedPlans = await fetchPlans(currentUser.uid); |
| 30 | + setPlans(fetchedPlans); |
| 31 | + } catch (err) { |
| 32 | + console.error('플랜 목록 로딩 실패:', err); |
| 33 | + setPlans([]); |
| 34 | + } |
| 35 | + } else { |
| 36 | + setPlans([]); |
| 37 | + } |
| 38 | + setIsLoading(false); |
| 39 | + }); |
| 40 | + return () => unsubscribe(); // 클린업 |
| 41 | + }, []); |
| 42 | + |
| 43 | + // 플랜 생성(추가) 핸들러 |
| 44 | + const handleSavePlan = async (title: string, description: string) => { |
| 45 | + if (!user) { |
| 46 | + alert('로그인이 필요합니다.'); |
| 47 | + return; |
| 48 | + } |
| 49 | + try { |
| 50 | + await addPlan(user.uid, title, description); |
| 51 | + |
| 52 | + // 목록 새로고침 |
| 53 | + const fetchedPlans = await fetchPlans(user.uid); |
| 54 | + setPlans(fetchedPlans); |
| 55 | + |
| 56 | + setIsAdding(false); // 폼 닫기 |
| 57 | + } catch (err) { |
| 58 | + console.error('플랜 추가 실패:', err); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + // 플랜 추가 취소 핸들러 |
| 63 | + const handleCancelAdd = () => { |
| 64 | + setIsAdding(false); |
| 65 | + }; |
| 66 | + |
| 67 | + // 플랜 삭제 핸들러 |
| 68 | + const handleDeletePlan = async (planId: string, title: string) => { |
| 69 | + if (!user) { |
| 70 | + alert('로그인이 필요합니다.'); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if ( |
| 75 | + confirm( |
| 76 | + `'${title}' 플랜을 정말 삭제하시겠습니까? 포함된 모든 할 일이 삭제됩니다.` |
| 77 | + ) |
| 78 | + ) { |
| 79 | + try { |
| 80 | + await deletePlan(user.uid, planId); |
| 81 | + |
| 82 | + // 목록 새로고침 |
| 83 | + const fetchedPlans = await fetchPlans(user.uid); |
| 84 | + setPlans(fetchedPlans); |
| 85 | + } catch (err) { |
| 86 | + console.error(err); |
| 87 | + } |
| 88 | + } |
| 89 | + }; |
| 90 | + |
32 | 91 | return ( |
33 | 92 | <div className="bg-background min-h-screen p-11"> |
34 | | - {/* 1. 페이지 헤더 */} |
35 | 93 | <PageHeader |
36 | 94 | title="플랜" |
37 | 95 | highlight="관리하기" |
38 | 96 | description="학습 주제를 만들고 세부 과제를 관리하세요" |
39 | 97 | /> |
40 | 98 |
|
41 | | - {/* 2. 상단 통계 카드 (Grid) */} |
| 99 | + {/* 상단 통계 카드 (Grid) */} |
42 | 100 | <div className="mb-4 grid grid-cols-1 gap-3.5 md:grid-cols-3"> |
43 | 101 | <Card className="flex items-center justify-between border-2 border-[#D5DCFB]"> |
44 | 102 | {/* 왼쪽: 텍스트 영역 */} |
45 | 103 | <div className="flex flex-col gap-1"> |
46 | 104 | <span className="text-text-sub text-sm font-medium"> |
47 | 105 | 전체 플랜 수 |
48 | 106 | </span> |
49 | | - <span className="text-4xl font-bold text-[#4757D3]">1</span> |
| 107 | + <span className="text-4xl font-bold text-[#4757D3]"> |
| 108 | + {plans.length} |
| 109 | + </span> |
50 | 110 | </div> |
51 | 111 |
|
52 | 112 | {/* 오른쪽: 아이콘 영역 */} |
@@ -84,23 +144,41 @@ const Page = () => { |
84 | 144 | </Card> |
85 | 145 | </div> |
86 | 146 |
|
87 | | - {/* 3. 검색 바 */} |
| 147 | + {/* 검색 바 */} |
88 | 148 | <SearchBar /> |
89 | 149 |
|
90 | | - {/* 4. 메인 플랜 목록 */} |
| 150 | + {/* 메인 플랜 목록 */} |
91 | 151 | <section className="space-y-6"> |
92 | | - <PlanSection |
93 | | - title="React Hooks 학습" |
94 | | - description="React Hooks의 기본부터 고급 패턴까지 학습" |
95 | | - tasks={sampleTasks} |
96 | | - /> |
97 | | - |
98 | | - {/* 추가적인 PlanSection이 있다면 여기에 배치 */} |
| 152 | + {isLoading ? ( |
| 153 | + <p>플랜을 불러오는 중...</p> |
| 154 | + ) : plans.length > 0 && user ? ( |
| 155 | + plans.map((plan) => ( |
| 156 | + <PlanSection |
| 157 | + key={plan.id} |
| 158 | + userId={user.uid} |
| 159 | + planId={plan.id} |
| 160 | + title={plan.title} |
| 161 | + description={plan.description} |
| 162 | + onDelete={handleDeletePlan} |
| 163 | + /> |
| 164 | + )) |
| 165 | + ) : ( |
| 166 | + <p>아직 생성된 플랜이 없습니다. 첫 플랜을 추가해보세요!</p> |
| 167 | + )} |
99 | 168 | </section> |
100 | 169 |
|
101 | | - {/* 5. 하단 추가 버튼 */} |
| 170 | + {/* 하단 추가 버튼 or 인라인 폼 */} |
102 | 171 | <div className="mt-6"> |
103 | | - <AddPlanButton /> |
| 172 | + {isAdding ? ( |
| 173 | + <InlineAddPlanForm |
| 174 | + onSave={handleSavePlan} |
| 175 | + onCancel={handleCancelAdd} |
| 176 | + /> |
| 177 | + ) : ( |
| 178 | + <div onClick={() => setIsAdding(true)}> |
| 179 | + <AddPlanButton /> |
| 180 | + </div> |
| 181 | + )} |
104 | 182 | </div> |
105 | 183 | </div> |
106 | 184 | ); |
|
0 commit comments