Skip to content

Commit 6dd4ac7

Browse files
committed
refactor: 유효성 검사 로직 함수화 및 버튼 활성화 조건 개선
1 parent 214c0a8 commit 6dd4ac7

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/app/(non-header)/signup/components/SignupForm.tsx

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,23 @@ export default function SignupForm() {
6060
passwordConfirmation: '',
6161
});
6262

63-
const hasErrors = Object.values(errors).some((msg) => msg !== '');
64-
const isAllFilled = email && nickname && password && passwordConfirmation;
65-
const isDisabled = !isAllFilled || hasErrors;
63+
const validateAll = () => ({
64+
email: validateEmail(email),
65+
nickname: validateNickname(nickname),
66+
password: validatePassword(password),
67+
passwordConfirmation: validatePasswordConfirmation(
68+
passwordConfirmation,
69+
password,
70+
),
71+
});
72+
73+
const hasErrors = (errs: typeof errors) =>
74+
Object.values(errs).some((msg) => msg !== '');
75+
76+
const isAllFilled = () =>
77+
email && nickname && password && passwordConfirmation;
78+
79+
const disabled = !isAllFilled() || hasErrors(errors);
6680

6781
const [errorPopupOpen, setErrorPopupOpen] = useState(false);
6882
const [errorMessage, setErrorMessage] = useState('');
@@ -85,6 +99,22 @@ export default function SignupForm() {
8599
}
86100
}, [state, setUser]);
87101

102+
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
103+
e.preventDefault();
104+
105+
const newErrors = validateAll();
106+
setErrors(newErrors);
107+
108+
if (!isAllFilled() || hasErrors(newErrors)) return;
109+
110+
const formData = new FormData();
111+
formData.append('email', email);
112+
formData.append('nickname', nickname);
113+
formData.append('password', password);
114+
115+
formAction(formData);
116+
};
117+
88118
/**
89119
* 카카오 회원가입 버튼 클릭 시 실행되는 함수입니다.
90120
* 환경변수에서 리디렉션 URI와 클라이언트 ID를 읽어,
@@ -121,7 +151,7 @@ export default function SignupForm() {
121151
</Link>
122152
</div>
123153

124-
<form action={formAction}>
154+
<form onSubmit={handleSubmit}>
125155
<div className='flex flex-col gap-28'>
126156
<Input
127157
required
@@ -192,7 +222,7 @@ export default function SignupForm() {
192222
<Button
193223
variant='primary'
194224
className='h-48 rounded-md'
195-
disabled={isDisabled}
225+
disabled={disabled}
196226
type='submit'
197227
>
198228
회원가입 하기

0 commit comments

Comments
 (0)