diff options
author | Noah Loomans <noahloomans@gmail.com> | 2018-07-06 21:01:51 +0200 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2018-07-06 21:01:59 +0200 |
commit | 7f1dc831265e0ba4f7f13a0e307daf28d91f8a90 (patch) | |
tree | c330848bf738aa90dee9e13ca1c10caa61d60b49 /src/client/react/store | |
parent | 74ef968a45e147069d044fbfde814886d7275aa3 (diff) |
client/schedule: Properly handle errors
Diffstat (limited to 'src/client/react/store')
-rw-r--r-- | src/client/react/store/actions.js | 12 | ||||
-rw-r--r-- | src/client/react/store/reducers.js | 17 |
2 files changed, 20 insertions, 9 deletions
diff --git a/src/client/react/store/actions.js b/src/client/react/store/actions.js index 2157030..131bcaa 100644 --- a/src/client/react/store/actions.js +++ b/src/client/react/store/actions.js @@ -5,6 +5,7 @@ import { selectUser, selectWeek, selectCurrentWeek } from './selectors'; import purifyWeek from '../lib/purifyWeek'; import withinRange from '../lib/withinRange'; import extractSchedule from '../lib/extractSchedule'; +import rejectIfBadStatus from '../lib/rejectIfBadStatus'; function updatePathname(pathname = '') { return (dispatch, getState) => { @@ -93,8 +94,8 @@ const fetchScheduleSuccess = (user, week, htmlStr) => ({ type: 'VIEW/FETCH_SCHEDULE_SUCCESS', user, week, htmlStr, }); -const fetchScheduleError = (user, week) => ({ - type: 'VIEW/FETCH_SCHEDULE_ERROR', user, week, +const fetchScheduleError = (user, week, statusCode) => ({ + type: 'VIEW/FETCH_SCHEDULE_ERROR', user, week, statusCode, }); @@ -110,17 +111,18 @@ export function fetchScheduleIfNeeded(user, week) { dispatch(fetchScheduleStart(user, week)); fetch(`/get/${user}?week=${week}`) + .then(rejectIfBadStatus) .then(r => r.text()) .then( - // success + // success (htmlStr) => { dispatch(fetchScheduleSuccess(user, week, htmlStr)); }, // error - () => { + (statusCode) => { // TODO: Handle error status - dispatch(fetchScheduleError(user, week)); + dispatch(fetchScheduleError(user, week, statusCode)); }, ); }; diff --git a/src/client/react/store/reducers.js b/src/client/react/store/reducers.js index 3e39096..b1af46f 100644 --- a/src/client/react/store/reducers.js +++ b/src/client/react/store/reducers.js @@ -30,17 +30,25 @@ const DEFAULT_STATE = { const schedule = (state = {}, action) => { switch (action.type) { - case 'VIEW/FETCH_SCHEDULE_START': + case 'VIEW/FETCH_SCHEDULE_START': { return { - ...state, state: 'FETCHING', }; - case 'VIEW/FETCH_SCHEDULE_SUCCESS': + } + case 'VIEW/FETCH_SCHEDULE_SUCCESS': { return { - ...state, state: 'FINISHED', htmlStr: action.htmlStr, }; + } + case 'VIEW/FETCH_SCHEDULE_ERROR': { + const { statusCode } = action; + + return { + state: 'ERROR', + statusCode, + }; + } default: return state; } @@ -105,6 +113,7 @@ function reducer(state = DEFAULT_STATE, action) { case 'VIEW/FETCH_SCHEDULE_START': case 'VIEW/FETCH_SCHEDULE_SUCCESS': + case 'VIEW/FETCH_SCHEDULE_ERROR': return { ...state, schedules: { |