Created
January 5, 2016 22:20
-
-
Save StevenLangbroek/6bba133be81511da75bd to your computer and use it in GitHub Desktop.
Reducer utility
This file contains hidden or 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
| export (reducerMap, initialState) => (state = initialState, action) => { | |
| const reducer = reducerMap[action.type]; | |
| return reducer ? reducer(state, action) : state; | |
| } |
This file contains hidden or 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 createReducer from '../utilities/createReducer'; | |
| import * as types from '../constants/TransactionTypes'; | |
| export const initialState = { | |
| $isLoading: false, | |
| $didFailFetching: false, | |
| $didLoad: false, | |
| userId: null, | |
| transactions: [], | |
| entities: { | |
| transactions: {} | |
| } | |
| }; | |
| export default createReducer({ | |
| [types.REQUEST_TRANSACTIONS]: (state, action) => ({ | |
| ...state, | |
| $isLoading: true | |
| }), | |
| [types.FAILURE_TRANSACTIONS]: (state, action) => ({ | |
| ...state, | |
| $isLoading: false, | |
| $didFailFetching: true | |
| }), | |
| [types.RECEIVE_TRANSACTIONS]: (state, { data: { transactions, user } }) => ({ | |
| ...state, | |
| userId: (user && user.id || null), | |
| $isLoading: false, | |
| $didLoad: true, | |
| transactions: union(pluck(transactions, 'subid'), state.transactions), | |
| entities: { | |
| transactions: merge({}, state.entities.transactions, indexBy(transactions, 'subid')) | |
| } | |
| }) | |
| }, initialState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment