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