Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/common/Modal/ErrorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface ErrorModalProps {
showCloseButton?: boolean;
children?: React.ReactNode;
className?: string;
confirmText?: string;
}

/* ๊ธฐ์กด์— errorMessage๋ฅผ props๋กœ ๋ฐ›์•˜๋Š”๋ฐ,
Expand All @@ -24,6 +25,7 @@ const ErrorModal = ({
showCloseButton = false,
children,
className,
confirmText,
}: ErrorModalProps) => {
return (
<Modal
Expand All @@ -48,7 +50,7 @@ const ErrorModal = ({
onConfirm?.();
}}
>
ํ™•์ธ
{confirmText ?? 'ํ™•์ธ'}
</Button>
</Modal.Footer>
</Modal>
Expand Down
15 changes: 10 additions & 5 deletions src/hooks/useInitUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ import { useUserStore } from '@/stores/userStore';
* ์•ฑ ์ง„์ž… ์‹œ ์œ ์ € ์ •๋ณด๋ฅผ ํŒจ์น˜ํ•˜๊ณ  Zustand์— ์ €์žฅํ•˜๋Š” ํ›… */
export const useInitUser = () => {
const setUser = useUserStore((state) => state.setUser);
const clearUser = useUserStore((state) => state.clearUser);
const setIsUserLoading = useUserStore((state) => state.setIsUserLoading);

/* ์ฒ˜์Œ์—๋งŒ ์‹คํ–‰ */
useEffect(() => {
const fetchUser = async () => {
setIsUserLoading(true); // ์œ ์ € ์ •๋ณด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์‹œ์ž‘

try {
const user = await getUser();
setUser(user);
const user = await getUser(); // API ํ˜ธ์ถœ
setUser(user); // ๋กœ๊ทธ์ธ ์ƒํƒœ๋กœ ์ „์—ญ ์ƒํƒœ ์„ค์ •
} catch (error) {
// ๋กœ๊ทธ์ธ ์•ˆ ๋ผ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฌด์‹œ
clearUser(); // ์‹คํŒจ ์‹œ ๋กœ๊ทธ์•„์›ƒ ์ƒํƒœ๋กœ ์ดˆ๊ธฐํ™”
} finally {
setIsUserLoading(false); // ๋กœ๋”ฉ ๋
}
};

fetchUser();
}, [setUser]);
}, [setUser, clearUser, setIsUserLoading]);
};
3 changes: 2 additions & 1 deletion src/hooks/useUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { useUserStore } from '@/stores/userStore';
export const useUser = () => {
const user = useUserStore((state) => state.user);
const isLoggedIn = useUserStore((state) => state.isLoggedIn);
const isUserLoading = useUserStore((state) => state.isUserLoading);
const setUser = useUserStore((state) => state.setUser);
const clearUser = useUserStore((state) => state.clearUser);

return { user, isLoggedIn, setUser, clearUser };
return { user, isLoggedIn, isUserLoading, setUser, clearUser };
};
87 changes: 55 additions & 32 deletions src/pages/my-profile/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';

import { useRouter } from 'next/router';

import { LoadingOverlay } from '@/components/common/LoadingOverlay';
import ErrorModal from '@/components/common/Modal/ErrorModal';
import Profile from '@/components/my-profile/Profile';
import { ReviewList } from '@/components/my-profile/ReviewList';
import { TabNav } from '@/components/my-profile/Tab';
import { WineList } from '@/components/my-profile/WineList';
import { useUser } from '@/hooks/useUser';

/**
* MyProfile
Expand All @@ -15,43 +20,61 @@ import { WineList } from '@/components/my-profile/WineList';
* - ๊ฐ ๋ฆฌ์ŠคํŠธ์˜ ์ด ๊ฐœ์ˆ˜๋ฅผ ์ƒ์œ„์—์„œ ๊ด€๋ฆฌ
*/
export default function MyProfile() {
/**
* ํ˜„์žฌ ์„ ํƒ๋œ ํƒญ ์ƒํƒœ
* - 'reviews': ๋‚ด๊ฐ€ ์“ด ๋ฆฌ๋ทฐ
* - 'wines': ๋‚ด๊ฐ€ ๋“ฑ๋กํ•œ ์™€์ธ
*/
const [tab, setTab] = useState<'reviews' | 'wines'>('reviews');
const router = useRouter();
const { isLoggedIn, isUserLoading } = useUser();
const [showModal, setShowModal] = useState(false);

/** ๋ฆฌ๋ทฐ ์ด ๊ฐœ์ˆ˜ (๋ฆฌ๋ทฐ ํƒญ์—์„œ ReviewList๊ฐ€ ์„ค์ •ํ•จ) */
const [tab, setTab] = useState<'reviews' | 'wines'>('reviews');
const [reviewsCount, setReviewsCount] = useState(0);

/** ์™€์ธ ์ด ๊ฐœ์ˆ˜ (์™€์ธ ํƒญ์—์„œ WineList๊ฐ€ ์„ค์ •ํ•จ) */
const [winesCount, setWinesCount] = useState(0);

useEffect(() => {
if (!isUserLoading && !isLoggedIn) {
setShowModal(true);
}
}, [isLoggedIn, isUserLoading]);

const handleRedirect = () => {
router.push('/signin'); // ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™
};

return (
<div>
<main className='max-w-6xl mx-auto p-4 gap-6 flex flex-col xl:flex-row'>
{/* ํ”„๋กœํ•„ ์„น์…˜ */}
<Profile />

{/* ํƒญ + ๋ฆฌ์ŠคํŠธ */}
<div className='flex flex-col flex-1'>
{/* ํƒญ ๋„ค๋น„๊ฒŒ์ด์…˜: ํ˜„์žฌ ํƒญ, ํƒญ ์ „ํ™˜ ํ•จ์ˆ˜, ๊ฐ๊ฐ์˜ ๊ฐœ์ˆ˜ ์ „๋‹ฌ */}
<TabNav
current={tab}
onChange={setTab}
reviewsCount={reviewsCount}
winesCount={winesCount}
/>

{/* ํƒญ ์ƒํƒœ์— ๋”ฐ๋ผ ๋ฆฌ์ŠคํŠธ ์ปดํฌ๋„ŒํŠธ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง */}
{tab === 'reviews' ? (
<ReviewList setTotalCount={setReviewsCount} />
) : (
<WineList setTotalCount={setWinesCount} />
)}
</div>
</main>
{/* ๋กœ๋”ฉ ์ค‘์ผ ๋•Œ ์ „์ฒด ์˜ค๋ฒ„๋ ˆ์ด */}
{isUserLoading && <LoadingOverlay />}

<ErrorModal
open={showModal}
onOpenChange={() => {}}
onConfirm={handleRedirect}
confirmText='๋กœ๊ทธ์ธ ํ•˜๋Ÿฌ ๊ฐ€๊ธฐ'
>
๋งˆ์ดํŽ˜์ด์ง€๋Š” ๋กœ๊ทธ์ธ ํ›„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์–ด์š”
</ErrorModal>
{isLoggedIn && (
<main className='max-w-6xl mx-auto p-4 gap-6 flex flex-col xl:flex-row'>
{/* ํ”„๋กœํ•„ ์„น์…˜ */}
<Profile />

{/* ํƒญ + ๋ฆฌ์ŠคํŠธ */}
<div className='flex flex-col flex-1'>
{/* ํƒญ ๋„ค๋น„๊ฒŒ์ด์…˜: ํ˜„์žฌ ํƒญ, ํƒญ ์ „ํ™˜ ํ•จ์ˆ˜, ๊ฐ๊ฐ์˜ ๊ฐœ์ˆ˜ ์ „๋‹ฌ */}
<TabNav
current={tab}
onChange={setTab}
reviewsCount={reviewsCount}
winesCount={winesCount}
/>

{/* ํƒญ ์ƒํƒœ์— ๋”ฐ๋ผ ๋ฆฌ์ŠคํŠธ ์ปดํฌ๋„ŒํŠธ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง */}
{tab === 'reviews' ? (
<ReviewList setTotalCount={setReviewsCount} />
) : (
<WineList setTotalCount={setWinesCount} />
)}
</div>
</main>
)}
</div>
);
}
38 changes: 24 additions & 14 deletions src/stores/userStore.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import { create } from 'zustand';

/* ์œ ์ € ์ •๋ณด ํƒ€์ž… */
interface User {
id: number;
nickname: string;
image: string | null;
teamId: string;
createdAt: string;
updatedAt: string;
}
import { GetUserResponse } from '@/types/UserTypes';

/* Zustand ์œ ์ € ์ƒํƒœ ์ €์žฅ์†Œ ํƒ€์ž… */
interface UserStore {
user: User | null;
user: GetUserResponse | null;
isLoggedIn: boolean;
isUserLoading: boolean;

/* ์œ ์ € ์ •๋ณด ์„ค์ • (๋กœ๊ทธ์ธ ๋“ฑ) */
setUser: (user: User) => void;
setUser: (user: GetUserResponse) => void;

/* ์œ ์ € ์ •๋ณด ์ดˆ๊ธฐํ™” (๋กœ๊ทธ์•„์›ƒ ๋“ฑ) */
clearUser: () => void;

/* ์œ ์ € ์ •๋ณด ๋กœ๋”ฉ ์ƒํƒœ ๋ณ€๊ฒฝ */
setIsUserLoading: (loading: boolean) => void;
}

/**
* ์œ ์ € ์ƒํƒœ ์ „์—ญ ์Šคํ† ์–ด */
* ์œ ์ € ์ƒํƒœ ์ „์—ญ ์Šคํ† ์–ด
*/
export const useUserStore = create<UserStore>((set) => ({
user: null,
isLoggedIn: false,
setUser: (user) => set({ user, isLoggedIn: true }),
clearUser: () => set({ user: null, isLoggedIn: false }),
isUserLoading: true,

setUser: (user) =>
set({
user,
isLoggedIn: true,
}),

clearUser: () =>
set({
user: null,
isLoggedIn: false,
}),

setIsUserLoading: (loading) => set({ isUserLoading: loading }),
}));