traccar-web/src/CachingController.js
2024-04-06 09:22:10 -07:00

71 lines
1.9 KiB
JavaScript

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);