@@ -4,38 +4,37 @@ import { type CustomError } from '@/api/axios.ts';
44import { type UserData , postLogin } from '@/api/user.ts' ;
55
66import { useAuthContext } from '@/hooks/useAuthContext.tsx' ;
7- import useForm , { type Validate } from '@/hooks/useForm' ;
7+ import useForm from '@/hooks/useForm' ;
88
99import { toast } from '@/components/Toast/index.ts' ;
1010import Button from '@/components/common/Button' ;
1111import Field from '@/components/common/Field' ;
1212import Icon from '@/components/common/Icon' ;
1313import Input from '@/components/common/Input' ;
1414
15+ import { lengthValidate } from '@/pages/LoginPage/validate.ts' ;
16+
17+ import { LOGIN_FAILED_MESSAGE } from '@/constants/user.ts' ;
18+ import type { LoginForm } from '@/type/user.ts' ;
1519import { useMutation } from '@tanstack/react-query' ;
1620import { AxiosResponse } from 'axios' ;
1721
18- type Form = {
19- id : string ;
20- password : string ;
21- } ;
22- type ResponseData = {
23- loginId : string ;
24- } ;
25-
26- const FAILED_MESSAGE = '아이디 또는 비밀번호가 일치하지 않습니다.' ;
2722export default function LoginPage ( ) {
23+ type ResponseData = {
24+ loginId : string ;
25+ } ;
26+
2827 const {
2928 handleSubmit,
3029 register,
3130 formState : { errors } ,
32- } = useForm < Form > ( ) ;
31+ } = useForm < LoginForm > ( ) ;
3332 const { login } = useAuthContext ( ) ;
3433 const navigation = useNavigate ( ) ;
3534 const { mutate, isPending, error } = useMutation < AxiosResponse < ResponseData > , CustomError , UserData > ( {
3635 mutationFn : postLogin ,
3736 onError : ( ) => {
38- toast . error ( `로그인 실패\n 사유 : ${ FAILED_MESSAGE } ` ) ;
37+ toast . error ( `로그인 실패\n 사유 : ${ LOGIN_FAILED_MESSAGE } ` ) ;
3938 } ,
4039 onSuccess : ( data ) => {
4140 const { loginId } = data . data ;
@@ -45,7 +44,7 @@ export default function LoginPage() {
4544 } ,
4645 } ) ;
4746
48- const submit = async ( data : Form ) => {
47+ const submit = async ( data : LoginForm ) => {
4948 const { id, password } = data ;
5049 mutate ( { loginId : id , loginPassword : password } ) ;
5150 } ;
@@ -59,7 +58,7 @@ export default function LoginPage() {
5958 < Field
6059 label = "Id"
6160 isValid = { ! errors . id && ! error }
62- errorMessage = { errors . id ? errors . id : FAILED_MESSAGE } >
61+ errorMessage = { errors . id ? errors . id : LOGIN_FAILED_MESSAGE } >
6362 < Input
6463 disabled = { isPending }
6564 { ...register ( 'id' , {
@@ -71,7 +70,7 @@ export default function LoginPage() {
7170 < Field
7271 label = "Password"
7372 isValid = { ! errors . password && ! error }
74- errorMessage = { errors . password ? errors . password : FAILED_MESSAGE } >
73+ errorMessage = { errors . password ? errors . password : LOGIN_FAILED_MESSAGE } >
7574 < Input
7675 type = "password"
7776 disabled = { isPending }
@@ -96,9 +95,3 @@ export default function LoginPage() {
9695 </ div >
9796 ) ;
9897}
99-
100- const lengthValidate : Validate < Form > = ( { value } ) => {
101- const isRightLength = value . length >= 4 && value . length <= 12 ;
102- if ( ! isRightLength ) return '최소 4자리, 최대 12자리 입니다.' ;
103- return null ;
104- } ;
0 commit comments