-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfoInputPage.tsx
More file actions
141 lines (134 loc) · 4.2 KB
/
InfoInputPage.tsx
File metadata and controls
141 lines (134 loc) · 4.2 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
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { useForm, SubmitHandler } from 'react-hook-form';
import Header from '../../../design-system/ui/Header';
import Button from '../../../design-system/ui/Button';
import UnderlineTextField from '../../../design-system/ui/textFields/UnderlineTextField';
import { FormData, zodValidation } from '../../shared/lib/formValidation';
import { useAgreeTerms, useUserInfo, useUserUpdate } from '../../features/join/hooks/useUserHook';
import useAuthStore from '../../app/provider/authStore';
import { formatPhoneNumber } from '../../shared/utils/phoneFormatter';
import { useAgreementStore } from '../../features/join/model/agreementStore';
const InfoInputPage = () => {
const { data, isLoading } = useUserInfo();
const { login, setName } = useAuthStore();
const { mutate: updateUser } = useUserUpdate();
const {
register,
handleSubmit,
formState: { errors, isValid },
reset,
setValue,
watch,
} = useForm<FormData>({
mode: 'onChange',
defaultValues: {
name: '',
email: '',
phone: '',
},
...zodValidation,
});
const navigate = useNavigate();
const phoneValue = watch('phone');
const { getAgreementStates } = useAgreementStore();
const { mutate: agreeTerms } = useAgreeTerms();
const handlePhoneChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const formatted = formatPhoneNumber(e.target.value);
setValue('phone', formatted, { shouldValidate: true });
};
const onSubmit: SubmitHandler<FormData> = formData => {
const agreementStates = getAgreementStates();
const updatedData = {
name: formData.name,
email: data?.email || '',
phoneNumber: formData.phone,
};
updateUser(updatedData, {
onSuccess: () => {
agreeTerms(
{
serviceAgreed: agreementStates.serviceAgreed,
privacyPolicyAgree: agreementStates.privacyPolicyAgree,
personalInfoUsageAgreed: agreementStates.personalInfoUsageAgreed,
marketingAgreed: agreementStates.marketingAgreed,
},
{
onSuccess: () => {
login();
setName(formData.name);
alert('회원가입이 완료되었습니다.');
navigate('/');
},
}
);
},
onError: () => {
alert('정보 업데이트에 실패했습니다. 다시 시도해주세요.');
},
});
};
useEffect(() => {
if (data) {
reset({
name: data.name || '',
email: data.email || '',
phone: '',
});
}
}, [data, reset]);
if (isLoading) {
return <div>로딩 중...</div>;
}
return (
<div className="relative flex flex-col w-full h-screen border ">
<Header
centerContent="정보입력"
leftButtonLabel="<"
leftButtonClassName="text-2xl z-30 font-semibold"
leftButtonClick={() => navigate(-1)}
color="black"
/>
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col gap-5 px-10 py-12 md:px-12 md:py-16">
{/* 이름 필드 */}
<UnderlineTextField
label="이름"
placeholder="이름"
errorMessage={errors.name?.message}
className="text-xl"
{...register('name')}
/>
{/* 연락처 필드 */}
<UnderlineTextField
label="연락처"
placeholder={'연락처'}
type="tel"
errorMessage={errors.phone?.message}
className="text-xl"
value={phoneValue}
onChange={handlePhoneChange}
/>
{/* 이메일 필드 */}
<UnderlineTextField
label="이메일"
placeholder="이메일"
type="email"
errorMessage={errors.email?.message}
className="text-xl"
readOnly
{...register('email')}
/>
</form>
<div className="flex flex-grow" />
<div className="w-full px-5 py-10">
<Button
label="시작하기"
onClick={handleSubmit(onSubmit)}
disabled={!isValid}
className="w-full h-12 rounded-full"
/>
</div>
</div>
);
};
export default InfoInputPage;