Skip to content

Commit 7c658e8

Browse files
committed
add errors for choose iterate email
1 parent 07608f1 commit 7c658e8

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

src/Components/Panel/Panel.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ class Panel extends PureComponent {
9494
this.changeUserInformation(['isLogin'], [false])
9595
}
9696

97-
componentDidUpdate(prevProps, prevState) {
97+
componentDidUpdate() {
9898
this.updateStorage()
99-
!prevState.user.isLogin && this.props.onLogOut()
99+
!this.state.user.isLogin && this.props.onLogOut()
100100
}
101101

102102
changeToggle(toggle) {
@@ -139,6 +139,7 @@ class Panel extends PureComponent {
139139
<Col className={`${styles['panel-column']} bg-white border ms-5 p-5`}>
140140
{this.state.toggle === 'information' && (
141141
<UserInformation
142+
username={this.state.user.username}
142143
firstName={this.state.user.firstName}
143144
lastName={this.state.user.lastName}
144145
email={this.state.user.email}

src/Components/Panel/UserInformation/UserInformation.jsx

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@ import { Form, Row, Col, Button } from 'react-bootstrap';
77
import { useFormik } from 'formik';
88
import { 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

Comments
 (0)