Skip to content

Commit a202fb6

Browse files
Merge pull request #34 from DeveloperBlog-Devflow/feature/home-page
feat: 홈 페이지에서 유저 정보를 가져와 카드 내용을 구성하도록 구현
2 parents 6ff687a + 2038c59 commit a202fb6

5 files changed

Lines changed: 74 additions & 25 deletions

File tree

api/signup.api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type SignupPayload = {
66
nickname: string;
77
email: string;
88
password: string;
9+
streakDays: number;
10+
tilCount: number;
911
};
1012

1113
export async function signupWithEmail(p: SignupPayload) {
@@ -15,6 +17,8 @@ export async function signupWithEmail(p: SignupPayload) {
1517
nickname: p.nickname,
1618
email: p.email,
1719
createdAt: serverTimestamp(),
20+
streakDays: 0,
21+
tilCount: 0,
1822
});
1923
return cred.user;
2024
}

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

Lines changed: 38 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,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>

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
11+
12+
// 1. 유저 프로필 정보 가져오기
13+
export const getProfile = async (uid: string): Promise<Profile | null> => {
14+
const userDocRef = doc(db, 'users', uid);
15+
const docSnap = await getDoc(userDocRef);
16+
17+
if (docSnap.exists()) {
18+
return docSnap.data() as Profile;
19+
} else return null;
20+
};
21+
22+
// 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)