UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

9 lines 4.07 kB
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 PipelineRequest from'@shopgate/pwa-core/classes/PipelineRequest';import{EFAVORITE,EUNKNOWN,EBIGAPI,ELIMIT}from'@shopgate/pwa-core/constants/Pipeline';import{shouldFetchData,mutable}from'@shopgate/pwa-common/helpers/redux';import{hasNewServices}from'@shopgate/engage/core/helpers';import{SHOPGATE_USER_GET_FAVORITES}from"../constants/Pipelines";import{receiveFavorites,requestFavorites,errorFetchFavorites}from"../action-creators";import{getHasMultipleFavoritesListsSupport,makeGetFavoritesProductsByList,getFavoritesDefaultList}from"../selectors";import receiveProducts from"../../product/action-creators/receiveProducts";/** * Fetch favorites action. * @param {boolean} ignoreCache Ignores cache when true * @param {string} listId The id of the list that needs to be fetched. * @returns {Function} A redux thunk. */function fetchFavorites(){var ignoreCache=arguments.length>0&&arguments[0]!==undefined?arguments[0]:false;var listId=arguments.length>1&&arguments[1]!==undefined?arguments[1]:undefined;return(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch,getState){var defaultList,takenListId,hasMultiSupport,getFavoritesProductsByList,data,timestamp,pipelineVersion,request,result;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:// Fallback for deprecated calls without list id. defaultList=getFavoritesDefaultList(getState());takenListId=listId||defaultList.code;hasMultiSupport=getHasMultipleFavoritesListsSupport(getState());getFavoritesProductsByList=makeGetFavoritesProductsByList(function(){return takenListId;});data=getFavoritesProductsByList(getState());if(!(!ignoreCache&&!shouldFetchData(data))){_context.next=7;break;}return _context.abrupt("return",null);case 7:timestamp=Date.now();dispatch(requestFavorites(takenListId));// v2 of the getFavorites pipeline doesn't exist right now within the old services pipelineVersion=hasNewServices()?2:1;request=new PipelineRequest(SHOPGATE_USER_GET_FAVORITES).setVersion(pipelineVersion).setInput(_extends({},hasMultiSupport?{favoritesListId:takenListId}:null)).setErrorBlacklist([EFAVORITE,EUNKNOWN,EBIGAPI,ELIMIT]).dispatch();_context.prev=11;_context.next=14;return request;case 14:result=_context.sent;if(pipelineVersion===1){// Convert response data based on the pipeline version result.items=((result===null||result===void 0?void 0:result.products)||[]).map(function(product){return{product:product,note:null,quantity:1};});delete result.products;}dispatch(receiveProducts({products:result.items.map(function(_ref2){var product=_ref2.product;return product;}),fetchInventory:false}));dispatch(receiveFavorites(result.items,timestamp,takenListId));return _context.abrupt("return",result);case 21:_context.prev=21;_context.t0=_context["catch"](11);dispatch(errorFetchFavorites(_context.t0,takenListId));return _context.abrupt("return",null);case 25:case"end":return _context.stop();}},_callee,null,[[11,21]]);}));return function(_x,_x2){return _ref.apply(this,arguments);};}());}/** @mixes {MutableFunction} */export default mutable(fetchFavorites);