aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/index.js13
-rw-r--r--src/pages/Profile/Profile.js11
-rw-r--r--src/pages/Profile/Registration/Registration.js20
3 files changed, 38 insertions, 6 deletions
diff --git a/src/index.js b/src/index.js
index 4db99cf..6563c60 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,4 +1,4 @@
-import React, { useState } from 'react';
+import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
import CssBaseline from '@material-ui/core/CssBaseline';
@@ -12,6 +12,8 @@ import Scoreboard from "./pages/Scoreboard/Scoreboard";
import Contribute from "./pages/Contribute/Contribute";
import Profile from "./pages/Profile/Profile";
+import { get } from "./requests";
+
const App = () => {
@@ -19,6 +21,15 @@ const App = () => {
const [user, setUser] = useState({ username: 'anonymous', id: null });
const [recentSolutions, setRecentSolutions] = useState([]);
+ useEffect(() => {
+ const userId = +localStorage.getItem('userId');
+ if (userId) {
+ get('users/').then(response => {
+ setUser(response.data.filter(user => user.id === +userId)[0]);
+ });
+ }
+ }, []);
+
const Page = ({ page }) => {
switch (page) {
case 'app':
diff --git a/src/pages/Profile/Profile.js b/src/pages/Profile/Profile.js
index f2c44ef..af90a56 100644
--- a/src/pages/Profile/Profile.js
+++ b/src/pages/Profile/Profile.js
@@ -2,6 +2,7 @@ import React from 'react';
import Window from "../../components/Window/Window";
import {
+ Button,
makeStyles,
} from "@material-ui/core";
import Registration from "./Registration/Registration";
@@ -18,13 +19,21 @@ const useStyles = makeStyles(theme => ({
const Profile = ({ user, setUser }) => {
const classes = useStyles();
+ const handleLogout = () => {
+ setUser({ username: 'anonymous', id: null });
+ localStorage.clear();
+ };
+
return (
<>
<Window type="primary">
<div className={classes.primary}>
{ user.id? (
<ContentSection sectionName={`Welcome back, ${user.username}`}>
-
+ <p> You can always log out from your account! </p>
+ <Button variant="contained" color="secondary" onClick={handleLogout}>
+ Logout
+ </Button>
</ContentSection>
): (
<Registration setUser={setUser} />
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>
);
};