@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
11 lines • 2.7 kB
JavaScript
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{getUseGetFavoriteIdsPipeline}from"../selectors";import fetchFavorites from"./fetchFavorites";import fetchFavoriteIds from"./fetchFavoriteIds";import fetchFavoritesList from"./fetchFavoritesList";/**
*
* Combine fetch favorites list action and fetch favorites action.
* @param {boolean} ignoreCache Ignores cache when true
* @returns {Function} A redux thunk.
*/function fetchFavoritesListsWithItems(){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 lists,useGetFavoriteIdsPipeline,itemPromises,wishlistItems;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_context.next=2;return dispatch(fetchFavoritesList(ignoreCache));case 2:_context.t0=_context.sent;if(_context.t0){_context.next=5;break;}_context.t0=[];case 5:lists=_context.t0;useGetFavoriteIdsPipeline=getUseGetFavoriteIdsPipeline(getState());itemPromises=lists.map(function(list){/**
* In PWA6 the fetchFavoriteIds pipelines was used to retrieve the contents of the
* favorites list. Its response doesn't contain product objects, but only product ids.
* For compatibility reasons and simplification, the action returns an object that's similar
* to the fetchFavorites response, so that the existing reducers can be used.
*/if(useGetFavoriteIdsPipeline){return dispatch(fetchFavoriteIds(ignoreCache,list.id));}return dispatch(fetchFavorites(ignoreCache,list.id));});_context.next=10;return Promise.all(itemPromises);case 10:wishlistItems=_context.sent;return _context.abrupt("return",lists.map(function(_ref2,index){var id=_ref2.id,name=_ref2.name;return{id:id,name:name,itemCount:(wishlistItems[index]||{}).itemCount,items:(wishlistItems[index]||{}).items};}));case 12:case"end":return _context.stop();}},_callee);}));return function(_x,_x2){return _ref.apply(this,arguments);};}());}export default fetchFavoritesListsWithItems;