|
1 | 1 | import { useJoinState } from '../../stores/joinStore';
|
| 2 | +import { isValidName, isValidFormatPhone, isValidEmail, isValidPassword, isValidPwCheck } from '@/utils/authUtils.ts'; |
| 3 | +import { useRef, useState } from 'react'; |
| 4 | +import useSignUp from '@/react-queries/useSignUp'; |
| 5 | +import { isAuthError } from '@supabase/supabase-js'; |
| 6 | +import { useNavigate } from 'react-router-dom'; |
| 7 | +import { Loading } from '@/pages'; |
| 8 | +import Dialog from '../Dialog'; |
| 9 | + |
| 10 | +interface DialogElement { |
| 11 | + openModal: () => void; |
| 12 | + closeModal: () => void; |
| 13 | +} |
| 14 | + |
| 15 | +const messages = { |
| 16 | + NAME_ISVAILD_ERROR: '이름은 두글자 이상 입력 가능합니다.', |
| 17 | + PHONE_ISVAILD_ERROR: '휴대폰 번호를 정확히 입력해주세요.', |
| 18 | + PWCHECK_ISVAILD_ERROR: '비밀번호 확인을 진행해주세요.', |
| 19 | + EMAIL_PASSWORD_ISVAILD_ERROR: '이메일 또는 비밀번호 형식이 잘못되었습니다.', |
| 20 | + TERMS_ISVAILD_ERROR: '개인정보 수집 및 이용약관에 동의해주세요.', |
| 21 | + AUTH_ERROR: '이미 가입한 계정이 있습니다. 계정을 확인해주세요.', |
| 22 | +}; |
2 | 23 |
|
3 | 24 | const JoinButton = () => {
|
4 |
| - const { name, phone, email, password, pwCheck, useTermsCheck, privacyTermsCheck } = useJoinState(); |
| 25 | + const { nickName, name, phone, email, password, pwCheck, useTermsCheck, privacyTermsCheck } = useJoinState(); |
| 26 | + const { mutate, isPending } = useSignUp(); |
| 27 | + const navigate = useNavigate(); |
| 28 | + const dialogRef = useRef<DialogElement | null>(null); |
| 29 | + const [dialogMessage, setDialogMessage] = useState(''); |
5 | 30 |
|
6 | 31 | const onClick = () => {
|
7 |
| - console.log('name: ', name); |
8 |
| - console.log('phone: ', phone); |
9 |
| - console.log('email: ', email); |
10 |
| - console.log('password: ', password); |
11 |
| - console.log('pwCheck: ', pwCheck); |
12 |
| - console.log('useTermsCheck: ', useTermsCheck); |
13 |
| - console.log('privacyTermsCheck: ', privacyTermsCheck); |
| 32 | + if (!isValidEmail(email) || !isValidPassword(password)) { |
| 33 | + setDialogMessage(messages.EMAIL_PASSWORD_ISVAILD_ERROR); |
| 34 | + dialogRef.current?.openModal(); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + if (!isValidName(name)) { |
| 39 | + setDialogMessage(messages.NAME_ISVAILD_ERROR); |
| 40 | + dialogRef.current?.openModal(); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (!isValidFormatPhone(phone)) { |
| 45 | + setDialogMessage(messages.PHONE_ISVAILD_ERROR); |
| 46 | + dialogRef.current?.openModal(); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (!isValidPwCheck(password, pwCheck)) { |
| 51 | + setDialogMessage(messages.PWCHECK_ISVAILD_ERROR); |
| 52 | + dialogRef.current?.openModal(); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (!useTermsCheck || !privacyTermsCheck) { |
| 57 | + setDialogMessage(messages.TERMS_ISVAILD_ERROR); |
| 58 | + dialogRef.current?.openModal(); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + mutate( |
| 63 | + { |
| 64 | + name, |
| 65 | + nickName, |
| 66 | + phone, |
| 67 | + email, |
| 68 | + password, |
| 69 | + pwCheck: '', |
| 70 | + useTermsCheck: false, |
| 71 | + privacyTermsCheck: false, |
| 72 | + }, |
| 73 | + { |
| 74 | + onError: (error) => { |
| 75 | + if (isAuthError(error)) { |
| 76 | + setDialogMessage(messages.AUTH_ERROR); |
| 77 | + dialogRef.current?.openModal(); |
| 78 | + } |
| 79 | + }, |
| 80 | + onSuccess: () => { |
| 81 | + navigate('/*'); |
| 82 | + }, |
| 83 | + }, |
| 84 | + ); |
14 | 85 | };
|
15 | 86 |
|
16 | 87 | return (
|
17 |
| - <button type={'submit'} onClick={onClick} className="btn btn-outline btn-primary w-full"> |
18 |
| - 회원가입 하기 |
19 |
| - </button> |
| 88 | + <> |
| 89 | + <button type={'submit'} onClick={onClick} className="btn btn-outline btn-primary w-full"> |
| 90 | + 회원가입 하기 |
| 91 | + </button> |
| 92 | + <Dialog ref={dialogRef} desc={dialogMessage}></Dialog> |
| 93 | + {isPending && <Loading size={'lg'} color={'primary'} display={'spinner'} />} |
| 94 | + </> |
20 | 95 | );
|
21 | 96 | };
|
22 | 97 |
|
|
0 commit comments