-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPage.tsx
More file actions
126 lines (119 loc) · 4.67 KB
/
MyPage.tsx
File metadata and controls
126 lines (119 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { useEffect, useState } from 'react';
import Header from '../../../../design-system/ui/Header';
import DefaultTextField from '../../../../design-system/ui/textFields/DefaultTextField';
import TertiaryButton from '../../../../design-system/ui/buttons/TertiaryButton';
import { SubmitHandler, useForm } from 'react-hook-form';
import BottomBar from '../../../widgets/main/ui/BottomBar';
import { myPageSchema } from '../../../shared/lib/formValidation';
import { useUserInfo, useUserUpdate } from '../../../features/join/hooks/useUserHook';
import useAuthStore from '../../../app/provider/authStore';
const MyPage = () => {
const { setName } = useAuthStore();
const isLoggedIn = useAuthStore(state => state.isLoggedIn);
const { data, isLoading, error } = useUserInfo(isLoggedIn);
const { mutate: updateUser } = useUserUpdate();
const [isChanged, setIsChanged] = useState<string>('');
const {
register,
handleSubmit,
formState: { errors },
setValue,
} = useForm({
defaultValues: { name: data?.name || '', phone: data?.phoneNumber || '' },
...myPageSchema,
});
useEffect(() => {
if (data) {
setValue('name', data.name);
setValue('phone', data.phoneNumber);
}
}, [data, setValue]);
const onSubmit: SubmitHandler<{ name: string; phone: string }> = formData => {
const { name, phone } = formData;
const updatedData = {
id: data?.id || 0,
name: name || '',
email: data?.email || '',
phoneNumber: phone || '',
};
updateUser(updatedData, {
onSuccess: () => {
setName(name);
alert('정보가 성공적으로 업데이트되었습니다.');
},
onError: err => {
alert('정보 업데이트에 실패했습니다. 다시 시도해주세요.');
console.error(err);
},
});
// 이름과 전화번호가 변경되었는지 확인하고 메시지 설정
let changeMessage = '';
if (name !== data?.name && phone !== data?.phoneNumber) {
changeMessage = `${name}, ${phone}으로 변경되었습니다`;
} else if (name !== data?.name) {
changeMessage = `${name}으로 변경되었습니다`;
} else if (phone !== data?.phoneNumber) {
changeMessage = `${phone}으로 변경되었습니다`;
}
setIsChanged(changeMessage); // 변경 상태 업데이트
};
if (isLoading) {
return <div>로딩 중...</div>;
}
if (error) {
return <div>정보를 불러오는데 실패했습니다. 다시 시도해주세요.</div>;
}
return (
<div>
<Header leftButtonLabel="마이페이지" leftButtonClassName="font-bold text-2xl" />
<div className="flex flex-col px-5 mt-8 md:mt-10 gap-6">
<div className="flex flex-col gap-3">
<h1 className="text-xl font-bold">기본 정보</h1>
<div className="flex flex-col">
<span className="text-sm md:text-base font-normal">이메일</span>
<span className="text-sm md:text-base font-light">{data?.email || '로그인이 필요합니다'}</span>
</div>
</div>
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col gap-2">
<div className="flex gap-3">
<div className="flex flex-col gap-2">
<DefaultTextField
label="이름"
placeholder={data?.name}
className=" h-8"
labelClassName="text-sm md:text-base font-normal"
disabled={!isLoggedIn}
{...register('name')}
/>
{errors.name && <span className=" text-sm text-red-500 whitespace-nowrap">{errors.name.message}</span>}
</div>
<div className="flex flex-col gap-2">
<DefaultTextField
label="전화번호"
placeholder={data?.phoneNumber}
className="h-8"
labelClassName="text-sm md:text-base font-normal"
disabled={!isLoggedIn}
{...register('phone')}
/>
{errors.phone && <span className="text-sm text-red-500 whitespace-nowrap">{errors.phone.message}</span>}
</div>
</div>
{isChanged && <span className="text-red-500 text-sm">{isChanged}</span>}
<TertiaryButton
label="저장하기"
color="black"
size="large"
type="submit"
className="w-24 h-8"
disabled={!isLoggedIn}
/>
</form>
{!isLoggedIn && <p className="text-sm text-gray-500 mt-2">로그인 후 정보를 수정하실 수 있습니다.</p>}
</div>
{/* <PaymentCard title={'등록된 카드'} /> */}
<BottomBar />
</div>
);
};
export default MyPage;