aboutsummaryrefslogtreecommitdiff
path: root/modern/src/SocketController.js
diff options
context:
space:
mode:
Diffstat (limited to 'modern/src/SocketController.js')
-rw-r--r--modern/src/SocketController.js41
1 files changed, 24 insertions, 17 deletions
diff --git a/modern/src/SocketController.js b/modern/src/SocketController.js
index 94be52a6..787527cc 100644
--- a/modern/src/SocketController.js
+++ b/modern/src/SocketController.js
@@ -3,6 +3,7 @@ import { useDispatch, useSelector } from 'react-redux';
import { connect } from 'react-redux';
import { positionsActions, devicesActions, sessionActions } from './store';
import { useHistory } from 'react-router-dom';
+import { useEffectAsync } from './reactHelper';
const displayNotifications = events => {
if ("Notification" in window) {
@@ -24,7 +25,7 @@ const displayNotifications = events => {
const SocketController = () => {
const dispatch = useDispatch();
const history = useHistory();
- const authenticated = useSelector(state => state.session.authenticated);
+ const authenticated = useSelector(state => !!state.session.user);
const connectSocket = () => {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
@@ -48,24 +49,30 @@ const SocketController = () => {
};
}
- useEffect(() => {
+ useEffectAsync(async () => {
+ const response = await fetch('/api/server');
+ if (response.ok) {
+ const server = await response.json();
+ dispatch(sessionActions.updateServer(server));
+ }
+ }, []);
+
+ useEffectAsync(async () => {
if (authenticated) {
- fetch('/api/devices').then(response => {
- if (response.ok) {
- response.json().then(devices => {
- dispatch(devicesActions.update(devices));
- });
- }
- connectSocket();
- });
+ const response = await fetch('/api/devices');
+ if (response.ok) {
+ const devices = await response.json();
+ dispatch(devicesActions.update(devices));
+ }
+ connectSocket();
} else {
- fetch('/api/session').then(response => {
- if (response.ok) {
- dispatch(sessionActions.authenticated(true));
- } else {
- history.push('/login');
- }
- });
+ const response = await fetch('/api/session');
+ if (response.ok) {
+ const user = await response.json();
+ dispatch(sessionActions.updateUser(user));
+ } else {
+ history.push('/login');
+ }
}
}, [authenticated]);