UNPKG

@shopgate/engage

Version:
6 lines 1.62 kB
import{logger}from'@shopgate/pwa-core/helpers';import PipelineRequest from'@shopgate/pwa-core/classes/PipelineRequest';import{mutable}from'@shopgate/pwa-common/helpers/redux';import{requestInventories,receiveInventories,errorInventories}from"../action-creators";import{SHOPGATE_STOREFRONT_GET_INVENTORIES}from"../constants";import{getPreferredLocation}from"../selectors";/** * Fetches location inventory for a list of products. * @param {string[]} productCodes The product IDs to fetch inventories for. * @param {string} [locationCode] A location code * @returns {Function} A redux thunk. */function fetchInventories(productCodes){var locationCode=arguments.length>1&&arguments[1]!==undefined?arguments[1]:null;return function(dispatch,getState){var _getPreferredLocation;if(!productCodes||!productCodes.length){return undefined;}var state=getState();var locationFilter=locationCode||((_getPreferredLocation=getPreferredLocation(state))===null||_getPreferredLocation===void 0?void 0:_getPreferredLocation.code);if(!locationFilter){return undefined;}var filters={products:productCodes.map(function(productCode){return{productCode:productCode,locationCode:locationFilter};})};dispatch(requestInventories(productCodes,filters.locationCodes));var request=new PipelineRequest(SHOPGATE_STOREFRONT_GET_INVENTORIES).setInput(filters).dispatch();request.then(function(result){dispatch(receiveInventories(result.productInventories));})["catch"](function(error){logger.error(error);dispatch(errorInventories(productCodes,error));});return request;};}/** @mixes {MutableFunction} */export default mutable(fetchInventories);