vasylmartyniv

Untitled

Jun 8th, 2021
912
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.   SELECTABLE_BUSINESS_PROMOTIONS_NEW_PAGE,
  3.   SELECTABLE_BUSINESS_ONLINE_NEW_PAGE,
  4.   SELECTABLE_PROMOTIONS_REMOVE,
  5.   AVAILABLE_SELECTED_PROMOTIONS_NEW_PAGE,
  6.   USED_SELECTED_PROMOTIONS_NEW_PAGE,
  7.   DELETED_SELECTED_PROMOTIONS_NEW_PAGE,
  8.   AVAILABLE_SELECTED_PROMOTIONS_REMOVE,
  9.   DELETED_SELECTED_PROMOTIONS_REMOVE,
  10.   ADD_PROMOTION_ADDRESS,
  11.   ADD_PROMOTION_ADDRESS_SUCCESS,
  12.   ADD_PROMOTION_ADDRESS_ERROR,
  13. } from '../constants/promotions';
  14.  
  15. export const initialState = {
  16.   selectableBusinessPromotions: [],
  17.   selectableBusinessPromotionsMeta: {},
  18.   selectableOnlinePromotions: [],
  19.   selectableOnlinePromotionsMeta: {},
  20.   availableSelectedPromotions: [],
  21.   availableSelectedPromotionsMeta: {},
  22.   usedSelectedPromotions: [],
  23.   usedSelectedPromotionsMeta: {},
  24.   deletedSelectedPromotions: [],
  25.   deletedSelectedPromotionsMeta: {},
  26.   isLoading: false,
  27. };
  28.  
  29. const handleNewPromotionPage = (stateName, metaName) => (actionData, state) => {
  30.   const { items, meta } = actionData;
  31.   const { currentPage } = meta;
  32.   let promotions;
  33.   if (currentPage === 1) {
  34.     promotions = [...items];
  35.   } else {
  36.     promotions = [...state[stateName], ...items];
  37.   }
  38.  
  39.   return {
  40.     ...state,
  41.     [stateName]: promotions,
  42.     [metaName]: meta,
  43.   };
  44. };
  45.  
  46. const promotionsReducer = (state = initialState, action) => {
  47.   const { type: actionType, data: actionData } = action;
  48.   switch (actionType) {
  49.     case DELETED_SELECTED_PROMOTIONS_REMOVE: {
  50.       const deletedSelectedPromotions = [
  51.         ...state.deletedSelectedPromotions,
  52.       ].filter(({ id }) => id !== actionData);
  53.       return {
  54.         ...state,
  55.         deletedSelectedPromotions,
  56.       };
  57.     }
  58.     case AVAILABLE_SELECTED_PROMOTIONS_REMOVE: {
  59.       const availableSelectedPromotions = [
  60.         ...state.availableSelectedPromotions,
  61.       ].filter(({ id }) => id !== actionData);
  62.       return {
  63.         ...state,
  64.         availableSelectedPromotions,
  65.       };
  66.     }
  67.     case DELETED_SELECTED_PROMOTIONS_NEW_PAGE: {
  68.       const { items, meta: deletedSelectedPromotionsMeta } = actionData;
  69.       const { currentPage } = deletedSelectedPromotionsMeta;
  70.       let deletedSelectedPromotions;
  71.       if (currentPage === 1) {
  72.         deletedSelectedPromotions = [...items];
  73.       } else {
  74.         deletedSelectedPromotions = [
  75.           ...state.deletedSelectedPromotions,
  76.           ...items,
  77.         ];
  78.       }
  79.  
  80.       return {
  81.         ...state,
  82.         deletedSelectedPromotions,
  83.         deletedSelectedPromotionsMeta,
  84.       };
  85.     }
  86.     case USED_SELECTED_PROMOTIONS_NEW_PAGE: {
  87.       const { items, meta: usedSelectedPromotionsMeta } = actionData;
  88.       const { currentPage } = usedSelectedPromotionsMeta;
  89.       let usedSelectedPromotions;
  90.       if (currentPage === 1) {
  91.         usedSelectedPromotions = [...items];
  92.       } else {
  93.         usedSelectedPromotions = [...state.usedSelectedPromotions, ...items];
  94.       }
  95.  
  96.       return {
  97.         ...state,
  98.         usedSelectedPromotions,
  99.         usedSelectedPromotionsMeta,
  100.       };
  101.     }
  102.     case AVAILABLE_SELECTED_PROMOTIONS_NEW_PAGE: {
  103.       const { items, meta: availableSelectedPromotionsMeta } = actionData;
  104.       const { currentPage } = availableSelectedPromotionsMeta;
  105.       let availableSelectedPromotions;
  106.       if (currentPage === 1) {
  107.         availableSelectedPromotions = [...items];
  108.       } else {
  109.         availableSelectedPromotions = [
  110.           ...state.availableSelectedPromotions,
  111.           ...items,
  112.         ];
  113.       }
  114.  
  115.       return {
  116.         ...state,
  117.         availableSelectedPromotions,
  118.         availableSelectedPromotionsMeta,
  119.       };
  120.     }
  121.     case SELECTABLE_PROMOTIONS_REMOVE: {
  122.       const selectableBusinessPromotions = [
  123.         ...state.selectableBusinessPromotions,
  124.       ].filter(({ id }) => id !== actionData);
  125.       const selectableOnlinePromotions = [
  126.         ...state.selectableOnlinePromotions,
  127.       ].filter(({ id }) => id !== actionData);
  128.  
  129.       const selectableBusinessPromotionsMeta = {
  130.         ...state.selectableBusinessPromotionsMeta,
  131.       };
  132.       if (
  133.         selectableBusinessPromotions.length !==
  134.         state.selectableBusinessPromotions.length
  135.       ) {
  136.         --selectableBusinessPromotionsMeta.totalItems;
  137.       }
  138.  
  139.       const selectableOnlinePromotionsMeta = {
  140.         ...state.selectableOnlinePromotionsMeta,
  141.       };
  142.       if (
  143.         selectableOnlinePromotions.length !==
  144.         state.selectableOnlinePromotions.length
  145.       ) {
  146.         --selectableOnlinePromotionsMeta.totalItems;
  147.       }
  148.  
  149.       return {
  150.         ...state,
  151.         selectableOnlinePromotions,
  152.         selectableOnlinePromotionsMeta,
  153.         selectableBusinessPromotions,
  154.         selectableBusinessPromotionsMeta,
  155.       };
  156.     }
  157.     case SELECTABLE_BUSINESS_PROMOTIONS_NEW_PAGE: {
  158.       return handleNewPromotionPage(
  159.         'selectableBusinessPromotions',
  160.         'selectableBusinessPromotionsMeta'
  161.       )(actionData, state);
  162.     }
  163.     case SELECTABLE_BUSINESS_ONLINE_NEW_PAGE: {
  164.       return handleNewPromotionPage(
  165.         'selectableOnlinePromotions',
  166.         'selectableOnlinePromotionsMeta'
  167.       )(actionData, state);
  168.     }
  169.  
  170.     case ADD_PROMOTION_ADDRESS:
  171.       return { ...state, isLoading: true };
  172.  
  173.     case ADD_PROMOTION_ADDRESS_SUCCESS:
  174.       return { ...state, isLoading: false };
  175.  
  176.     case ADD_PROMOTION_ADDRESS_ERROR:
  177.       return { ...state, isLoading: false };
  178.     default:
  179.       return state;
  180.   }
  181. };
  182.  
  183. export default promotionsReducer;
  184.  
RAW Paste Data