Skip to content

Commit 2ecadf9

Browse files
committed
refactor the App.js
1 parent 5d99441 commit 2ecadf9

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

src/Components/Panel/Panel.jsx

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@ import { createPortal } from 'react-dom';
33

44
// import styles of this component
55
import styles from './Panel.module.css'
6+
67
// import other component
78
import UserCard from './UserCard/UserCard'
89
import UserInformation from './UserInformation/UserInformation'
910
import UserChangePassword from './UserChangePassword/UserChangePassword'
11+
1012
// import other pkgs
1113
import { UserEdit, Lock, ProfileCircle, Code1 } from "iconsax-react";
1214
import { Row, Col, Container, Button } from 'react-bootstrap'
1315
import PropTypes from 'prop-types'
1416

17+
// import utils
18+
import { getStorage, updateStorage } from './../../utils/storage';
19+
1520
class Panel extends PureComponent {
1621
constructor(props) {
1722
super(props)
1823
this.myVerifyUser = this.getUserFromStorage()
1924

2025
this.state = {
21-
user: {
22-
...this.initState(this.myVerifyUser)
23-
},
26+
user: {...this.initState(this.myVerifyUser)},
2427
toggle: 'information',
2528
}
2629

@@ -61,46 +64,28 @@ class Panel extends PureComponent {
6164
}
6265

6366
getUserFromStorage() {
64-
const users = JSON.parse(localStorage.getItem('users'))
65-
const userId = localStorage.getItem("id")
67+
const users = getStorage('users')
68+
const userId = getStorage('id')
6669
const myVerifyUser = users.find(user => user.id === userId)
6770

6871
return myVerifyUser
6972
}
7073

7174
initState({ id, username, email, birthday, password, confirmPassword, isLogin, firstName, lastName }) {
72-
return ({
73-
id,
74-
username,
75-
email,
76-
birthday,
77-
password,
78-
firstName,
79-
lastName,
80-
confirmPassword,
81-
isLogin,
82-
})
83-
}
84-
85-
updateStorage() {
86-
const users = JSON.parse(localStorage.getItem('users'))
87-
const myVerifyUserIdx = users.findIndex(user => user.id === this.state.user.id)
88-
users.splice(myVerifyUserIdx, 1)
89-
users.push(this.state.user)
90-
localStorage.setItem('users', JSON.stringify(users))
75+
return ({ id, username, email, birthday, password, firstName, lastName, confirmPassword, isLogin, })
9176
}
9277

9378
logOut() {
9479
this.changeUserInformation(['isLogin'], [false])
9580
}
9681

9782
componentDidUpdate() {
98-
this.updateStorage()
83+
updateStorage(getStorage('users'), this.state.user)
9984
!this.state.user.isLogin && this.props.onLogOut()
10085
}
10186

10287
changeToggle(toggle) {
103-
this.setState({ toggle, })
88+
this.setState({ toggle })
10489
}
10590

10691
changeUserInformation(keyInfos, valInfos) {

src/utils/storage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ export const getStorage = storage => {
33
return JSON.parse(localStorage.getItem(storage))
44
}
55

6-
export const setUserId = id => localStorage.setItem('id', id)
6+
export const setUserId = id => localStorage.setItem('id', id)
77

88
export const setUserInStorage = (storage, value) => localStorage.setItem(storage, JSON.stringify([ ...value ]))
99

1010
export const updateStorage = (users, myUser, login) => {
1111
const myVerifyUserIdx = users.findIndex(user => user.id === myUser.id)
1212
users.splice(myVerifyUserIdx, 1)
13-
myUser.isLogin = login
13+
if (login) myUser.isLogin = login
1414
users.push(myUser)
1515
setUserInStorage('users', users)
1616
}

0 commit comments

Comments
 (0)