-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileInfo.tsx
More file actions
148 lines (138 loc) · 5.22 KB
/
ProfileInfo.tsx
File metadata and controls
148 lines (138 loc) · 5.22 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { zodResolver } from '@hookform/resolvers/zod';
import { useEffect, useState } from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { myPageSchema } from '../../../shared/lib/formValidation';
import { formatPhoneNumber } from '../../../shared/utils/phoneFormatter';
import ProfileCircle from '../../../../design-system/ui/Profile';
import TertiaryButton from '../../../../design-system/ui/buttons/TertiaryButton';
import DefaultTextField from '../../../../design-system/ui/textFields/DefaultTextField';
import useAuthStore from '../../../app/provider/authStore';
import { useUserInfo, useUserUpdate } from '../../../features/join/hooks/useUserHook';
import { formatProfilName } from '../../../shared/lib/formatProfileName';
const ProfileInfo = () => {
const isLoggedIn = useAuthStore(state => state.isLoggedIn);
const { setName } = useAuthStore();
const { data, isLoading, error, refetch } = useUserInfo(isLoggedIn);
const { mutate: updateUser } = useUserUpdate();
const [isEditing, setIsEditing] = useState(false);
const {
register,
handleSubmit,
formState: { errors },
setValue,
} = useForm<{ name: string; phone: string }>({
defaultValues: { name: data?.name || '', phone: data?.phoneNumber || '' },
resolver: zodResolver(myPageSchema),
});
useEffect(() => {
if (data) {
setValue('name', data.name);
setValue('phone', data.phoneNumber);
}
}, [data, setValue]);
const handlePhoneChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const formatted = formatPhoneNumber(e.target.value);
setValue('phone', formatted, { shouldValidate: true });
};
const onSubmit: SubmitHandler<{ name: string; phone: string }> = formData => {
const { name, phone } = formData;
if (!data?.id) {
alert('사용자 정보를 불러오는 데 실패했습니다. 다시 시도해주세요.');
return;
}
const updatedData = {
id: data.id,
name: name,
email: data.email,
phoneNumber: phone,
};
updateUser(updatedData, {
onSuccess: () => {
setName(name);
refetch();
setIsEditing(false);
},
onError: () => {
alert('정보 업데이트에 실패했습니다. 다시 시도해주세요.');
},
});
};
if (isLoading) {
return <div>로딩 중...</div>;
}
if (error) {
return <div>정보를 불러오는데 실패했습니다. 다시 시도해주세요.</div>;
}
return (
<div className="relative w-full h-52 md:h-56">
<div className="absolute inset-0 bg-main rounded-[10px]" />
<div className="relative z-10 bg-dashboardBg rounded-[10px] p-5 ml-2 h-full">
<div className="flex flex-col items-start h-full">
<h1 className="text-20 md:text-22 font-bold">프로필 정보</h1>
{!isEditing ? (
<>
<div className="flex items-center gap-3">
<ProfileCircle
profile="userProfile"
name={formatProfilName(data?.name || '')}
className="w-16 h-16 md:w-18 md:h-18 text-xl md:text-2xl"
/>
<div className="flex flex-col gap-1 py-5 md:py-7 mb-2">
<span className="text-17 md:text-19 font-bold">{data?.name}</span>
<span className="text-14 md:text-16 text-gray-500">{data?.phoneNumber}</span>
</div>
</div>
<TertiaryButton
label="수정하기"
type="button"
color="pink"
size="full"
onClick={() => setIsEditing(true)}
/>
</>
) : (
<form onSubmit={handleSubmit(onSubmit)} className="w-full">
<div className="flex py-2 md:py-3 gap-3">
<ProfileCircle
profile="userProfile"
name={formatProfilName(data?.name || '')}
className="w-16 h-16 md:w-18 md:h-18 text-xl md:text-2xl"
/>
<div className="flex flex-col gap-1">
<DefaultTextField
{...register('name')}
errorPosition="right"
errorMessage={errors.name?.message}
className="h-9"
/>
<DefaultTextField
{...register('phone')}
onChange={handlePhoneChange}
errorPosition="right"
errorMessage={errors.phone?.message}
className="h-9"
/>
</div>
</div>
<div className="flex gap-2">
<TertiaryButton
label="취소하기"
type="button"
color="pink"
size="full"
onClick={() => {
setIsEditing(false);
setValue('name', data?.name || '');
setValue('phone', data?.phoneNumber || '');
}}
/>
<TertiaryButton label="수정하기" type="submit" color="pink" size="full" />
</div>
</form>
)}
</div>
</div>
</div>
);
};
export default ProfileInfo;