aboutsummaryrefslogtreecommitdiff
path: root/modern/src/SocketController.js
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-07-25 12:36:19 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-07-25 12:36:19 -0700
commitd69bb2b2c3053c2c61e4e5d7029751debcfb0dd9 (patch)
treea8acdb87aea6c39ba1c0712186a2be1dadaff181 /modern/src/SocketController.js
parent94be29b98ef9ca509c38c2576dc56828a788937e (diff)
downloadetbsa-traccar-web-d69bb2b2c3053c2c61e4e5d7029751debcfb0dd9.tar.gz
etbsa-traccar-web-d69bb2b2c3053c2c61e4e5d7029751debcfb0dd9.tar.bz2
etbsa-traccar-web-d69bb2b2c3053c2c61e4e5d7029751debcfb0dd9.zip
Implement simple route report
Diffstat (limited to 'modern/src/SocketController.js')
-rw-r--r--modern/src/SocketController.js35
1 files changed, 24 insertions, 11 deletions
diff --git a/modern/src/SocketController.js b/modern/src/SocketController.js
index 13ff0a9..94be52a 100644
--- a/modern/src/SocketController.js
+++ b/modern/src/SocketController.js
@@ -1,7 +1,8 @@
import { useEffect } from 'react';
-import { useDispatch } from 'react-redux';
+import { useDispatch, useSelector } from 'react-redux';
import { connect } from 'react-redux';
-import { positionsActions, devicesActions } from './store';
+import { positionsActions, devicesActions, sessionActions } from './store';
+import { useHistory } from 'react-router-dom';
const displayNotifications = events => {
if ("Notification" in window) {
@@ -22,6 +23,8 @@ const displayNotifications = events => {
const SocketController = () => {
const dispatch = useDispatch();
+ const history = useHistory();
+ const authenticated = useSelector(state => state.session.authenticated);
const connectSocket = () => {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
@@ -46,15 +49,25 @@ const SocketController = () => {
}
useEffect(() => {
- fetch('/api/devices').then(response => {
- if (response.ok) {
- response.json().then(devices => {
- dispatch(devicesActions.update(devices));
- });
- }
- connectSocket();
- });
- }, []);
+ if (authenticated) {
+ fetch('/api/devices').then(response => {
+ if (response.ok) {
+ response.json().then(devices => {
+ dispatch(devicesActions.update(devices));
+ });
+ }
+ connectSocket();
+ });
+ } else {
+ fetch('/api/session').then(response => {
+ if (response.ok) {
+ dispatch(sessionActions.authenticated(true));
+ } else {
+ history.push('/login');
+ }
+ });
+ }
+ }, [authenticated]);
return null;
}