@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
12 lines • 3.52 kB
JavaScript
function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}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 uniq from'lodash/uniq';import pickBy from'lodash/pickBy';import{ENOTFOUND}from'@shopgate/pwa-core';import{SELECT_GLOBAL_LOCATION}from'@shopgate/engage/core';import{PRODUCT_LIFETIME,REQUEST_PRODUCTS,RECEIVE_PRODUCTS,ERROR_PRODUCTS,EXPIRE_PRODUCT_BY_ID,EXPIRE_PRODUCTS_BY_HASH,ERROR_PRODUCT}from"../constants";/**
* Stores a collection of products by the related hash of the request parameters.
* @param {Object} [state={}] The current state.
* @param {Object} action The current redux action.
* @return {Object} The new state.
*/export default function resultsByHash(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var action=arguments.length>1?arguments[1]:undefined;switch(action.type){case REQUEST_PRODUCTS:return _extends({},state,_defineProperty({},action.hash,_extends({},state[action.hash],{isFetching:true,expires:0})));case RECEIVE_PRODUCTS:{if(!action.hash){return state;}var products=state[action.hash].products;var nextProducts=action.products||[];/**
* If there are no previous products and no incoming products
* its set to empty array, otherwise it will be an array of the previous and the
* new products. Duplicates are removed.
*/var stateProducts=products||nextProducts.length?uniq([].concat(products||[],nextProducts.map(function(product){return product.id;}))):[];return _extends({},state,_defineProperty({},action.hash,_extends({},state[action.hash],{products:stateProducts,totalResultCount:typeof action.totalResultCount!=='undefined'?action.totalResultCount:null,isFetching:false,expires:action.cached?Date.now()+(action.cachedTime||PRODUCT_LIFETIME):0})));}/* Remove not found product from hash results */case ERROR_PRODUCT:if(action.errorCode===ENOTFOUND){return Object.keys(state).reduce(function(accumulator,hash){if(accumulator[hash].products&&accumulator[hash].products.includes(action.productId)){accumulator[hash]=_extends({},accumulator[hash],{products:accumulator[hash].products.filter(function(pId){return pId!==action.productId;})});}return accumulator;},_extends({},state));}return state;case EXPIRE_PRODUCTS_BY_HASH:{return _extends({},state,_defineProperty({},action.hash,_extends({},state[action.hash],{products:[],expires:0})));}case EXPIRE_PRODUCT_BY_ID:{var productIds=[].concat(action.productId);return Object.keys(state).reduce(function(accumulator,hash){if(accumulator[hash].products&&productIds.some(function(id){return accumulator[hash].products.includes(id);})){accumulator[hash]=_extends({},accumulator[hash],{expires:0},action.complete&&{products:accumulator[hash].products.filter(function(id){return!productIds.includes(id);})});}return accumulator;},_extends({},state));}case ERROR_PRODUCTS:return _extends({},state,_defineProperty({},action.hash,_extends({},state[action.hash],{isFetching:false})));case SELECT_GLOBAL_LOCATION:// Remove all hashes that are not bound to a location
// because stock information may change per location
return pickBy(state,function(value,key){return!JSON.parse(key).locationCodes;});default:return state;}}