Created
September 7, 2016 03:25
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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