-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[#169] 비밀번호 찾기 페이지 리팩토링
- Loading branch information
Showing
3 changed files
with
202 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/pages/passwordResetPage/components/fieldValues/FieldValues.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import InputField from "@components/inputField/InputField"; | ||
import * as S from "../../PasswordReset.style"; | ||
import { EMAIL_REGEX } from "@constants/regex"; | ||
import { useFormContext } from "react-hook-form"; | ||
import { useEffect, useState } from "react"; | ||
import { useValidateEmailMutation } from "@hooks/api/mutation/useValidateEmailMutation"; | ||
import axios from "axios"; | ||
|
||
type FormValues = { | ||
email: string; | ||
password: string; | ||
checkPassword: string; | ||
code: string; | ||
isCodeCorrect: boolean; | ||
}; | ||
|
||
const FieldValues = () => { | ||
const { setError, clearErrors, control, watch, getValues } = | ||
useFormContext<FormValues>(); | ||
|
||
const [isEmailValidated, setIsEmailValidated] = useState(false); | ||
const [isCodeValidated, setIsCodeValidated] = useState(false); | ||
const [codeState, setCodeState] = useState("######"); | ||
|
||
const validateEmailMutation = useValidateEmailMutation(); | ||
|
||
const handleEmailValidateClick = async () => { | ||
const email = getValues("email"); | ||
validateEmailMutation.mutate( | ||
{ email }, | ||
{ | ||
onSuccess: (response) => { | ||
clearErrors("email"); | ||
setCodeState(response); | ||
setIsEmailValidated(true); | ||
}, | ||
onError: (error) => { | ||
if (axios.isAxiosError(error) && error.response) { | ||
setError("email", { message: error.response.data.message }); | ||
setIsEmailValidated(false); | ||
} | ||
}, | ||
}, | ||
); | ||
}; | ||
|
||
const code = watch("code"); | ||
useEffect(() => { | ||
setIsCodeValidated(false); | ||
}, [code]); | ||
|
||
const handleValidationCodeClick = () => { | ||
const isValid = code === codeState; | ||
|
||
setIsCodeValidated(isValid); | ||
if (code === codeState) { | ||
setIsCodeValidated(true); | ||
clearErrors("code"); | ||
} else { | ||
setIsCodeValidated(false); | ||
setError("code", { message: "인증번호를 다시 입력해주세요" }); | ||
} | ||
}; | ||
|
||
return ( | ||
<S.PasswordResetInputContainer> | ||
<InputField | ||
name="email" | ||
control={control} | ||
label="이메일" | ||
placeholder="이메일을 입력해주세요" | ||
rules={{ | ||
required: "이메일을 입력해주세요", | ||
pattern: { | ||
value: EMAIL_REGEX, | ||
message: "이메일 양식이 올바르지 않습니다", | ||
}, | ||
}} | ||
defaultValue="" | ||
onButtonClick={handleEmailValidateClick} | ||
buttonText={isEmailValidated ? "재요청" : "인증 요청"} | ||
buttonVariant={isEmailValidated ? "solid" : "outline"} | ||
buttonColor="percentOrange" | ||
color="black" | ||
/> | ||
|
||
<InputField | ||
type="number" | ||
name="code" | ||
control={control} | ||
label="인증번호 입력" | ||
placeholder="코드 6자리를 입력해주세요" | ||
rules={{ | ||
required: "인증번호를 입력해주세요", | ||
minLength: { | ||
value: 6, | ||
message: "6자리 이상 입력해주세요", | ||
}, | ||
}} | ||
defaultValue="" | ||
onButtonClick={handleValidationCodeClick} | ||
buttonText={isCodeValidated ? "인증 완료" : "인증 확인"} | ||
buttonVariant={isCodeValidated ? "solid" : "outline"} | ||
isSuccess={isCodeValidated} | ||
successMessage="인증이 완료되었습니다" | ||
buttonColor="percentOrange" | ||
color="black" | ||
/> | ||
|
||
<InputField | ||
type="password" | ||
name="password" | ||
control={control} | ||
label="비밀번호" | ||
placeholder="비빌번호를 설정해주세요" | ||
rules={{ | ||
required: "비빌번호를 설정해주세요", | ||
pattern: { | ||
value: /^(?=.*[a-zA-Z])(?=.*[!@#$%^*+=-])(?=.*[0-9]).{7,}$/, | ||
message: | ||
"영문, 숫자, 특수기호중 2개 이상을 포함해 8자 이상이여야 합니다.", | ||
}, | ||
}} | ||
defaultValue="" | ||
color="black" | ||
/> | ||
|
||
<InputField | ||
type="password" | ||
name="checkPassword" | ||
control={control} | ||
label="비밀번호 확인" | ||
placeholder="동일한 비밀번호를 입력해주세요" | ||
rules={{ | ||
required: "입력하신 비밀번호와 다릅니다.", | ||
validate: (checkPassword) => { | ||
return ( | ||
getValues("password") === checkPassword || | ||
"입력하신 비밀번호와 다릅니다." | ||
); | ||
}, | ||
}} | ||
defaultValue="" | ||
color="black" | ||
/> | ||
</S.PasswordResetInputContainer> | ||
); | ||
}; | ||
|
||
export default FieldValues; |
Oops, something went wrong.