Skip to content

Instantly share code, notes, and snippets.

@StevenLangbroek
Created January 5, 2016 22:20
Show Gist options
  • Save StevenLangbroek/6bba133be81511da75bd to your computer and use it in GitHub Desktop.
Save StevenLangbroek/6bba133be81511da75bd to your computer and use it in GitHub Desktop.
Reducer utility
export (reducerMap, initialState) => (state = initialState, action) => {
const reducer = reducerMap[action.type];
return reducer ? reducer(state, action) : state;
}
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