@@ -14,35 +14,20 @@ import {
1414 AddTodo ,
1515 toggleTodoStatus ,
1616} from '@/lib/home/todoService' ;
17+ import { getProfile , Profile } from '@/lib/home/profileService' ;
1718
1819import { User , onAuthStateChanged } from 'firebase/auth' ;
1920import { auth } from '@/lib/firebase' ;
2021
2122const Page = ( ) => {
23+ const [ profile , setProfile ] = useState < Profile | null > ( null ) ;
2224 const [ todos , setTodos ] = useState < Todo [ ] > ( [ ] ) ;
25+
2326 const [ error , setError ] = useState < string | null > ( null ) ;
2427
2528 // 현재 사용자 정보
2629 const [ currentUser , setCurrentUser ] = useState < User | null > ( null ) ;
2730
28- // 사용자 로그인 상태 감지
29- useEffect ( ( ) => {
30- const unsubscribe = onAuthStateChanged ( auth , ( user ) => {
31- setCurrentUser ( user ) ;
32- } ) ;
33-
34- return ( ) => unsubscribe ( ) ;
35- } , [ ] ) ;
36-
37- // 사용자가 존재하면 데이터 불러옴
38- useEffect ( ( ) => {
39- if ( currentUser ) {
40- loadTodos ( currentUser . uid ) ;
41- } else {
42- setTodos ( [ ] ) ;
43- }
44- } , [ currentUser ] ) ;
45-
4631 // 1. 할 일 목록 불러오기
4732 const loadTodos = async ( uid : string ) => {
4833 try {
@@ -86,6 +71,36 @@ const Page = () => {
8671 }
8772 } ;
8873
74+ // 사용자 로그인 상태 감지
75+ useEffect ( ( ) => {
76+ const unsubscribe = onAuthStateChanged ( auth , ( user ) => {
77+ setCurrentUser ( user ) ;
78+ } ) ;
79+
80+ return ( ) => unsubscribe ( ) ;
81+ } , [ ] ) ;
82+
83+ // 사용자가 존재하면 데이터 불러옴
84+ useEffect ( ( ) => {
85+ if ( currentUser ) {
86+ const loadData = async ( ) => {
87+ try {
88+ const userProfile = await getProfile ( currentUser . uid ) ;
89+ setProfile ( userProfile ) ;
90+ await loadTodos ( currentUser . uid ) ;
91+ } catch ( err ) {
92+ console . error ( err ) ;
93+ setError ( '프로필 정보를 불러오는 데 실패하였습니다' ) ;
94+ }
95+ } ;
96+ loadData ( ) ;
97+ } else {
98+ setTodos ( [ ] ) ;
99+ setProfile ( null ) ;
100+ }
101+ } , [ currentUser ] ) ;
102+
103+ // 유저 정보가 없을 시
89104 if ( ! currentUser ) {
90105 return (
91106 < div className = "flex min-h-screen items-center justify-center" >
@@ -94,13 +109,17 @@ const Page = () => {
94109 ) ;
95110 }
96111
112+ // 유저 정보가 있을 시
97113 return (
98114 < div className = "bg-background flex min-h-screen flex-col gap-4 font-sans md:p-[137px]" >
99115 { /* 1. Header */ }
100116 < HeaderSection />
101117
102118 { /* 2-1. ProfileSection */ }
103- < ProfileSection className = "grid grid-cols-1 gap-4 md:grid-cols-3" />
119+ < ProfileSection
120+ className = "grid grid-cols-1 gap-4 md:grid-cols-3"
121+ profile = { profile }
122+ />
104123
105124 { /* 2-2. GraphSection */ }
106125 < GraphSection > </ GraphSection >
0 commit comments