Last active
February 19, 2019 14:55
-
-
Save rbinksy/b617294ca18f98d0084dec52efcef2d1 to your computer and use it in GitHub Desktop.
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
export default function withAssetRequestsReducer(originalReducer, reducerName) { | |
const actionTypes = { | |
FETCH_START: `${reducerName}/FETCH_START`, | |
FETCH_SUCCESS: `${reducerName}/FETCH_SUCCESS`, | |
FETCH_FAILURE: `${reducerName}/FETCH_FAILURE`, | |
UPDATE_START: `${reducerName}/UPDATE_START`, | |
UPDATE_SUCCESS: `${reducerName}/UPDATE_SUCCESS`, | |
UPDATE_FAILURE: `${reducerName}/UPDATE_FAILURE`, | |
NEXT_PAGE: `${reducerName}/NEXT_PAGE`, | |
CLEAR: `${reducerName}/CLEAR` | |
}; | |
return function reducer(state, action) { | |
const { type, payload } = action; | |
switch (type) { | |
case actionTypes.FETCH_START: | |
return state | |
.set('loading', true) | |
.set('error', initialState.get('error')); | |
case actionTypes.FETCH_SUCCESS: | |
return state | |
.set('loading', false) | |
.set('error', initialState.get('error')) | |
.set('page', 1) | |
.setIn(['entities', 'ASSETREQUESTS'], payload); | |
case actionTypes.FETCH_FAILURE: | |
return state.set('loading', false).set('error', payload); | |
case actionTypes.CLEAR: | |
return state.setIn( | |
['entities', 'ASSETREQUESTS'], | |
initialState.getIn(['entities', 'ASSETREQUESTS']) | |
); | |
case actionTypes.UPDATE_START: | |
return state.set('updating', true); | |
case actionTypes.UPDATE_SUCCESS: | |
return state.updateIn(['entities', 'ASSETREQUESTS'], assetReqs => | |
assetReqs.mergeDeep(payload)); | |
case actionTypes.UPDATE_FAILURE: | |
return state.set('updating', false).set('updateError', action.payload); | |
case actionTypes.NEXT_PAGE: | |
return state.update('page', pagenumber => pagenumber + 1); | |
default: | |
return originalReducer(state, action); | |
} | |
}; | |
} | |
export function withAssetRequestsActions(originalActions = {}, reducerName) { | |
const types = { | |
FETCH_START: `${reducerName}/FETCH_START`, | |
UPDATE_START: `${reducerName}/UPDATE_START` | |
}; | |
return Object.assign( | |
{ | |
loadEntities: payload => ({ | |
type: types.FETCH_START, | |
payload | |
}), | |
updateEntities: payload => ({ | |
type: types.UPDATE_START, | |
payload | |
}) | |
}, | |
originalActions | |
); | |
} | |
export const getIsLoading = (state, path): boolean => | |
state.getIn([path, 'loading']); | |
export const getAssetRequests = createSelector( | |
(state, path) => state.getIn([path, 'entities', 'ASSETREQUESTS']), | |
assetReqs => | |
assetReqs.sort( | |
(a, b) => | |
sortByDateReversed('requiredBy')(a, b) | |
|| sortAlphabetic('productionId')(a, b) | |
) | |
); | |
/** ******************** */ | |
/** USAGE REDUCERS */ | |
enum InternationalActionTypes { | |
SOME_CUSTOM_ACTION = 'INTERNATIONAL/FETCH_START' | |
} | |
function internationalReducer(state, action) { | |
const { type, payload } = action; | |
switch (type) { | |
case InternationalActionTypes.SOME_CUSTOM_ACTION: | |
return state.setIn(['entities', 'ASSETREQUESTS'], payload); | |
default: | |
return state; | |
} | |
} | |
const actions = withAssetRequestActions({ | |
someCusctomAction: payload => ({ | |
type: InternationalActionTypes.SOME_CUSTOM_ACTION, | |
payload | |
}), | |
}, 'INTERNATIONAL') | |
export default withAssetRequestsReducer(internationalReducer, 'INTERNATIONAL'); | |
/** ******************** */ | |
/** USAGE SELECTORS */ | |
const mapStateToProps = state => ({ | |
assetReqs: getAssetRequests(state, 'international') | |
}); | |
/** ******************** */ | |
/** USAGE SAGAS */ | |
// TODO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/craigrich/b617294ca18f98d0084dec52efcef2d1#file-higher-order-reducer-tests-L114
We merge the two reducers together here