@@ -3,24 +3,27 @@ import { createPortal } from 'react-dom';
33
44// import styles of this component
55import styles from './Panel.module.css'
6+
67// import other component
78import UserCard from './UserCard/UserCard'
89import UserInformation from './UserInformation/UserInformation'
910import UserChangePassword from './UserChangePassword/UserChangePassword'
11+
1012// import other pkgs
1113import { UserEdit , Lock , ProfileCircle , Code1 } from "iconsax-react" ;
1214import { Row , Col , Container , Button } from 'react-bootstrap'
1315import PropTypes from 'prop-types'
1416
17+ // import utils
18+ import { getStorage , updateStorage } from './../../utils/storage' ;
19+
1520class 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 ) {
0 commit comments