@shopgate/engage
Version:
Shopgate's ENGAGE library.
7 lines • 1.51 kB
JavaScript
function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import{produce}from'immer';import{REQUEST_ORDER_DETAILS,RECEIVE_ORDER_DETAILS,ERROR_ORDER_DETAILS,CLEAR_ORDERS}from"../constants";export var CACHE_TIME=60*60*1000;// 1 hr in milliseconds
var defaultState={};/**
* Stores orders by the order number.
* @param {Object} [state={}] The current state.
* @param {Object} action The action object.
* @returns {Object} The new state.
*/var ordersById=function ordersById(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:defaultState;var action=arguments.length>1?arguments[1]:undefined;/* eslint-disable no-param-reassign */var producer=produce(function(draft){switch(action.type){case REQUEST_ORDER_DETAILS:{if(action.orderId){draft[action.orderId]=_extends({},draft[action.orderId],{isFetching:true,expires:0});}break;}case RECEIVE_ORDER_DETAILS:{if(action.orderId){draft[action.orderId]=_extends({},draft[action.orderId],{order:action.order,isFetching:false,expires:Date.now()+CACHE_TIME});}break;}case ERROR_ORDER_DETAILS:{if(action.orderId){draft[action.orderId]={isFetching:false,expires:0};}break;}case CLEAR_ORDERS:{return defaultState;}default:break;}return undefined;});/* eslint-enable no-param-reassign */return producer(state);};export default ordersById;