Guest User

Untitled

a guest
Mar 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import { get } from 'lodash';
  2.  
  3. export function reducerWrapper(featureName: string, initialState: any = {}) {
  4. // In this article, our featureName will be USER_INTERESTS
  5. return function baseReducer(state = initialState, action: IAction) {
  6. switch (action.type) {
  7. case `SUCCESS_${featureName}`: {
  8. // case `SUCCESS_USER_INTERESTS`
  9. let stateSlice = get(state, action.payload.path);
  10. // Check if state at path exists, otherwise error will be thrown when adding data
  11. if (stateSlice) {
  12. stateSlice.data = action.payload.data;
  13. stateSlice.status = 'SUCCESS';
  14. }
  15. return state;
  16. }
  17. case `FAILED_${featureName}`: {
  18. // case `FAILED_USER_INTERESTS`
  19. let stateSlice = get(state, action.payload.path);
  20. if (stateSlice) {
  21. stateSlice.status = 'FAILURE';
  22. }
  23. return state;
  24. }
  25. default:
  26. return state;
  27. }
  28. };
  29. }
Add Comment
Please, Sign In to add comment