Skip to content

Instantly share code, notes, and snippets.

@feltnerm
Created September 7, 2016 03:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feltnerm/27117572e478844aeb24c14d7b008414 to your computer and use it in GitHub Desktop.
Save feltnerm/27117572e478844aeb24c14d7b008414 to your computer and use it in GitHub Desktop.
automatically store the state (pending, rejected, fulfilled) of promissory actions in a place in the redux store.
import { handleActions } from 'redux-actions'
import methods from 'actions' // `methods` are all the UPPERCASE_STRING constants defining action strings.
export const createProgressReducer = (options = {}) => {
const initialState = {}
const progressReducer = Object.keys(methods).reduce((accum, method) => {
if (typeof method === 'string') {
accum[`${method}_PENDING`] = (state, action) => {
return {
...state,
[method]: 'pending'
}
}
accum[`${method}_REJECTED`] = (state, action) => {
return {
...state,
[method]: 'rejected'
}
}
accum[`${method}_FULFILLED`] = (state, action) => {
return {
...state,
[method]: 'fulfilled'
}
}
}
return accum
}, {})
return handleActions(progressReducer, initialState)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment