UNPKG

@shopgate/engage

Version:
12 lines 2.89 kB
var _excluded=["postalCode","geolocation"];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);}function _objectWithoutProperties(source,excluded){if(source==null)return{};var target=_objectWithoutPropertiesLoose(source,excluded);var key,i;if(Object.getOwnPropertySymbols){var sourceSymbolKeys=Object.getOwnPropertySymbols(source);for(i=0;i<sourceSymbolKeys.length;i++){key=sourceSymbolKeys[i];if(excluded.indexOf(key)>=0)continue;if(!Object.prototype.propertyIsEnumerable.call(source,key))continue;target[key]=source[key];}}return target;}function _objectWithoutPropertiesLoose(source,excluded){if(source==null)return{};var target={};var sourceKeys=Object.keys(source);var key,i;for(i=0;i<sourceKeys.length;i++){key=sourceKeys[i];if(excluded.indexOf(key)>=0)continue;target[key]=source[key];}return target;}import PipelineRequest from'@shopgate/pwa-core/classes/PipelineRequest';import{mutable}from'@shopgate/pwa-common/helpers/redux';import{requestProductLocations,receiveProductLocations,errorProductLocations}from"../action-creators";import{SHOPGATE_STOREFRONT_GET_PRODUCT_LOCATIONS}from"../constants";/** * Returns a list of locations and item availability for a given product * @param {string} productCode The product ID to fetch locations for. * @param {Object} [params={}] Optional params for the location request. * @param {string} [params.postalCode] A postal code * @param {Object} [params.geolocation] A geolocation object * @param {string} params.geolocation.longitude Longitude to search by coordinates. Works only in * combination with latitude. * @param {string} params.geolocation.latitude Latitude to search by coordinates. Works only in * combination with longitude. * @returns {Function} A redux thunk. */function fetchProductLocations(productCode){var params=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return function(dispatch){var postalCode=params.postalCode,geolocation=params.geolocation,restParams=_objectWithoutProperties(params,_excluded);var filters=_extends({productCode:productCode},restParams);if(geolocation){filters.longitude=params.geolocation.longitude;filters.latitude=params.geolocation.latitude;}if(postalCode){filters.postalCode=params.postalCode;}dispatch(requestProductLocations(productCode,filters));var request=new PipelineRequest(SHOPGATE_STOREFRONT_GET_PRODUCT_LOCATIONS).setInput(filters).dispatch();request.then(function(result){dispatch(receiveProductLocations(productCode,filters,result.locations));})["catch"](function(error){dispatch(errorProductLocations(productCode,error.code));});return request;};}/** @mixes {MutableFunction} */export default mutable(fetchProductLocations);