-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthCodeForm.tsx
More file actions
174 lines (155 loc) · 5.33 KB
/
Copy pathAuthCodeForm.tsx
File metadata and controls
174 lines (155 loc) · 5.33 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { BlockButton, LabelButton, TextField, toastController } from "@ject/jds";
import type { FormEventHandler } from "react";
import { useState } from "react";
import { Controller } from "react-hook-form";
import { PATH } from "@/constants/path";
import {
useCheckEmailExistsMutation,
useSendAuthCodeMutation,
useVerifyAuthCodeMutation,
} from "@/hooks/apply";
import { useApplyEmailForm } from "@/hooks/useApplyEmailForm";
import { useCountdownTimer } from "@/hooks/useCountdownTimer";
import { deriveInputValidation } from "@/utils/validationHelpers";
const formatTime = (seconds: number) => {
const min = Math.floor(seconds / 60);
const sec = seconds % 60;
return `${min}:${String(sec).padStart(2, "0")}`;
};
interface AuthCodeFormProps {
defaultEmail?: string;
sendGroupCode: "AUTH_CODE" | "PIN_RESET";
onVerified: (email: string, authCode: string) => void | Promise<void>;
onExistingMember?: (email: string) => void;
}
export function AuthCodeForm({
defaultEmail = "",
sendGroupCode,
onVerified,
onExistingMember,
}: AuthCodeFormProps) {
const [hasSentCode, setHasSentCode] = useState(false);
const [emailErrorMessage, setEmailErrorMessage] = useState<string | null>(null);
const [authCode, setAuthCode] = useState("");
const timer = useCountdownTimer();
const {
control: emailControl,
watch: watchEmail,
getFieldState,
formState,
} = useApplyEmailForm();
const currentEmail = watchEmail("email") || defaultEmail;
const emailState = getFieldState("email", formState);
const { mutateAsync: checkEmailMutateAsync } = useCheckEmailExistsMutation();
const { mutate: sendCodeMutate } = useSendAuthCodeMutation();
const { mutateAsync: verifyCodeMutateAsync } = useVerifyAuthCodeMutation();
const handleSendEmailCode = async () => {
setEmailErrorMessage(null);
//Todo: PIN_RESET 일 때는 checkEmailMutate가 호출되지 않도록
const isUserExists = await checkEmailMutateAsync(currentEmail);
if (sendGroupCode === "AUTH_CODE" && isUserExists) {
onExistingMember?.(currentEmail);
return;
}
if (sendGroupCode === "PIN_RESET" && !isUserExists) {
setEmailErrorMessage("가입되지 않은 이메일입니다. 이메일을 확인해주세요.");
return;
}
sendCodeMutate(
{ email: currentEmail, sendGroupCode },
{
onSuccess: () => {
setHasSentCode(true);
if (sendGroupCode === "AUTH_CODE") {
timer.start(180);
}
},
onError: () => {
toastController.destructive(
"인증번호 발송 실패",
"일시적 오류일 수 있으니 다시 시도해주세요.",
);
},
},
);
};
const handleVerifyCode: FormEventHandler<HTMLFormElement> = e => {
e.preventDefault();
verifyCodeMutateAsync({ email: currentEmail, authCode, template: sendGroupCode })
.then(() => onVerified(currentEmail, authCode))
.catch(() => {
toastController.destructive(
"인증번호 시도 실패",
"일시적 오류일 수 있으니 다시 시도해주세요.",
);
});
};
const hasEmailFormError = Boolean(emailState.error) || Boolean(emailErrorMessage);
const emailValidation = deriveInputValidation({
hasError: hasEmailFormError,
hasValue: Boolean(currentEmail?.length),
});
const emailHelperText = emailState.error?.message ?? emailErrorMessage ?? "";
const authCodeHelperText = timer.isActive
? `인증번호 유효 시간 ${formatTime(timer.seconds)}`
: "";
return (
<div className='flex flex-col items-start gap-(--semantic-spacing-24) self-stretch'>
<Controller
name='email'
control={emailControl}
defaultValue={defaultEmail}
render={({ field }) => (
<TextField.Button
type='email'
label='이메일'
validation={emailValidation}
helperText={emailHelperText}
placeholder='이메일을 입력해주세요'
value={field.value}
onChange={field.onChange}
button={
<BlockButton.Basic
size='md'
variant='solid'
hierarchy='primary'
disabled={!formState.isValid}
onClick={() => void handleSendEmailCode()}
>
인증번호 받기
</BlockButton.Basic>
}
/>
)}
/>
{hasSentCode && (
<form className='self-stretch' onSubmit={handleVerifyCode}>
<TextField.Button
label='인증번호'
helperText={authCodeHelperText}
placeholder='인증번호를 입력해주세요'
value={authCode}
onChange={e => setAuthCode(e.target.value)}
button={
<BlockButton.Basic type='submit' size='md' variant='solid' hierarchy='primary'>
인증하기
</BlockButton.Basic>
}
/>
</form>
)}
{hasSentCode && (
<LabelButton.Basic
size='sm'
hierarchy='secondary'
suffixIcon='arrow-right-s-line'
onClick={() => {
window.open(`${PATH.faq}/1/apply-5`, "_blank");
}}
>
인증번호를 받지 못하셨나요?
</LabelButton.Basic>
)}
</div>
);
}