diff options
Diffstat (limited to 'src/CachingController.js')
-rw-r--r-- | src/CachingController.js | 70 |
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); |