@@ -7,9 +7,19 @@ import { Form, Row, Col, Button } from 'react-bootstrap';
77import { useFormik } from 'formik' ;
88import { object , string , date } from 'yup'
99
10- const UserInformation = ( { firstName, lastName, email, birthday, onChangeInfo } ) => {
10+ const UserInformation = ( { username , firstName, lastName, email, birthday, onChangeInfo } ) => {
1111 const [ submit , setSubmit ] = useState ( false )
1212
13+ const isNotIterateEmail = ( username , email ) => {
14+ const users = JSON . parse ( localStorage . getItem ( 'users' ) )
15+
16+ const [ isNotIterateUsername , isNotIterateEmail ] = users . map ( user => (
17+ user . username !== username && user . email === email
18+ ) )
19+
20+ return ( ( ! isNotIterateUsername && ! isNotIterateEmail ) || ( isNotIterateUsername && isNotIterateEmail ) ) ? true : false
21+ }
22+
1323 const formik = useFormik ( {
1424 initialValues : {
1525 firstName : firstName ? firstName : '' ,
@@ -26,27 +36,32 @@ const UserInformation = ({ firstName, lastName, email, birthday, onChangeInfo })
2636 . max ( '2022-05-22' , 'invalid birthday date' ) ,
2737 } ) ,
2838 onSubmit : ( { firstName, lastName, email, birthday } , { setFieldError } ) => {
29- if ( ! firstName && ! lastName ) {
30- onChangeInfo (
31- [ 'firstName' , 'lastName' , 'email' , 'birthday' ] ,
32- [ '' , '' , email , birthday ]
33- )
34- } else if ( ! firstName ) {
35- onChangeInfo (
36- [ 'firstName' , 'lastName' , 'email' , 'birthday' ] ,
37- [ '' , lastName , email , birthday ]
38- )
39- } else if ( ! lastName ) {
40- onChangeInfo (
41- [ 'firstName' , 'lastName' , 'email' , 'birthday' ] ,
42- [ firstName , '' , email , birthday ]
43- )
44- } else {
45- onChangeInfo (
46- [ 'firstName' , 'lastName' , 'email' , 'birthday' ] ,
47- [ firstName , lastName , email , birthday ]
48- )
49- }
39+ const boolIsIterateEmail = isNotIterateEmail ( username , email )
40+
41+ if ( boolIsIterateEmail ) {
42+ if ( ! firstName && ! lastName ) {
43+ onChangeInfo (
44+ [ 'firstName' , 'lastName' , 'email' , 'birthday' ] ,
45+ [ '' , '' , email , birthday ]
46+ )
47+ } else if ( ! firstName ) {
48+ onChangeInfo (
49+ [ 'firstName' , 'lastName' , 'email' , 'birthday' ] ,
50+ [ '' , lastName , email , birthday ]
51+ )
52+ } else if ( ! lastName ) {
53+ onChangeInfo (
54+ [ 'firstName' , 'lastName' , 'email' , 'birthday' ] ,
55+ [ firstName , '' , email , birthday ]
56+ )
57+ } else {
58+ onChangeInfo (
59+ [ 'firstName' , 'lastName' , 'email' , 'birthday' ] ,
60+ [ firstName , lastName , email , birthday ]
61+ )
62+ }
63+ } else
64+ setFieldError ( 'email' , "you can't choose this email" )
5065 }
5166 } )
5267
0 commit comments