@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
26 lines • 3.81 kB
JavaScript
var _excluded=["useDefaultRequestForProductIds"];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 fetchHighlightProducts from"./fetchHighlightProducts";import fetchProducts from"./fetchProducts";import fetchProductsById from"./fetchProductsById";/**
* Dispatches other actions based on the query type.
* @param {number|string} type The query type.
* @param {string} value The value to use with the query.
* @param {Object} options Any additional options for requesting products.
* @param {string} [id=null] A unique id for the component that is using this action.
* @return {Function} A Redux Thunk
*/var fetchProductsByQuery=function fetchProductsByQuery(type,value){var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};var id=arguments.length>3&&arguments[3]!==undefined?arguments[3]:null;return function(dispatch){/**
* Remove all properties from the options which are just intended to be used inside this function
* and not supposed to be used for the actual products request.
*/var _options$useDefaultRe=options.useDefaultRequestForProductIds,useDefaultRequestForProductIds=_options$useDefaultRe===void 0?false:_options$useDefaultRe,sanitizedOptions=_objectWithoutProperties(options,_excluded);switch(type){// Product highlights
case 1:{var params=_extends({},sanitizedOptions);dispatch(fetchHighlightProducts(_extends({params:params},id&&{id:id})));break;}// Search phrase
case 2:case 3:{var _params=_extends({searchPhrase:value},sanitizedOptions);return dispatch(fetchProducts(_extends({params:_params},id&&{id:id},{includeFilters:false})));}// Product ID's
case 4:{/**
* By default the productIds query type bypasses the regular product request logic. It will
* just request the products that are not available in Redux yet.
* This can cause update issues in the UI, since selectors might not return fresh data when
* Redux changes.
* So when the "useDefaultRequestForProductIds" flag is active, the regular request system is
* used and whenever the fetch params change, new product data fill be fetched.
*
* ATTENTION: To make the system work completely, also the "getProductsResult" selector helper
* needs to be called with this parameter.
*/if(useDefaultRequestForProductIds){var _params2=_extends({productIds:value},sanitizedOptions);// Limit and offset are not fully supported for product requests with productId list
delete _params2.limit;delete _params2.offset;dispatch(fetchProducts(_extends({params:_params2},id&&{id:id},{includeFilters:false})));}else{dispatch(fetchProductsById(value,id));}break;}// Category
case 5:{var _params3=_extends({categoryId:value},sanitizedOptions);return dispatch(fetchProducts(_extends({params:_params3},id&&{id:id},{includeFilters:false})));}default:}return null;};};export default fetchProductsByQuery;