You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

loadingReducer.js 872B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import createReducer from "../../utils/createReducer";
  2. import {
  3. ADD_LOADER,
  4. REMOVE_LOADER,
  5. } from "../../actions/app/appActionConstants";
  6. const initialState = {
  7. loaderCount: 0,
  8. };
  9. export default createReducer(
  10. {
  11. [ADD_LOADER]: addLoader,
  12. [REMOVE_LOADER]: removeLoader,
  13. },
  14. initialState
  15. );
  16. function addLoader(state, action) {
  17. let loaderCount = state.loaderCount;
  18. let actionType = action.payload.replace("[FETCH]", "");
  19. if (!state[actionType]) {
  20. loaderCount++;
  21. }
  22. return {
  23. ...state,
  24. [actionType]: true,
  25. loaderCount,
  26. };
  27. }
  28. function removeLoader(state, action) {
  29. let actionType = action.payload
  30. .replace("[SUCCESS]", "")
  31. .replace("[ERROR]", "");
  32. let loaderCount = state.loaderCount;
  33. if (state[actionType] === true) {
  34. loaderCount--;
  35. }
  36. return {
  37. ...state,
  38. [actionType]: false,
  39. loaderCount,
  40. };
  41. }