@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
7 lines • 5.47 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";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 asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}import{LoadingProvider}from'@shopgate/pwa-common/providers';import{ACTION_PUSH,ACTION_REPLACE}from'@virtuous/conductor';import{buildFetchSearchResultsParams,getProductsResult}from'@shopgate/engage/product';import{hex2bin,router}from'@shopgate/engage/core';import{CATEGORY_ALL_PATTERN,fetchCategory,getShowAllProductsFilters}from'@shopgate/engage/category';import{getCurrentRoute}from'@shopgate/pwa-common/selectors/router';import expireProductsByHash from'@shopgate/pwa-common-commerce/product/action-creators/expireProductsByHash';import fetchSearchResults from'@shopgate/pwa-common-commerce/search/actions/fetchSearchResults';import fetchFilters from'@shopgate/pwa-common-commerce/filter/actions/fetchFilters';import{buildFilterParamsForFetchFiltersRequest}from'@shopgate/engage/filter/helpers';import{categoryAllWillEnter$,categoryAllFiltersDidUpdateFromFilterPage$}from'@shopgate/pwa-common-commerce/category/streams';import{searchWillEnter$,searchFiltersDidUpdateFromFilterPage$,searchRequesting$,searchReceived$,searchProductsNeedUpdate$}from"../streams";import{SEARCH_PATTERN}from"../constants";var searchBasedRouteWillEnter$=searchWillEnter$.merge(categoryAllWillEnter$);var searchBasedFiltersDidUpdateFromFilterPage$=searchFiltersDidUpdateFromFilterPage$.merge(categoryAllFiltersDidUpdateFromFilterPage$);/**
* Search subscriptions.
* @param {Function} subscribe The subscribe function.
*/export default function search(subscribe){subscribe(searchRequesting$,function(){LoadingProvider.setLoading(SEARCH_PATTERN);});subscribe(searchReceived$,function(){LoadingProvider.unsetLoading(SEARCH_PATTERN);});subscribe(searchBasedRouteWillEnter$,/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref){var _action$route;var action,dispatch,getState,filters,searchPhrase,sort,category,_getProductsResult,hash,expired;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:action=_ref.action,dispatch=_ref.dispatch,getState=_ref.getState;if([ACTION_PUSH,ACTION_REPLACE].includes(action===null||action===void 0?void 0:action.historyAction)){_context.next=3;break;}return _context.abrupt("return");case 3:filters=action.route.state.filters;searchPhrase=action.route.query.s;sort=action.route.query.sort;if(!(action.route.pattern===CATEGORY_ALL_PATTERN)){_context.next=14;break;}searchPhrase='*';if(filters){_context.next=14;break;}_context.next=11;return dispatch(fetchCategory(hex2bin(action.route.params.categoryId)));case 11:category=_context.sent;filters=getShowAllProductsFilters(category);router.update(action.route.id,{filters:filters,categoryName:category.name});case 14:_getProductsResult=getProductsResult(getState(),_extends({searchPhrase:searchPhrase,routeId:action===null||action===void 0?void 0:(_action$route=action.route)===null||_action$route===void 0?void 0:_action$route.id},buildFetchSearchResultsParams())),hash=_getProductsResult.hash,expired=_getProductsResult.expired;if(expired){dispatch(expireProductsByHash(hash));}dispatch(fetchSearchResults(_extends({filters:filters,searchPhrase:searchPhrase,sort:sort},buildFetchSearchResultsParams())));dispatch(fetchFilters({filters:buildFilterParamsForFetchFiltersRequest(filters)}));case 18:case"end":return _context.stop();}},_callee);}));return function(_x){return _ref2.apply(this,arguments);};}());// No logic related to "show all products" necessary here, since stream only emits when
// location based shopping is active. That's currently only supported when new services are
// used, but the new services don't support "show all products".
subscribe(searchProductsNeedUpdate$,function(_ref3){var dispatch=_ref3.dispatch,getState=_ref3.getState;var _getCurrentRoute=getCurrentRoute(getState()),query=_getCurrentRoute.query,filters=_getCurrentRoute.state.filters,pattern=_getCurrentRoute.pattern;var sort=query.sort;var searchPhrase=query.s;if(pattern===CATEGORY_ALL_PATTERN){searchPhrase='*';}dispatch(fetchSearchResults(_extends({filters:filters,searchPhrase:searchPhrase,sort:sort},buildFetchSearchResultsParams())));});subscribe(searchBasedFiltersDidUpdateFromFilterPage$,function(_ref4){var dispatch=_ref4.dispatch,action=_ref4.action;var filters=action.route.state.filters;var pattern=action.route.pattern;var sort=action.route.query.sort;var searchPhrase=action.route.query.s;if(pattern===CATEGORY_ALL_PATTERN){searchPhrase='*';}dispatch(fetchSearchResults(_extends({filters:filters,searchPhrase:searchPhrase,sort:sort},buildFetchSearchResultsParams())));});}