fix: 에러 useState 단순화

This commit is contained in:
정기영 2024-08-07 12:42:20 +09:00
parent b8bdbfccf1
commit 7e63b1b866

View File

@ -14,19 +14,16 @@ export default function UserRegisterPage() {
const passwordConfirmRef = useRef(); const passwordConfirmRef = useRef();
const [userType, setUserType] = useState('STUDENT'); const [userType, setUserType] = useState('STUDENT');
const [passwordMatch, setPasswordMatch] = useState(true); const [error, setError] = useState('');
const [existingId, setExistingId] = useState(false);
const [existingEmail, setExistingEmail] = useState(false);
const { userRegister } = useAuth(); const { userRegister } = useAuth();
const handleSubmit = async (e) => { const handleSubmit = async (e) => {
e.preventDefault(); e.preventDefault();
const isPWMatch = passwordRef.current.value === passwordConfirmRef.current.value; const isPWMatch = passwordRef.current.value === passwordConfirmRef.current.value;
setExistingId(false); setError('');
setExistingEmail(false);
setPasswordMatch(isPWMatch);
if (!isPWMatch) { if (!isPWMatch) {
setError('pwNotMatch');
return; return;
} }
userRegister( userRegister(
@ -41,10 +38,10 @@ export default function UserRegisterPage() {
}) })
.catch((err) => { .catch((err) => {
if (err.response.data === '아이디가 중복 됐습니다.') { if (err.response.data === '아이디가 중복 됐습니다.') {
setExistingId(true); setError('existingId');
} }
if (err.response.data === '이메일이 중복 됐습니다.') { if (err.response.data === '이메일이 중복 됐습니다.') {
setExistingEmail(true); setError('existingEmail');
} }
}); });
}; };
@ -84,11 +81,9 @@ export default function UserRegisterPage() {
type="text" type="text"
id="ID" id="ID"
ref={idRef} ref={idRef}
hasError={existingId} hasError={error === 'existingId'}
> >
{existingId && ( {error === 'existingId' && <div>이미 존재하는 아이디입니다</div>}
<div className={`${styles.textBodyStrong} ${styles.dangerColor}`}>이미 존재하는 아이디입니다</div>
)}
</InputBox> </InputBox>
<InputBox <InputBox
title="이름" title="이름"
@ -101,11 +96,9 @@ export default function UserRegisterPage() {
type="email" type="email"
id="email" id="email"
ref={emailRef} ref={emailRef}
hasError={existingEmail} hasError={error === 'existingEmail'}
> >
{existingEmail && ( {error === 'existingEmail' && <div>이미 등록된 이메일입니다</div>}
<div className={`${styles.textBodyStrong} ${styles.dangerColor}`}>이미 등록된 이메일입니다</div>
)}
</InputBox> </InputBox>
<InputBox <InputBox
title="비밀번호" title="비밀번호"
@ -118,11 +111,9 @@ export default function UserRegisterPage() {
type="password" type="password"
id="passwordConfirm" id="passwordConfirm"
ref={passwordConfirmRef} ref={passwordConfirmRef}
hasError={!passwordMatch} hasError={error === 'pwNotMatch'}
> >
{!passwordMatch && ( {error === 'pwNotMatch' && <div>비밀번호가 일치하지 않습니다</div>}
<div className={`${styles.textBodyStrong} ${styles.dangerColor}`}>비밀번호가 일치하지 않습니다</div>
)}
</InputBox> </InputBox>
</AuthForm> </AuthForm>
</div> </div>