@@ -4,63 +4,62 @@ import { Text, PageWrapper, Input, Modal } from 'components';
44import { useNavigate } from 'react-router-dom' ;
55import { useState } from 'react' ;
66import theme from 'styles/theme' ;
7- import useDebounce from 'hooks/useDebounce' ;
87
98const MyInfoEditPage = ( ) => {
109 const { onChangePassword } = useUserContext ( ) ;
1110 const navigate = useNavigate ( ) ;
1211
1312 const [ password , setPassword ] = useState ( '' ) ;
14- const [ confirm , setConfirm ] = useState ( '' ) ;
1513 const [ passwordInvalid , setPasswordInvalid ] = useState ( false ) ;
1614 const [ confirmInvalid , setConfirmInvalid ] = useState ( false ) ;
1715 const [ errors , setErrors ] = useState ( { } ) ;
1816 const [ isModal , setIsModal ] = useState ( false ) ;
19- const [ value , setValue ] = useState ( '' ) ;
2017
2118 const onClose = ( ) => {
2219 setIsModal ( false ) ;
2320 } ;
2421
2522 const regex = / \S { 8 , 10 } / ;
2623
27- const validate = useDebounce (
28- ( ) => {
29- const newErrors = { } ;
30- if ( password && ! regex . test ( password ) ) {
31- newErrors . password = '! 8-10자 사이로 공백없이 입력해주세요' ;
32- setPasswordInvalid ( true ) ;
24+ const validate = ( e ) => {
25+ const { value, name } = e . target ;
26+
27+ const newErrors = { } ;
28+ if ( name === 'password' ) {
29+ if ( value . length > 10 ) {
30+ e . target . value = value . slice ( 0 , 10 ) ;
3331 }
34- if ( password . length > 10 ) {
32+ if ( ! regex . test ( value ) ) {
3533 newErrors . password = '! 8-10자 사이로 공백없이 입력해주세요' ;
36- setPassword ( password . slice ( 0 , 10 ) ) ;
3734 setPasswordInvalid ( true ) ;
3835 }
39- if ( confirm && password !== confirm ) {
36+ setPassword ( e . target . value ) ;
37+ }
38+ if ( name === 'confirm' ) {
39+ if ( value . length > 10 ) {
40+ e . target . value = value . slice ( 0 , 10 ) ;
41+ }
42+ if ( password !== e . target . value ) {
4043 newErrors . confirm = '! 비밀번호와 일치하지 않습니다.' ;
4144 setConfirmInvalid ( true ) ;
4245 }
43- if ( confirm . length > 10 ) {
44- setConfirm ( confirm . slice ( 0 , 10 ) ) ;
45- }
46- setErrors ( newErrors ) ;
47- ! newErrors . password && setPasswordInvalid ( false ) ;
48- ! newErrors . confirm && setConfirmInvalid ( false ) ;
49- } ,
50- 200 ,
51- [ password , confirm ] ,
52- ) ;
46+ }
47+
48+ setErrors ( newErrors ) ;
49+ ! newErrors . password && setPasswordInvalid ( false ) ;
50+ ! newErrors . confirm && setConfirmInvalid ( false ) ;
51+ } ;
5352
5453 const handleSubmit = ( e ) => {
5554 e . preventDefault ( ) ;
56- if ( password && confirm && ! passwordInvalid && ! confirmInvalid ) {
55+ if ( password && ! passwordInvalid && ! confirmInvalid ) {
5756 onChangePassword ( password ) ;
5857 setIsModal ( true ) ;
5958 }
6059 } ;
6160 return (
6261 < PageWrapper header prev complete onComplete = { handleSubmit } >
63- < UserContainter >
62+ < UserContainer >
6463 < UserInfo >
6564 < Text
6665 style = { {
@@ -74,18 +73,14 @@ const MyInfoEditPage = () => {
7473 >
7574 비밀번호를 설정해주세요
7675 </ Text >
77- < UserEditForm onSubmit = { handleSubmit } >
76+ < UserEditForm >
7877 < Input
7978 label = "변경할 비밀번호"
8079 style = { { marginTop : 5 } }
8180 type = "password"
8281 name = "password"
8382 inValid = { passwordInvalid }
84- value = { password }
85- onChange = { ( e ) => {
86- setPassword ( e . target . value ) ;
87- validate ( ) ;
88- } }
83+ onChange = { validate }
8984 > </ Input >
9085 { errors . password ? (
9186 < ErrorText > { errors . password } </ ErrorText >
@@ -100,11 +95,7 @@ const MyInfoEditPage = () => {
10095 type = "password"
10196 name = "confirm"
10297 inValid = { confirmInvalid }
103- value = { confirm }
104- onChange = { ( e ) => {
105- setConfirm ( e . target . value ) ;
106- validate ( ) ;
107- } }
98+ onChange = { validate }
10899 > </ Input >
109100 { errors . confirm && < ErrorText > { errors . confirm } </ ErrorText > }
110101 </ UserEditForm >
@@ -122,14 +113,14 @@ const MyInfoEditPage = () => {
122113 </ Modal . Button >
123114 </ Modal >
124115 ) }
125- </ UserContainter >
116+ </ UserContainer >
126117 </ PageWrapper >
127118 ) ;
128119} ;
129120
130121export default MyInfoEditPage ;
131122
132- const UserContainter = styled . div `
123+ const UserContainer = styled . div `
133124 width: 100%;
134125 height: 100%;
135126 position: relative;
0 commit comments