UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

9 lines 2.33 kB
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);}import PipelineRequest from'@shopgate/pwa-core/classes/PipelineRequest';import{EFAVORITE,EUNKNOWN}from'@shopgate/pwa-core/constants/Pipeline';import{shouldFetchData,mutable}from'@shopgate/pwa-common/helpers/redux';import{SHOPGATE_USER_GET_FAVORITE_IDS}from"../constants/Pipelines";import{receiveFavorites,requestFavorites,errorFetchFavorites}from"../action-creators";import{getHasMultipleFavoritesListsSupport,makeGetFavoritesProductsByList}from"../selectors";/** * Fetch favorite IDs of a favorites list action. * @param {boolean} ignoreCache Ignores cache when true * @param {string} [favoritesListId] The ID of the favorites list * @returns {Function} A redux thunk. */function fetchFavoriteIds(){var ignoreCache=arguments.length>0&&arguments[0]!==undefined?arguments[0]:false;var favoritesListId=arguments.length>1&&arguments[1]!==undefined?arguments[1]:'DEFAULT';return function(dispatch,getState){var hasMultiSupport=getHasMultipleFavoritesListsSupport(getState());var getFavoritesProductsByList=makeGetFavoritesProductsByList(function(){return favoritesListId;});var data=getFavoritesProductsByList(getState());if(!ignoreCache&&!shouldFetchData(data)){return Promise.resolve(data);}var timestamp=Date.now();dispatch(requestFavorites(favoritesListId));return new PipelineRequest(SHOPGATE_USER_GET_FAVORITE_IDS).setInput(_extends({},hasMultiSupport?{favoritesListId:favoritesListId}:null)).setErrorBlacklist([EFAVORITE,EUNKNOWN]).dispatch().then(function(_ref){var productIds=_ref.productIds;/** * Sanitize the pipeline return value to archive compatibility with the reducers for the * fetchFavorites pipeline. */var items=productIds.map(function(productId){return{product:{id:productId}};});var sanitizedResponse={items:items,itemCount:items.length};dispatch(receiveFavorites(sanitizedResponse.items,timestamp,favoritesListId));return sanitizedResponse;})["catch"](function(err){dispatch(errorFetchFavorites(err,favoritesListId));});};}/** @mixes {MutableFunction} */export default mutable(fetchFavoriteIds);