Permalink
Browse files

Refactor reducer

  • Loading branch information...
MrLeebo committed Oct 12, 2017
1 parent 680255a commit bdcfde2e88055ab01f09553e6a64cfc13017202e
Showing with 16 additions and 11 deletions.
  1. +3 −3 example/package-lock.json
  2. +1 −1 example/package.json
  3. +12 −7 lib/reducer.js

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -18,7 +18,7 @@
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"redux-promise-middleware": "^4.4.1",
"redux-supermodel": "^0.17.0"
"redux-supermodel": "^0.18.0"
},
"scripts": {
"start": "react-scripts start",
View
@@ -78,6 +78,14 @@ function clearErrors (state, action) {
return { ...state, [resourceName]: { ...resource, error: null } }
}
const handlers = {
[types.RESET]: reset,
[types.PENDING]: pending,
[types.FULFILLED]: fulfilled,
[types.REJECTED]: rejected,
[types.CLEAR_ERRORS]: clearErrors
}
export default function reducer (state = initialState, action) {
const { type, meta } = action
if (!type || !type.startsWith(types.PREFIX)) return state
@@ -86,12 +94,9 @@ export default function reducer (state = initialState, action) {
throw new Error(`Invalid ${type}: Missing "meta.resourceName" property`)
}
switch (type) {
case types.RESET: return reset(state, action)
case types.PENDING: return pending(state, action)
case types.FULFILLED: return fulfilled(state, action)
case types.REJECTED: return rejected(state, action)
case types.CLEAR_ERRORS: return clearErrors(state, action)
default: throw new Error(`Unrecognized action type: ${type}`)
if (handlers[type]) {
return handlers[type](state, action)
} else {
throw new Error(`Unrecognized action type: ${type}`)
}
}

0 comments on commit bdcfde2

Please sign in to comment.