aboutsummaryrefslogtreecommitdiff
path: root/modern/src/MainPage.js
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-06-06 15:38:59 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-06-06 15:38:59 -0700
commitf8643e6bd88c20cc13383a86f457d909cdd6ae6e (patch)
tree8a9ec30921684d822a4346907617d9bb94c12f80 /modern/src/MainPage.js
parentcc03e41dd7f9f401797f39eb08e7d4617d624a2f (diff)
downloadtrackermap-web-f8643e6bd88c20cc13383a86f457d909cdd6ae6e.tar.gz
trackermap-web-f8643e6bd88c20cc13383a86f457d909cdd6ae6e.tar.bz2
trackermap-web-f8643e6bd88c20cc13383a86f457d909cdd6ae6e.zip
Use proper session state
Diffstat (limited to 'modern/src/MainPage.js')
-rw-r--r--modern/src/MainPage.js29
1 files changed, 16 insertions, 13 deletions
diff --git a/modern/src/MainPage.js b/modern/src/MainPage.js
index f81d5e74..ebc20051 100644
--- a/modern/src/MainPage.js
+++ b/modern/src/MainPage.js
@@ -1,4 +1,6 @@
-import React, { useEffect, useState } from 'react';
+import React, { useEffect } from 'react';
+import { useDispatch, useSelector } from 'react-redux';
+import { sessionActions } from './store';
import { useHistory } from 'react-router-dom';
import { isWidthUp, makeStyles, withWidth } from '@material-ui/core';
import Drawer from '@material-ui/core/Drawer';
@@ -9,7 +11,6 @@ import MainMap from './MainMap';
import MainToobar from './MainToolbar';
import SocketController from './SocketController';
-
const useStyles = makeStyles(theme => ({
root: {
height: "100vh",
@@ -40,22 +41,24 @@ const useStyles = makeStyles(theme => ({
}));
const MainPage = ({ width }) => {
- const [loading, setLoading] = useState(!document.authenticated);
+ const dispatch = useDispatch();
+ const authenticated = useSelector(state => state.session.authenticated);
const classes = useStyles();
const history = useHistory();
useEffect(() => {
- fetch('/api/session').then(response => {
- if (response.ok) {
- document.authenticated = true;
- setLoading(false);
- } else {
- history.push('/login');
- }
- });
- }, [history]);
+ if (!authenticated) {
+ fetch('/api/session').then(response => {
+ if (response.ok) {
+ dispatch(sessionActions.authenticated(true));
+ } else {
+ history.push('/login');
+ }
+ });
+ }
+ }, [authenticated, history]);
- return loading ? (<div>Loading...</div>) : (
+ return !authenticated ? (<div>Loading...</div>) : (
<div className={classes.root}>
<SocketController />
<MainToobar />