Skip to content

Commit 3e35ca1

Browse files
committed
refactor: 마이페이지 타입 전역 관리 리팩토링
1 parent f0cdee6 commit 3e35ca1

File tree

4 files changed

+27
-55
lines changed

4 files changed

+27
-55
lines changed

src/apis/myPage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const fetchMyPageInfo = async () => {
44
try {
55
const response = await client.get('/api/members/me');
66
if (!response) throw new Error('❌데이터를 불러오던 중 오류가 발생했습니다');
7-
return response.data;
7+
return response.data.data;
88
} catch (error) {
99
console.error(error);
1010
}

src/pages/MyPage/index.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
import { useState, useEffect } from 'react';
22
import { Link } from 'react-router';
33

4+
import { fetchMyPageInfo } from '@/apis/myPage';
45
import { deleteUserInfo } from '@/apis/auth';
56
import ConfirmModal from '@/components/ConfirmModal';
67
import useAuthStore from '@/stores/authStore';
7-
import useMyPageStore from '@/stores/myPageStore';
88

99
import { TEMPERATURE_RANGE } from './constants';
1010
import useToastStore from '@/stores/toastStore';
1111
import ModalOverlay from '@/components/ModalOverlay';
1212

1313
const MyPage = () => {
14-
useEffect(() => {
15-
fetchMyPageInfo();
16-
}, []);
17-
18-
const { data, fetchMyPageInfo } = useMyPageStore();
14+
const [myInfo, setMyInfo] = useState<MyPage>();
1915

2016
const [isOpenModal, setIsOpenModal] = useState(false);
2117
const [isOpenWarningModal, setIsOpenWarningModal] = useState(false);
2218

2319
const logout = useAuthStore((state) => state.logout);
2420
const setToastActive = useToastStore((state) => state.setToastActive);
2521

22+
useEffect(() => {
23+
const fetchData = async () => {
24+
const data = await fetchMyPageInfo();
25+
if (data) setMyInfo(data);
26+
};
27+
fetchData();
28+
}, []);
29+
2630
const getDescriptionByTemperature = (temp: number) => {
2731
const range = TEMPERATURE_RANGE.find((range) => temp >= range.min && temp < range.max);
2832
return range?.description;
2933
};
3034

31-
const description = getDescriptionByTemperature(Number(data.temperature));
32-
3335
const handleLeave = async () => {
3436
try {
3537
const response = await deleteUserInfo();
@@ -88,7 +90,7 @@ const MyPage = () => {
8890

8991
<main className="flex grow flex-col gap-12 px-5 pt-20 pb-6">
9092
<h1 className="h2-b mx-auto flex gap-1.5">
91-
{data.zipCode.split('').map((code, index) => (
93+
{myInfo?.zipCode.split('').map((code, index) => (
9294
<div
9395
key={index}
9496
className="flex h-13.5 w-10 items-center justify-center rounded-sm bg-white inset-shadow-[0_4px_4px_0] inset-shadow-black/10"
@@ -99,13 +101,15 @@ const MyPage = () => {
99101
</h1>
100102
<section>
101103
<h2 className="mb-2 flex justify-between">
102-
<p className="body-sb text-gray-60 dark:text-white">{description}</p>
103-
<p className="body-sb text-accent-2">{data.temperature}</p>
104+
<p className="body-sb text-gray-60 dark:text-white">
105+
{getDescriptionByTemperature(Number(myInfo?.temperature))}
106+
</p>
107+
<p className="body-sb text-accent-2">{myInfo?.temperature}</p>
104108
</h2>
105109
<div className="h-4 w-full rounded-full bg-white">
106110
<div
107111
className="h-full w-[calc(${degree}%)] rounded-full bg-[#FFB5AC]"
108-
style={{ width: `calc(${data.temperature}%)` }}
112+
style={{ width: `calc(${myInfo?.temperature}%)` }}
109113
/>
110114
</div>
111115
</section>
@@ -131,8 +135,8 @@ const MyPage = () => {
131135
<div className="flex justify-between">
132136
<p className="body-sb text-gray-100 dark:text-white">로그인 정보</p>
133137
<p className="body-r text-gray-60 dark:text-white">
134-
<span className="mr-2">{data.social}</span>
135-
<span>{data.email}</span>
138+
<span className="mr-2">{myInfo?.social}</span>
139+
<span>{myInfo?.email}</span>
136140
</p>
137141
</div>
138142
<div
@@ -143,7 +147,7 @@ const MyPage = () => {
143147
>
144148
<p className="body-sb text-gray-100 dark:text-white">경고 횟수</p>
145149
<p className="body-r text-gray-60 dark:text-white">
146-
<span>{data.warningCount}</span>
150+
<span>{myInfo?.warningCount}</span>
147151
</p>
148152
</div>
149153

src/stores/myPageStore.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/types/mypage.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
interface MyPage {
2+
zipCode: string;
3+
temperature: string;
4+
social: string;
5+
email: string;
6+
warningCount: number;
7+
}

0 commit comments

Comments
 (0)