diff options
author | Eug-VS <eug-vs@keemail.me> | 2020-01-12 16:02:25 +0300 |
---|---|---|
committer | Eug-VS <eug-vs@keemail.me> | 2020-01-12 18:11:45 +0300 |
commit | 0de06228f4b5e482b6f73230210225b332f0a2ff (patch) | |
tree | fc73b4e0703e5d5b96290cfa22d71d57129d9c31 /src/pages/Profile/Registration | |
parent | 3ef3cb7f0b25a448e81e5b81738758f4545337cc (diff) | |
download | chrono-cube-ui-0de06228f4b5e482b6f73230210225b332f0a2ff.tar.gz |
Store user data in localStorage (optionally)
Diffstat (limited to 'src/pages/Profile/Registration')
-rw-r--r-- | src/pages/Profile/Registration/Registration.js | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/pages/Profile/Registration/Registration.js b/src/pages/Profile/Registration/Registration.js index 8853a7a..ce8384f 100644 --- a/src/pages/Profile/Registration/Registration.js +++ b/src/pages/Profile/Registration/Registration.js @@ -15,19 +15,32 @@ import {get, post} from "../../../requests"; const Registration = ({ setUser }) => { const [username, setUsername] = useState(''); + const [isRememberMe, setIsRememberMe] = useState(false); const handleChange = (event) => { setUsername(event.target.value); }; + const handleCheck = (event) => { + setIsRememberMe(event.target.checked); + }; + const handleSubmit = () => { post('users/', { username }) .then(response => { - setUser(response.data); + const user = response.data; + setUser(user); + if (isRememberMe) { + localStorage.setItem('userId', user.id); + } }) .catch(err => { get('users/').then(response => { - setUser(response.data.filter(user => user.username === username)[0]); + const user = response.data.filter(user => user.username === username)[0]; + setUser(user); + if (isRememberMe) { + localStorage.setItem('userId', user.id); + } }); }); }; @@ -47,7 +60,7 @@ const Registration = ({ setUser }) => { </Grid> <Grid item> <FormControlLabel - control={<Checkbox color="secondary" />} + control={<Checkbox color="secondary" onChange={handleCheck} />} label="Remember me" /> </Grid> @@ -57,7 +70,6 @@ const Registration = ({ setUser }) => { </Button> </Grid> </Grid> - </ContentSection> ); }; |