|
| 1 | +import styles from './SigninForm.module.css'; |
| 2 | +import { ChangeEvent, FocusEvent, FormEvent, useEffect, useState } from 'react'; |
| 3 | +import { useRouter } from 'next/router'; |
| 4 | +import { useDispatch } from 'react-redux'; |
| 5 | +import { setUserInfo } from '@/redux/settingSlice'; |
| 6 | +import { AppDispatch } from '@/redux/store'; |
| 7 | +import AuthInput from '@/components/common/input/auth-input/AuthInput'; |
| 8 | +import CDSButton from '@/components/common/button/CDSButton'; |
| 9 | +import OverlayContainer from '@/components/common/modal/overlay-container/OverlayContainer'; |
| 10 | +import AuthModal from '@/components/common/modal/auth/AuthModal'; |
| 11 | +import { postSignin } from '@/lib/signin/postSignin'; |
| 12 | +import { ERROR_MESSAGE, PLACEHOLDER } from '@/constants/messages'; |
| 13 | +import { emailValidation, passwordValidation } from '@/utils/authValidation'; |
| 14 | + |
| 15 | +const INITIAL_VALUES = { |
| 16 | + email: '', |
| 17 | + password: '', |
| 18 | +}; |
| 19 | + |
| 20 | +function SigninForm() { |
| 21 | + const [values, setValues] = useState(INITIAL_VALUES); |
| 22 | + const [emailValid, setEmailValid] = useState(false); |
| 23 | + const [passwordValid, setPasswordValid] = useState(false); |
| 24 | + const [disabled, setDisabled] = useState(true); |
| 25 | + const [isModalVisible, setIsModalVisible] = useState(false); |
| 26 | + const [responseMessage, setResponseMessage] = useState(''); |
| 27 | + const router = useRouter(); |
| 28 | + const dispatch = useDispatch<AppDispatch>(); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + const isEmailValid = emailValidation(values.email); |
| 32 | + const isPasswordValid = passwordValidation(values.password); |
| 33 | + setDisabled(!(isEmailValid && isPasswordValid)); |
| 34 | + }, [values]); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (sessionStorage.getItem('accessToken')) { |
| 38 | + router.push('/mydashboard'); |
| 39 | + } |
| 40 | + }, []); |
| 41 | + |
| 42 | + const handleChange = (e: ChangeEvent<HTMLInputElement>) => { |
| 43 | + const { name, value } = e.target; |
| 44 | + |
| 45 | + setValues((prevValues) => ({ |
| 46 | + ...prevValues, |
| 47 | + [name]: value, |
| 48 | + })); |
| 49 | + }; |
| 50 | + |
| 51 | + const handleBlur = (e: FocusEvent<HTMLInputElement>) => { |
| 52 | + const { name, value } = e.target; |
| 53 | + |
| 54 | + if (name === 'email') { |
| 55 | + setEmailValid(!emailValidation(value)); |
| 56 | + } else if (name === 'password') { |
| 57 | + setPasswordValid(!passwordValidation(value)); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + const onSubmit = async (e: FormEvent<HTMLFormElement>) => { |
| 62 | + e.preventDefault(); |
| 63 | + |
| 64 | + try { |
| 65 | + const response = await postSignin(values); |
| 66 | + sessionStorage.setItem('accessToken', response.accessToken); |
| 67 | + |
| 68 | + // 리덕스 액션 호출 |
| 69 | + dispatch( |
| 70 | + setUserInfo({ |
| 71 | + user: response.user, |
| 72 | + }), |
| 73 | + ); |
| 74 | + |
| 75 | + router.push('/mydashboard'); |
| 76 | + } catch (error) { |
| 77 | + setResponseMessage(error.message); |
| 78 | + setIsModalVisible(true); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + const handleCancelClick = () => { |
| 83 | + setIsModalVisible(false); |
| 84 | + setResponseMessage(null); |
| 85 | + }; |
| 86 | + return ( |
| 87 | + <> |
| 88 | + <form onSubmit={onSubmit}> |
| 89 | + <AuthInput |
| 90 | + name="email" |
| 91 | + htmlFor="email" |
| 92 | + title="이메일" |
| 93 | + id="email" |
| 94 | + type="text" |
| 95 | + placeholder={PLACEHOLDER.EMAIL_PLACEHOLDER} |
| 96 | + value={values.email} |
| 97 | + onChange={handleChange} |
| 98 | + onBlur={handleBlur} |
| 99 | + error={emailValid} |
| 100 | + errorMessage={ERROR_MESSAGE.EMAIL_INVALID_FORMAT} |
| 101 | + autoComplete="email" |
| 102 | + /> |
| 103 | + |
| 104 | + <AuthInput |
| 105 | + name="password" |
| 106 | + htmlFor="password" |
| 107 | + title="비밀번호" |
| 108 | + id="password" |
| 109 | + type="password" |
| 110 | + placeholder="비밀번호를 입력해 주세요" |
| 111 | + value={values.password} |
| 112 | + onChange={handleChange} |
| 113 | + onBlur={handleBlur} |
| 114 | + error={passwordValid} |
| 115 | + errorMessage={ERROR_MESSAGE.PASSWORD_MIN_LENGTH} |
| 116 | + autoComplete="password" |
| 117 | + /> |
| 118 | + |
| 119 | + <div className={styles['login-button']}> |
| 120 | + <CDSButton btnType="auth" type="submit" disabled={disabled}> |
| 121 | + 로그인 |
| 122 | + </CDSButton> |
| 123 | + </div> |
| 124 | + </form> |
| 125 | + |
| 126 | + {isModalVisible && ( |
| 127 | + <OverlayContainer> |
| 128 | + <AuthModal |
| 129 | + message={responseMessage} |
| 130 | + handleCancelClick={handleCancelClick} |
| 131 | + /> |
| 132 | + </OverlayContainer> |
| 133 | + )} |
| 134 | + </> |
| 135 | + ); |
| 136 | +} |
| 137 | + |
| 138 | +export default SigninForm; |
0 commit comments