@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
10 lines • 1.91 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{getProductDataById}from'@shopgate/engage/product/selectors/product';import{shouldFetchData}from'@shopgate/pwa-common/helpers/redux';import fetchProducts from"./fetchProducts";/**
* Retrieves products by id from the store.
* @param {Array} productIds The product id's to request.
* @param {string} [componentId=null] A unique id for the component that is using this action.
* @param {boolean} [cached=true] cache result by hash
* @param {boolean} [includeFulfillment=true] includes fulfillment
* @return {Function} A Redux Thunk
*/var fetchProductsById=function fetchProductsById(productIds){var componentId=arguments.length>1&&arguments[1]!==undefined?arguments[1]:null;var cached=arguments.length>2&&arguments[2]!==undefined?arguments[2]:true;var includeFulfillment=arguments.length>3&&arguments[3]!==undefined?arguments[3]:true;return function(dispatch,getState){var state=getState();var products=state.product.productsById;// Filter out only the products that are not yet available in the store.
var missingIds=productIds.filter(function(id){return shouldFetchData(products[id]);});// Then only perform a pipeline request if there are products missing.
if(!missingIds.length){var productsById=productIds.map(function(id){return getProductDataById(state,{productId:id});});var totalProductCount=productsById.length;return{products:productsById,totalProductCount:totalProductCount};}return dispatch(fetchProducts(_extends({},componentId&&{id:componentId},{cached:cached,params:{productIds:missingIds},includeFulfillment:includeFulfillment,includeFilters:false,includeSort:false})));};};export default fetchProductsById;