UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

9 lines 2.87 kB
import _regeneratorRuntime from"@babel/runtime/regenerator";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{shouldFetchData,mutable}from'@shopgate/pwa-common/helpers/redux';import{getFavoritesListState,getHasMultipleFavoritesListsSupport}from"../selectors";import{SHOPGATE_USER_GET_FAVORITES_LIST}from"../constants/Pipelines";import{RECEIVE_FAVORITES_LISTS}from"../constants";/** * Fetch favorites list action. * @param {boolean} ignoreCache Ignores cache when true * @returns {Function} A redux thunk. */function fetchFavoritesLists(){var ignoreCache=arguments.length>0&&arguments[0]!==undefined?arguments[0]:false;return(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch,getState){var state,hasMultiSupport,favoritesLists,data,_ref3,_favoritesLists;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:state=getState();hasMultiSupport=getHasMultipleFavoritesListsSupport(state);if(hasMultiSupport){_context.next=6;break;}/** * If the doesn't have support for multiple favorite lists, we just mock the pipeline response * and don't dispatch the request. So we can keep backwards compatibility with older * extensions that don't support the pipeline. */favoritesLists=[{id:'DEFAULT',name:''}];dispatch({type:RECEIVE_FAVORITES_LISTS,favoritesLists:favoritesLists});return _context.abrupt("return",favoritesLists);case 6:data=getFavoritesListState(state);if(!(!ignoreCache&&!shouldFetchData(data))){_context.next=9;break;}return _context.abrupt("return",(data===null||data===void 0?void 0:data.lists)||[]);case 9:_context.prev=9;_context.next=12;return new PipelineRequest(SHOPGATE_USER_GET_FAVORITES_LIST).dispatch();case 12:_ref3=_context.sent;_favoritesLists=_ref3.favoritesLists;dispatch({type:RECEIVE_FAVORITES_LISTS,favoritesLists:_favoritesLists});return _context.abrupt("return",_favoritesLists);case 18:_context.prev=18;_context.t0=_context["catch"](9);return _context.abrupt("return",[]);case 21:case"end":return _context.stop();}},_callee,null,[[9,18]]);}));return function(_x,_x2){return _ref.apply(this,arguments);};}());}/** @mixes {MutableFunction} */export default mutable(fetchFavoritesLists);