Created
October 21, 2017 19:39
-
-
Save Tahseenm/b80c872bff9863c90690ef9075b84fad to your computer and use it in GitHub Desktop.
Redux Ducks module pattern
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
/** | |
* PATH: -> ./src/redux/modules/widgets.js | |
* | |
* [RULES] | |
* | |
* A module... | |
* | |
* - MUST export default a function called reducer() | |
* - MUST export its action creators as functions | |
* - MUST have action types in the form npm-module-or-app/reducer/ACTION_TYPE | |
* - MAY export its action types as UPPER_SNAKE_CASE, if an external reducer needs | |
* to listen for them, or if it is a published reusable library | |
*/ | |
import axios from 'axios' | |
/** | |
* ACTIONS | |
*/ | |
const LOAD = 'my-app/widgets/LOAD' | |
const CREATE = 'my-app/widgets/CREATE' | |
const UPDATE = 'my-app/widgets/UPDATE' | |
const REMOVE = 'my-app/widgets/REMOVE' | |
/** | |
* REDUCER | |
*/ | |
const reducer = (state = {}, action = {}) => { | |
switch (action.type) { | |
case LOAD: | |
// return new state | |
case CREATE: | |
// return new state | |
case UPDATE: | |
// return new state | |
case REMOVE: | |
// return new state | |
default: | |
return state | |
} | |
} | |
/** | |
* ACTION CREATORS | |
*/ | |
export const loadWidgets = () => ({ type: 'LOAD' }) | |
export const createWidget = widget => ({ widget, type: 'CREATE' }) | |
export const updateWidget = widget => ({ widget, type: 'UPDATE' }) | |
export const removeWidget = widget => ({ widget, type: 'REMOVE' }) | |
/* Causes Side effects */ | |
export const getWidget = () => dispatch => axios.get('/widget') | |
.then(widget => dispatch(updateWidget(widget))) | |
export default reducer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment