aboutsummaryrefslogtreecommitdiff
path: root/src/store/session.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/store/session.js')
-rw-r--r--src/store/session.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/store/session.js b/src/store/session.js
new file mode 100644
index 00000000..bfe94a41
--- /dev/null
+++ b/src/store/session.js
@@ -0,0 +1,53 @@
+import { createSlice } from '@reduxjs/toolkit';
+
+const { reducer, actions } = createSlice({
+ name: 'session',
+ initialState: {
+ server: null,
+ user: null,
+ socket: null,
+ includeLogs: false,
+ logs: [],
+ positions: {},
+ history: {},
+ },
+ reducers: {
+ updateServer(state, action) {
+ state.server = action.payload;
+ },
+ updateUser(state, action) {
+ state.user = action.payload;
+ },
+ updateSocket(state, action) {
+ state.socket = action.payload;
+ },
+ enableLogs(state, action) {
+ state.includeLogs = action.payload;
+ if (!action.payload) {
+ state.logs = [];
+ }
+ },
+ updateLogs(state, action) {
+ state.logs.push(...action.payload);
+ },
+ updatePositions(state, action) {
+ const liveRoutes = state.user.attributes.mapLiveRoutes || state.server.attributes.mapLiveRoutes || 'none';
+ const liveRoutesLimit = state.user.attributes['web.liveRouteLength'] || state.server.attributes['web.liveRouteLength'] || 10;
+ action.payload.forEach((position) => {
+ state.positions[position.deviceId] = position;
+ if (liveRoutes !== 'none') {
+ const route = state.history[position.deviceId] || [];
+ const last = route.at(-1);
+ if (!last || (last[0] !== position.longitude && last[1] !== position.latitude)) {
+ state.history[position.deviceId] = [...route.slice(1 - liveRoutesLimit), [position.longitude, position.latitude]];
+ }
+ } else {
+ state.history = {};
+ }
+ });
+ },
+ },
+});
+
+export { actions as sessionActions };
+export { reducer as sessionReducer };