Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {
- SELECTABLE_BUSINESS_PROMOTIONS_NEW_PAGE,
- SELECTABLE_BUSINESS_ONLINE_NEW_PAGE,
- SELECTABLE_PROMOTIONS_REMOVE,
- AVAILABLE_SELECTED_PROMOTIONS_NEW_PAGE,
- USED_SELECTED_PROMOTIONS_NEW_PAGE,
- DELETED_SELECTED_PROMOTIONS_NEW_PAGE,
- AVAILABLE_SELECTED_PROMOTIONS_REMOVE,
- DELETED_SELECTED_PROMOTIONS_REMOVE,
- ADD_PROMOTION_ADDRESS,
- ADD_PROMOTION_ADDRESS_SUCCESS,
- ADD_PROMOTION_ADDRESS_ERROR,
- } from '../constants/promotions';
- export const initialState = {
- selectableBusinessPromotions: [],
- selectableBusinessPromotionsMeta: {},
- selectableOnlinePromotions: [],
- selectableOnlinePromotionsMeta: {},
- availableSelectedPromotions: [],
- availableSelectedPromotionsMeta: {},
- usedSelectedPromotions: [],
- usedSelectedPromotionsMeta: {},
- deletedSelectedPromotions: [],
- deletedSelectedPromotionsMeta: {},
- isLoading: false,
- };
- const handleNewPromotionPage = (stateName, metaName) => (actionData, state) => {
- const { items, meta } = actionData;
- const { currentPage } = meta;
- let promotions;
- if (currentPage === 1) {
- promotions = [...items];
- } else {
- promotions = [...state[stateName], ...items];
- }
- return {
- ...state,
- [stateName]: promotions,
- [metaName]: meta,
- };
- };
- const promotionsReducer = (state = initialState, action) => {
- const { type: actionType, data: actionData } = action;
- switch (actionType) {
- case DELETED_SELECTED_PROMOTIONS_REMOVE: {
- const deletedSelectedPromotions = [
- ...state.deletedSelectedPromotions,
- ].filter(({ id }) => id !== actionData);
- return {
- ...state,
- deletedSelectedPromotions,
- };
- }
- case AVAILABLE_SELECTED_PROMOTIONS_REMOVE: {
- const availableSelectedPromotions = [
- ...state.availableSelectedPromotions,
- ].filter(({ id }) => id !== actionData);
- return {
- ...state,
- availableSelectedPromotions,
- };
- }
- case DELETED_SELECTED_PROMOTIONS_NEW_PAGE: {
- const { items, meta: deletedSelectedPromotionsMeta } = actionData;
- const { currentPage } = deletedSelectedPromotionsMeta;
- let deletedSelectedPromotions;
- if (currentPage === 1) {
- deletedSelectedPromotions = [...items];
- } else {
- deletedSelectedPromotions = [
- ...state.deletedSelectedPromotions,
- ...items,
- ];
- }
- return {
- ...state,
- deletedSelectedPromotions,
- deletedSelectedPromotionsMeta,
- };
- }
- case USED_SELECTED_PROMOTIONS_NEW_PAGE: {
- const { items, meta: usedSelectedPromotionsMeta } = actionData;
- const { currentPage } = usedSelectedPromotionsMeta;
- let usedSelectedPromotions;
- if (currentPage === 1) {
- usedSelectedPromotions = [...items];
- } else {
- usedSelectedPromotions = [...state.usedSelectedPromotions, ...items];
- }
- return {
- ...state,
- usedSelectedPromotions,
- usedSelectedPromotionsMeta,
- };
- }
- case AVAILABLE_SELECTED_PROMOTIONS_NEW_PAGE: {
- const { items, meta: availableSelectedPromotionsMeta } = actionData;
- const { currentPage } = availableSelectedPromotionsMeta;
- let availableSelectedPromotions;
- if (currentPage === 1) {
- availableSelectedPromotions = [...items];
- } else {
- availableSelectedPromotions = [
- ...state.availableSelectedPromotions,
- ...items,
- ];
- }
- return {
- ...state,
- availableSelectedPromotions,
- availableSelectedPromotionsMeta,
- };
- }
- case SELECTABLE_PROMOTIONS_REMOVE: {
- const selectableBusinessPromotions = [
- ...state.selectableBusinessPromotions,
- ].filter(({ id }) => id !== actionData);
- const selectableOnlinePromotions = [
- ...state.selectableOnlinePromotions,
- ].filter(({ id }) => id !== actionData);
- const selectableBusinessPromotionsMeta = {
- ...state.selectableBusinessPromotionsMeta,
- };
- if (
- selectableBusinessPromotions.length !==
- state.selectableBusinessPromotions.length
- ) {
- --selectableBusinessPromotionsMeta.totalItems;
- }
- const selectableOnlinePromotionsMeta = {
- ...state.selectableOnlinePromotionsMeta,
- };
- if (
- selectableOnlinePromotions.length !==
- state.selectableOnlinePromotions.length
- ) {
- --selectableOnlinePromotionsMeta.totalItems;
- }
- return {
- ...state,
- selectableOnlinePromotions,
- selectableOnlinePromotionsMeta,
- selectableBusinessPromotions,
- selectableBusinessPromotionsMeta,
- };
- }
- case SELECTABLE_BUSINESS_PROMOTIONS_NEW_PAGE: {
- return handleNewPromotionPage(
- 'selectableBusinessPromotions',
- 'selectableBusinessPromotionsMeta'
- )(actionData, state);
- }
- case SELECTABLE_BUSINESS_ONLINE_NEW_PAGE: {
- return handleNewPromotionPage(
- 'selectableOnlinePromotions',
- 'selectableOnlinePromotionsMeta'
- )(actionData, state);
- }
- case ADD_PROMOTION_ADDRESS:
- return { ...state, isLoading: true };
- case ADD_PROMOTION_ADDRESS_SUCCESS:
- return { ...state, isLoading: false };
- case ADD_PROMOTION_ADDRESS_ERROR:
- return { ...state, isLoading: false };
- default:
- return state;
- }
- };
- export default promotionsReducer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement