diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-07-06 18:36:55 +0200 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-07-06 18:36:55 +0200 |
commit | 74ef968a45e147069d044fbfde814886d7275aa3 (patch) | |
tree | 19cbf0296628ba371a289be7619c960e082be15f /src/client/react/store/actions.js | |
parent | 7bfb87a0beda7247f1edc687b34144859813758a (diff) |
client/View: Move API call to action
Diffstat (limited to 'src/client/react/store/actions.js')
-rw-r--r-- | src/client/react/store/actions.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/client/react/store/actions.js b/src/client/react/store/actions.js index e7ef91d..2157030 100644 --- a/src/client/react/store/actions.js +++ b/src/client/react/store/actions.js @@ -4,6 +4,7 @@ import users from '../users'; import { selectUser, selectWeek, selectCurrentWeek } from './selectors'; import purifyWeek from '../lib/purifyWeek'; import withinRange from '../lib/withinRange'; +import extractSchedule from '../lib/extractSchedule'; function updatePathname(pathname = '') { return (dispatch, getState) => { @@ -83,3 +84,44 @@ export function showRoomFinder() { dispatch({ type: 'ROOM_FINDER/SHOW' }); }; } + +const fetchScheduleStart = (user, week) => ({ + type: 'VIEW/FETCH_SCHEDULE_START', user, week, +}); + +const fetchScheduleSuccess = (user, week, htmlStr) => ({ + type: 'VIEW/FETCH_SCHEDULE_SUCCESS', user, week, htmlStr, +}); + +const fetchScheduleError = (user, week) => ({ + type: 'VIEW/FETCH_SCHEDULE_ERROR', user, week, +}); + + +export function fetchScheduleIfNeeded(user, week) { + return (dispatch, getState) => { + const { schedules } = getState(); + const schedule = extractSchedule(schedules, user, week); + + if (schedule.state !== 'NOT_REQUESTED') { + return; + } + + dispatch(fetchScheduleStart(user, week)); + + fetch(`/get/${user}?week=${week}`) + .then(r => r.text()) + .then( + // success + (htmlStr) => { + dispatch(fetchScheduleSuccess(user, week, htmlStr)); + }, + + // error + () => { + // TODO: Handle error status + dispatch(fetchScheduleError(user, week)); + }, + ); + }; +} |