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