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/index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/index.js') 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': -- cgit v1.2.3