diff options
Diffstat (limited to 'src/client/react/store')
-rw-r--r-- | src/client/react/store/actions.js | 42 | ||||
-rw-r--r-- | src/client/react/store/reducers.js | 4 |
2 files changed, 44 insertions, 2 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)); + }, + ); + }; +} diff --git a/src/client/react/store/reducers.js b/src/client/react/store/reducers.js index cd68d96..3e39096 100644 --- a/src/client/react/store/reducers.js +++ b/src/client/react/store/reducers.js @@ -30,7 +30,7 @@ const DEFAULT_STATE = { const schedule = (state = {}, action) => { switch (action.type) { - case 'VIEW/FETCH_SCHEDULE_REQUEST': + case 'VIEW/FETCH_SCHEDULE_START': return { ...state, state: 'FETCHING', @@ -103,7 +103,7 @@ function reducer(state = DEFAULT_STATE, action) { isRoomFinderVisible: false, }; - case 'VIEW/FETCH_SCHEDULE_REQUEST': + case 'VIEW/FETCH_SCHEDULE_START': case 'VIEW/FETCH_SCHEDULE_SUCCESS': return { ...state, |