@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
6 lines • 2.96 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}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{historyReplace}from'@shopgate/pwa-common/actions/router';import{mutable}from'@shopgate/pwa-common/helpers/redux';import fetchProductsByQuery from'@shopgate/pwa-common-commerce/product/actions/fetchProductsByQuery';import{getProductRoute}from'@shopgate/pwa-common-commerce/product/helpers';import{getSearchRoute}from'@shopgate/pwa-common-commerce/search/helpers';/**
* Handle product search. The thunk will return false if nothing was found or true if it processed
* the action as it should.
* @param {string} searchPhrase The search phrase.
* @return {Function} A redux thunk.
*/var handleSearch=function handleSearch(searchPhrase){return(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch){var _ref2,_ref2$products,products,_products2,first,productId;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_context.next=2;return dispatch(fetchProductsByQuery(2,searchPhrase));case 2:_ref2=_context.sent;_ref2$products=_ref2.products;products=_ref2$products===void 0?[]:_ref2$products;if(products.length){_context.next=7;break;}return _context.abrupt("return",false);case 7:if(products.length===1){_products2=_slicedToArray(products,1),first=_products2[0];productId=typeof first==='string'?first:first.id;dispatch(historyReplace({pathname:getProductRoute(productId)}));}else{dispatch(historyReplace({pathname:getSearchRoute(searchPhrase)}));}return _context.abrupt("return",true);case 9:case"end":return _context.stop();}},_callee);}));return function(_x){return _ref.apply(this,arguments);};}());};/** @mixes {MutableFunction} */export default mutable(handleSearch);