Last active
October 20, 2017 12:29
-
-
Save guilhermepontes/4f955980b307c86424a5b2e866a2b1a0 to your computer and use it in GitHub Desktop.
Redux Promise Middleware - Auto suffix the actions with: `_PENDING`, `_FULFILLED` and `_REJECTED`
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
/** | |
* This method auto add to a list of actions the suffixes | |
* provided by `redux-promise-middleware`. | |
* | |
* Example: | |
* | |
* export default createActionTypes(['GET_USER', 'CREATE_USER']) | |
* | |
* [object Object] { | |
* CREATE_USER: "CREATE_USER", | |
* CREATE_USER_FULFILLED: "CREATE_USER_FULFILLED", | |
* CREATE_USER_PENDING: "CREATE_USER_PENDING", | |
* CREATE_USER_REJECTED: "CREATE_USER_REJECTED", | |
* | |
* GET_USER: "GET_USER", | |
* GET_USER_FULFILLED: "GET_USER_FULFILLED", | |
* GET_USER_PENDING: "GET_USER_PENDING", | |
* GET_USER_REJECTED: "GET_USER_REJECTED" | |
* } | |
*/ | |
const defaultSuffixes = ['', '_PENDING', '_FULFILLED', '_REJECTED'] | |
const createActionTypes = (actions, suffixes = defaultSuffixes) => { | |
const addSuffixes = action => ( | |
suffixes.reduce((acc, suffix) => { | |
const name = `${action}${suffix}` | |
acc[name] = name | |
return acc | |
}, {}) | |
) | |
const actionTypes = actions.reduce((acc, item) => ({ | |
...acc, | |
...addSuffixes(item) | |
}), {}) | |
return Object.freeze(actionTypes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment