From 3ef3cb7f0b25a448e81e5b81738758f4545337cc Mon Sep 17 00:00:00 2001 From: Eug-VS Date: Sun, 12 Jan 2020 15:35:46 +0300 Subject: Implement Registration component --- src/pages/Profile/Registration/Registration.js | 66 ++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/pages/Profile/Registration/Registration.js (limited to 'src/pages/Profile/Registration') diff --git a/src/pages/Profile/Registration/Registration.js b/src/pages/Profile/Registration/Registration.js new file mode 100644 index 0000000..8853a7a --- /dev/null +++ b/src/pages/Profile/Registration/Registration.js @@ -0,0 +1,66 @@ +import React, {useState} from 'react'; + +import { + TextField, + Button, + Checkbox, + FormControlLabel, + Grid, +} from "@material-ui/core"; + +import ContentSection from "../../../components/ContentSection/ContentSection"; +import {get, post} from "../../../requests"; + + +const Registration = ({ setUser }) => { + + const [username, setUsername] = useState(''); + + const handleChange = (event) => { + setUsername(event.target.value); + }; + + const handleSubmit = () => { + post('users/', { username }) + .then(response => { + setUser(response.data); + }) + .catch(err => { + get('users/').then(response => { + setUser(response.data.filter(user => user.username === username)[0]); + }); + }); + }; + + return ( + +

Choose yourself a username to track your progress and compete with others:

+ + + + + + } + label="Remember me" + /> + + + + + + +
+ ); +}; + + +export default Registration; -- cgit v1.2.3 From 0de06228f4b5e482b6f73230210225b332f0a2ff Mon Sep 17 00:00:00 2001 From: Eug-VS Date: Sun, 12 Jan 2020 16:02:25 +0300 Subject: Store user data in localStorage (optionally) --- src/pages/Profile/Registration/Registration.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/pages/Profile/Registration') 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 }) => { } + control={} label="Remember me" /> @@ -57,7 +70,6 @@ const Registration = ({ setUser }) => { - ); }; -- cgit v1.2.3 From fff9eae8022e1e3bb88884a47bfd4153b089f444 Mon Sep 17 00:00:00 2001 From: Eug-VS Date: Sun, 12 Jan 2020 18:04:57 +0300 Subject: Add Profile history, link Profile at Timer page --- src/pages/Profile/Registration/Registration.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'src/pages/Profile/Registration') diff --git a/src/pages/Profile/Registration/Registration.js b/src/pages/Profile/Registration/Registration.js index ce8384f..45c83bc 100644 --- a/src/pages/Profile/Registration/Registration.js +++ b/src/pages/Profile/Registration/Registration.js @@ -26,23 +26,25 @@ const Registration = ({ setUser }) => { }; const handleSubmit = () => { - post('users/', { username }) - .then(response => { - const user = response.data; - setUser(user); - if (isRememberMe) { - localStorage.setItem('userId', user.id); - } - }) - .catch(err => { - get('users/').then(response => { - const user = response.data.filter(user => user.username === username)[0]; + if (username !== '') { + post('users/', { username }) + .then(response => { + const user = response.data; setUser(user); if (isRememberMe) { localStorage.setItem('userId', user.id); } + }) + .catch(err => { + get('users/').then(response => { + const user = response.data.filter(user => user.username === username)[0]; + setUser(user); + if (isRememberMe) { + localStorage.setItem('userId', user.id); + } + }); }); - }); + } }; return ( -- cgit v1.2.3