Skip to content

Commit bdc7ed5

Browse files
committed
feat: 홈 페이지에서 유저 정보를 가져와 카드 내용을 구성하도록 구현
1 parent 6ff687a commit bdc7ed5

4 files changed

Lines changed: 67 additions & 25 deletions

File tree

app/(with-sidebar)/(home)/page.tsx

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,20 @@ import {
1414
AddTodo,
1515
toggleTodoStatus,
1616
} from '@/lib/home/todoService';
17+
import { getProfile, Profile } from '@/lib/home/profileService';
1718

1819
import { User, onAuthStateChanged } from 'firebase/auth';
1920
import { auth } from '@/lib/firebase';
2021

2122
const 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,32 @@ 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+
const userProfile = await getProfile(currentUser.uid);
88+
setProfile(userProfile);
89+
90+
await loadTodos(currentUser.uid);
91+
};
92+
loadData();
93+
} else {
94+
setTodos([]);
95+
setProfile(null);
96+
}
97+
}, [currentUser]);
98+
99+
// 유저 정보가 없을 시
89100
if (!currentUser) {
90101
return (
91102
<div className="flex min-h-screen items-center justify-center">
@@ -94,13 +105,17 @@ const Page = () => {
94105
);
95106
}
96107

108+
// 유저 정보가 있을 시
97109
return (
98110
<div className="bg-background flex min-h-screen flex-col gap-4 font-sans md:p-[137px]">
99111
{/* 1. Header */}
100112
<HeaderSection />
101113

102114
{/* 2-1. ProfileSection */}
103-
<ProfileSection className="grid grid-cols-1 gap-4 md:grid-cols-3" />
115+
<ProfileSection
116+
className="grid grid-cols-1 gap-4 md:grid-cols-3"
117+
profile={profile}
118+
/>
104119

105120
{/* 2-2. GraphSection */}
106121
<GraphSection></GraphSection>

components/home/ProfileSection.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { Profile } from '@/lib/home/profileService';
12
import Card from './Card';
23

34
interface ProfileSectionProps {
45
className?: string;
6+
profile: Profile | null;
57
}
68

7-
const ProfileSection = ({ className }: ProfileSectionProps) => {
9+
const ProfileSection = ({ className, profile }: ProfileSectionProps) => {
810
return (
911
<div className={className}>
1012
<div className="md:col-span-2">
@@ -14,12 +16,14 @@ const ProfileSection = ({ className }: ProfileSectionProps) => {
1416
</div>
1517

1618
<div>
17-
<h3 className="text-xl font-bold text-gray-900">이건무</h3>
18-
<p className="text-sm text-gray-500">@mnmnnmm324</p>
19+
<h3 className="text-xl font-bold text-gray-900">
20+
{profile?.nickname}
21+
</h3>
22+
<p className="text-sm text-gray-500">{profile?.email}</p>
1923
<div className="mt-1 flex gap-2 text-xs font-medium">
20-
<span className="text-primary">38193일 </span>연속
24+
<span className="text-primary">{profile?.streakDays} </span>연속
2125
<span className="text-gray-400">|</span>
22-
<span className="text-primary">12개 </span>TIL
26+
<span className="text-primary">{profile?.tilCount} </span>TIL
2327
</div>
2428
</div>
2529
</Card>

lib/home/profileService.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { doc, getDoc } from 'firebase/firestore';
2+
import { db } from '../firebase';
3+
4+
// 프로필 데이터 타입
5+
export interface Profile {
6+
nickname: string;
7+
email: string;
8+
streakDays: number;
9+
tilCount: number;
10+
dailyGoal: number;
11+
}
12+
13+
// 1. 유저 프로필 정보 가져오기
14+
export const getProfile = async (uid: string): Promise<Profile | null> => {
15+
const userDocRef = doc(db, 'users', uid);
16+
const docSnap = await getDoc(userDocRef);
17+
18+
if (docSnap.exists()) {
19+
return docSnap.data() as Profile;
20+
} else return null;
21+
};
22+
23+
// 2. 사용자 프로필 정보 업데이트

lib/home/todoService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface Todo {
2121
// 1. 할 일 목록 가져오기
2222
export const fetchTodos = async (uid: string): Promise<Todo[]> => {
2323
const todosCollectionPath = collection(db, 'users', uid, 'todos');
24-
const q = query(todosCollectionPath, orderBy('createAt', 'desc'));
24+
const q = query(todosCollectionPath, orderBy('createAt', 'asc'));
2525
const snapshot = await getDocs(q);
2626

2727
return snapshot.docs.map((doc) => ({

0 commit comments

Comments
 (0)