UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

23 lines 3.75 kB
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{ITEMS_PER_LOAD}from'@shopgate/pwa-common/constants/DisplayOptions';import{mutable}from'@shopgate/pwa-common/helpers/redux';import{makeGetDefaultSortOrder}from'@shopgate/engage/filter/selectors';import fetchProducts from"../../product/actions/fetchProducts";import requestSearchResults from"../action-creators/requestSearchResults";import receiveSearchResults from"../action-creators/receiveSearchResults";import errorSearchResults from"../action-creators/errorSearchResults";var getDefaultSortOrder=makeGetDefaultSortOrder();/** * Retrieves products for a certain search query. * @param {Object} params The params for the search products to request. * @param {string} params.searchPhrase Search phrase for the request * @param {number} [params.offset=0] Offset for the request * @param {number} [params.limit=ITEMS_PER_LOAD] Limit for the request * @param {string} [params.sort=DEFAULT_SORT] Limit for the request * @param {Object} [params.filters = null] Filters object for the request * @param {Object} [params.params = null] Additional params for the fetchProducts pipeline request * @param {number} [params.cachedTime=null] Cache TTL in ms. * @param {boolean} [params.resolveCachedProducts=false] Whether to resolve with products even * when no actual request was done due to cached data. * @return {Function} The dispatched action. */var fetchSearchResults=function fetchSearchResults(params){return function(dispatch,getState){var defaultSortOrder=getDefaultSortOrder(getState(),{searchPhrase:params.searchPhrase});var _params$offset=params.offset,offset=_params$offset===void 0?0:_params$offset,searchPhrase=params.searchPhrase,_params$limit=params.limit,limit=_params$limit===void 0?ITEMS_PER_LOAD:_params$limit,_params$sort=params.sort,sort=_params$sort===void 0?defaultSortOrder:_params$sort,_params$filters=params.filters,filters=_params$filters===void 0?null:_params$filters,_params$params=params.params,searchParams=_params$params===void 0?null:_params$params,_params$cachedTime=params.cachedTime,cachedTime=_params$cachedTime===void 0?null:_params$cachedTime,_params$resolveCached=params.resolveCachedProducts,resolveCachedProducts=_params$resolveCached===void 0?false:_params$resolveCached;if(!searchPhrase){return;}var promise=dispatch(fetchProducts({cachedTime:cachedTime,params:_extends({searchPhrase:searchPhrase,offset:offset,limit:limit,sort:sort},searchParams),filters:filters,resolveCachedProducts:resolveCachedProducts,onBeforeDispatch:function onBeforeDispatch(){// Dispatch the request action before the related pipeline request is executed. dispatch(requestSearchResults(searchPhrase,offset));}}));/** * Whenever fetchProducts is able to deliver product data * - either via a request or from the cache - * it returns a promise which will be resolved with the response data. */if(promise instanceof Promise){promise.then(function(response){// Inspect the response object to determine, if it represents a search result, or an error. if(response&&response.products&&Array.isArray(response.products)){// Dispatch the receive action when the response contains valid data.s dispatch(receiveSearchResults(searchPhrase,offset,response));}else{// If no valid data is delivered within the response the error action is dispatched. dispatch(errorSearchResults(searchPhrase,offset));}});}// eslint-disable-next-line consistent-return return promise;};};/** @mixes {MutableFunction} */export default mutable(fetchSearchResults);