aboutsummaryrefslogtreecommitdiff
path: root/modern/src/SocketController.js
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2020-09-20 17:56:56 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2020-09-20 17:56:56 -0700
commitb2cd88bbbad82654057cec8aec7415a8caca667e (patch)
treec9d8485cdd5f2e4fd7558b963d095027c3913e86 /modern/src/SocketController.js
parentfc990dcc50650862c4f12a1a530b98bbd36ea4b8 (diff)
downloadtrackermap-web-b2cd88bbbad82654057cec8aec7415a8caca667e.tar.gz
trackermap-web-b2cd88bbbad82654057cec8aec7415a8caca667e.tar.bz2
trackermap-web-b2cd88bbbad82654057cec8aec7415a8caca667e.zip
Use async functions
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]);