aboutsummaryrefslogtreecommitdiff
path: root/src/CachingController.js
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2024-04-06 09:22:10 -0700
committerAnton Tananaev <anton@traccar.org>2024-04-06 09:22:10 -0700
commitf418231b6b2f5e030a0d2dcc390c314602b1f740 (patch)
tree10326adf3792bc2697e06bb5f2b8ef2a8f7e55fe /src/CachingController.js
parentb392a4af78e01c8e0f50aad5468e9583675b24be (diff)
downloadtrackermap-web-f418231b6b2f5e030a0d2dcc390c314602b1f740.tar.gz
trackermap-web-f418231b6b2f5e030a0d2dcc390c314602b1f740.tar.bz2
trackermap-web-f418231b6b2f5e030a0d2dcc390c314602b1f740.zip
Move modern to the top
Diffstat (limited to 'src/CachingController.js')
-rw-r--r--src/CachingController.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/CachingController.js b/src/CachingController.js
new file mode 100644
index 00000000..b8e5fd90
--- /dev/null
+++ b/src/CachingController.js
@@ -0,0 +1,70 @@
+import { useDispatch, useSelector, connect } from 'react-redux';
+
+import {
+ geofencesActions, groupsActions, driversActions, maintenancesActions, calendarsActions,
+} from './store';
+import { useEffectAsync } from './reactHelper';
+
+const CachingController = () => {
+ const authenticated = useSelector((state) => !!state.session.user);
+ const dispatch = useDispatch();
+
+ useEffectAsync(async () => {
+ if (authenticated) {
+ const response = await fetch('/api/geofences');
+ if (response.ok) {
+ dispatch(geofencesActions.refresh(await response.json()));
+ } else {
+ throw Error(await response.text());
+ }
+ }
+ }, [authenticated]);
+
+ useEffectAsync(async () => {
+ if (authenticated) {
+ const response = await fetch('/api/groups');
+ if (response.ok) {
+ dispatch(groupsActions.refresh(await response.json()));
+ } else {
+ throw Error(await response.text());
+ }
+ }
+ }, [authenticated]);
+
+ useEffectAsync(async () => {
+ if (authenticated) {
+ const response = await fetch('/api/drivers');
+ if (response.ok) {
+ dispatch(driversActions.refresh(await response.json()));
+ } else {
+ throw Error(await response.text());
+ }
+ }
+ }, [authenticated]);
+
+ useEffectAsync(async () => {
+ if (authenticated) {
+ const response = await fetch('/api/maintenance');
+ if (response.ok) {
+ dispatch(maintenancesActions.refresh(await response.json()));
+ } else {
+ throw Error(await response.text());
+ }
+ }
+ }, [authenticated]);
+
+ useEffectAsync(async () => {
+ if (authenticated) {
+ const response = await fetch('/api/calendars');
+ if (response.ok) {
+ dispatch(calendarsActions.refresh(await response.json()));
+ } else {
+ throw Error(await response.text());
+ }
+ }
+ }, [authenticated]);
+
+ return null;
+};
+
+export default connect()(CachingController);