menuItemsReducer.js 1.05 KB
import { FETCH_MENU_ITEMS_REQUEST, FETCH_MENU_ITEMS_SUCCESS, FETCH_MENU_ITEMS_FAIL, CLEAR_ERRORS } from "../constants/menuItemsConstants";

// Room details reducer.
export const menuItemsReducer =
  menu =>
  (state = { menuItems: [] }, action) => {
    const fmir = `${FETCH_MENU_ITEMS_REQUEST}_${menu.toUpperCase()}`;
    const fmis = `${FETCH_MENU_ITEMS_SUCCESS}_${menu.toUpperCase()}`;
    const fmif = `${FETCH_MENU_ITEMS_FAIL}_${menu.toUpperCase()}`;
    const ce = `${CLEAR_ERRORS}_${menu.toUpperCase()}`;
    switch (action.type) {
      case fmir:
        return {
          loading: true
        };

      case fmis:
        return {
          loading: false,
          totalCount: action.payload.meta.pagination.total,
          resultsPerPage: action.payload.meta.pagination.pageSize,
          menuItems: action.payload.data
        };

      case fmif:
        return {
          error: action.payload.error.message
        };

      case ce:
        return {
          ...state,
          error: null
        };

      default:
        return state;
    }
  };