From 7f1dc831265e0ba4f7f13a0e307daf28d91f8a90 Mon Sep 17 00:00:00 2001 From: Noah Loomans Date: Fri, 6 Jul 2018 21:01:51 +0200 Subject: client/schedule: Properly handle errors --- src/client/react/store/actions.js | 12 +++++++----- src/client/react/store/reducers.js | 17 +++++++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) (limited to 'src/client/react/store') 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: { -- cgit v1.1