UNPKG

@shopgate/engage

Version:
42 lines (41 loc) 1.14 kB
import { produce } from 'immer'; import { REQUEST_ORDER_HISTORY, RECEIVE_ORDER_HISTORY, ERROR_ORDER_HISTORY, CLEAR_ORDERS } from "../constants"; const defaultState = { orders: [] }; /** * Stores orders by the order number. * @param {Object} [state={}] The current state. * @param {Object} action The action object. * @returns {Object} The new state. */ const orders = (state = defaultState, action = {}) => { if (action.type === CLEAR_ORDERS) { return defaultState; } const producer = produce(draft => { switch (action.type) { case REQUEST_ORDER_HISTORY: { draft.isFetching = true; break; } case RECEIVE_ORDER_HISTORY: { draft.isFetching = false; draft.orders = [].concat(draft.orders.slice(0, action.offset), action.orders.map(order => order.orderNumber)); draft.totalOrderCount = action.totalOrderCount; break; } case ERROR_ORDER_HISTORY: { draft.isFetching = false; break; } default: break; } }); return producer(state); }; export default orders;