aboutsummaryrefslogtreecommitdiff
path: root/modern
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2021-04-26 22:56:40 -0700
committerGitHub <noreply@github.com>2021-04-26 22:56:40 -0700
commit17e1f8c7b67a36c4e9384b7c7b930b68c8bfd6f4 (patch)
treecc596678a30c0c50d1a99831e30839cd964ed501 /modern
parent047bf86480e2b3e215c13df2af0da456a664b507 (diff)
parent127a2628b61b0a151eb5225af7ecaf483f907665 (diff)
downloadtrackermap-web-17e1f8c7b67a36c4e9384b7c7b930b68c8bfd6f4.tar.gz
trackermap-web-17e1f8c7b67a36c4e9384b7c7b930b68c8bfd6f4.tar.bz2
trackermap-web-17e1f8c7b67a36c4e9384b7c7b930b68c8bfd6f4.zip
Merge pull request #844 from mail2bishnoi/stores
Created mostly used stores
Diffstat (limited to 'modern')
-rw-r--r--modern/src/CachingController.js29
-rw-r--r--modern/src/store/drivers.js16
-rw-r--r--modern/src/store/groups.js16
-rw-r--r--modern/src/store/index.js9
-rw-r--r--modern/src/store/maintenances.js16
5 files changed, 85 insertions, 1 deletions
diff --git a/modern/src/CachingController.js b/modern/src/CachingController.js
index fa3b9f6b..3f808de2 100644
--- a/modern/src/CachingController.js
+++ b/modern/src/CachingController.js
@@ -1,6 +1,6 @@
import { useDispatch, useSelector } from 'react-redux';
import { connect } from 'react-redux';
-import { geofencesActions } from './store';
+import { geofencesActions, groupsActions, driversActions, maintenancesActions } from './store';
import { useEffectAsync } from './reactHelper';
const CachingController = () => {
@@ -15,6 +15,33 @@ const CachingController = () => {
}
}
}, [authenticated]);
+
+ useEffectAsync(async () => {
+ if (authenticated) {
+ const response = await fetch('/api/groups');
+ if (response.ok) {
+ dispatch(groupsActions.update(await response.json()));
+ }
+ }
+ }, [authenticated]);
+
+ useEffectAsync(async () => {
+ if (authenticated) {
+ const response = await fetch('/api/drivers');
+ if (response.ok) {
+ dispatch(driversActions.update(await response.json()));
+ }
+ }
+ }, [authenticated]);
+
+ useEffectAsync(async () => {
+ if (authenticated) {
+ const response = await fetch('/api/maintenance');
+ if (response.ok) {
+ dispatch(maintenancesActions.update(await response.json()));
+ }
+ }
+ }, [authenticated]);
return null;
}
diff --git a/modern/src/store/drivers.js b/modern/src/store/drivers.js
new file mode 100644
index 00000000..63522d78
--- /dev/null
+++ b/modern/src/store/drivers.js
@@ -0,0 +1,16 @@
+import { createSlice } from '@reduxjs/toolkit';
+
+const { reducer, actions } = createSlice({
+ name: 'drivers',
+ initialState: {
+ items: {},
+ },
+ reducers: {
+ update(state, action) {
+ action.payload.forEach(item => state.items[item['id']] = item);
+ },
+ }
+});
+
+export { actions as driversActions };
+export { reducer as driversReducer };
diff --git a/modern/src/store/groups.js b/modern/src/store/groups.js
new file mode 100644
index 00000000..483323f3
--- /dev/null
+++ b/modern/src/store/groups.js
@@ -0,0 +1,16 @@
+import { createSlice } from '@reduxjs/toolkit';
+
+const { reducer, actions } = createSlice({
+ name: 'groups',
+ initialState: {
+ items: {},
+ },
+ reducers: {
+ update(state, action) {
+ action.payload.forEach(item => state.items[item['id']] = item);
+ },
+ }
+});
+
+export { actions as groupsActions };
+export { reducer as groupsReducer };
diff --git a/modern/src/store/index.js b/modern/src/store/index.js
index 1fddae99..6e2bb204 100644
--- a/modern/src/store/index.js
+++ b/modern/src/store/index.js
@@ -4,17 +4,26 @@ import { sessionReducer as session } from './session';
import { devicesReducer as devices } from './devices';
import { positionsReducer as positions } from './positions';
import { geofencesReducer as geofences } from './geofences';
+import { groupsReducer as groups } from './groups';
+import { driversReducer as drivers } from './drivers';
+import { maintenancesReducer as maintenances } from './maintenances';
const reducer = combineReducers({
session,
devices,
positions,
geofences,
+ groups,
+ drivers,
+ maintenances,
});
export { sessionActions } from './session';
export { devicesActions } from './devices';
export { positionsActions } from './positions';
export { geofencesActions } from './geofences';
+export { groupsActions } from './groups';
+export { driversActions } from './drivers';
+export { maintenancesActions } from './maintenances';
export default configureStore({ reducer });
diff --git a/modern/src/store/maintenances.js b/modern/src/store/maintenances.js
new file mode 100644
index 00000000..0813f6b6
--- /dev/null
+++ b/modern/src/store/maintenances.js
@@ -0,0 +1,16 @@
+import { createSlice } from '@reduxjs/toolkit';
+
+const { reducer, actions } = createSlice({
+ name: 'maintenances',
+ initialState: {
+ items: {},
+ },
+ reducers: {
+ update(state, action) {
+ action.payload.forEach(item => state.items[item['id']] = item);
+ },
+ }
+});
+
+export { actions as maintenancesActions };
+export { reducer as maintenancesReducer };