|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import Link from 'next/link'; |
| 4 | +import FormField from '@/components/auth/FormField'; |
| 5 | +import { SignupValues, useSignupForm } from './useSignupForm'; |
| 6 | +import { useRouter } from 'next/navigation'; |
| 7 | +import { signupWithEmail } from '@/components/auth/SignupForm/signup.api'; |
| 8 | + |
| 9 | +const toPayload = (v: SignupValues) => ({ |
| 10 | + nickname: v.nickname, |
| 11 | + email: v.email, |
| 12 | + password: v.password, |
| 13 | +}); |
| 14 | + |
| 15 | +const SignupForm = () => { |
| 16 | + const router = useRouter(); |
| 17 | + const { values, errors, register, showError, hasError, touchAll } = |
| 18 | + useSignupForm(); |
| 19 | + |
| 20 | + const onSubmit = async (e: React.FormEvent) => { |
| 21 | + e.preventDefault(); |
| 22 | + touchAll(); |
| 23 | + if (hasError()) return; |
| 24 | + |
| 25 | + try { |
| 26 | + await signupWithEmail(toPayload(values)); |
| 27 | + alert('회원가입 완료되었습니다.'); |
| 28 | + router.push('/login'); |
| 29 | + } catch (e: unknown) { |
| 30 | + const code = |
| 31 | + typeof e === 'object' && |
| 32 | + e && |
| 33 | + 'code' in e && |
| 34 | + typeof (e as { code: unknown }).code === 'string' |
| 35 | + ? (e as { code: string }).code |
| 36 | + : ''; |
| 37 | + |
| 38 | + if (code === 'auth/email-already-in-use') |
| 39 | + alert('이미 가입된 이메일입니다'); |
| 40 | + else alert('회원가입에 실패했습니다'); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <form onSubmit={onSubmit}> |
| 46 | + <div className="space-y-6"> |
| 47 | + <FormField |
| 48 | + id="nickname" |
| 49 | + label="닉네임" |
| 50 | + error={errors.nickname} |
| 51 | + showError={showError('nickname')} |
| 52 | + {...register('nickname')} |
| 53 | + /> |
| 54 | + <FormField |
| 55 | + id="email" |
| 56 | + label="이메일" |
| 57 | + type="email" |
| 58 | + error={errors.email} |
| 59 | + showError={showError('email')} |
| 60 | + {...register('email')} |
| 61 | + /> |
| 62 | + <FormField |
| 63 | + id="password" |
| 64 | + label="비밀번호" |
| 65 | + type="password" |
| 66 | + error={errors.password} |
| 67 | + showError={showError('password')} |
| 68 | + {...register('password')} |
| 69 | + /> |
| 70 | + <FormField |
| 71 | + id="passwordConfirm" |
| 72 | + label="비밀번호 확인" |
| 73 | + type="password" |
| 74 | + error={errors.passwordConfirm} |
| 75 | + showError={showError('passwordConfirm')} |
| 76 | + {...register('passwordConfirm')} |
| 77 | + /> |
| 78 | + </div> |
| 79 | + |
| 80 | + <button |
| 81 | + type="submit" |
| 82 | + className="bg-primary hover:bg-primary-hover active:bg-primary-active mt-6 h-12 w-full cursor-pointer rounded-lg text-base font-medium text-white transition-all duration-150 active:scale-[0.98]" |
| 83 | + > |
| 84 | + 가입하기 |
| 85 | + </button> |
| 86 | + |
| 87 | + <p className="text-text-sub mt-4 text-center text-sm"> |
| 88 | + 계정이 있으신가요? |
| 89 | + <Link |
| 90 | + className="text-primary ml-1 cursor-pointer font-medium" |
| 91 | + href="/login" |
| 92 | + > |
| 93 | + 로그인 |
| 94 | + </Link> |
| 95 | + </p> |
| 96 | + </form> |
| 97 | + ); |
| 98 | +}; |
| 99 | + |
| 100 | +export default SignupForm; |
0 commit comments