UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

10 lines 1.96 kB
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{REVIEWS_LIFETIME,REQUEST_REVIEWS,RECEIVE_REVIEWS,ERROR_REVIEWS}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. */function reviewsByHash(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var action=arguments.length>1?arguments[1]:undefined;switch(action.type){case REQUEST_REVIEWS:return _extends({},state,_defineProperty({},action.hash,_extends({},state[action.hash],{isFetching:true,expires:0})));case RECEIVE_REVIEWS:{var reviews=state[action.hash].reviews||[];var nextReviews=action.reviews||[];/** * If there are no previous reviews and no incoming reviews * its set to empty array, otherwise it will be an array of the previous and the * new reviews. Duplicates are removed. */var stateReviews=reviews||nextReviews.length?uniq([].concat(reviews,nextReviews.map(function(review){return review.id;}))):[];return _extends({},state,_defineProperty({},action.hash,_extends({},state[action.hash],{reviews:stateReviews,totalReviewCount:action.totalReviewCount||null,isFetching:false,expires:Date.now()+REVIEWS_LIFETIME})));}case ERROR_REVIEWS:return _extends({},state,_defineProperty({},action.hash,_extends({},state[action.hash],{isFetching:false,expires:0})));default:return state;}}export default reviewsByHash;