@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
47 lines • 5.86 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{mutable}from'@shopgate/pwa-common/helpers/redux';import{getWishlistItemQuantityEnabled,getLoadWishlistOnAppStartEnabled}from'@shopgate/engage/core/selectors/shopSettings';import{getFavoritesDefaultList,getFavoritesLists,makeIsProductOnSpecificFavoriteList}from"../selectors";import{addProductToFavorites,removeProductFromFavorites,requestFlushFavoritesBuffer,openFavoritesListChooser,updateProductInFavorites}from"../action-creators";import fetchFavoritesListsWithItems from"./fetchFavoritesListsWithItems";/**
* Adds a product to the favorite list (debounced and buffered).
* @mixes {MutableFunction}
* @param {string} productId Product identifier.
* @param {string} listId List identifier.
* @param {number} quantity New favorites quantity to set
* @param {string} notes New favorites notes to set
* @param {boolean} showToast Whether to show a confirmation toast after product was added
* @return {Function}
*/export var addFavorite=mutable(function(productId,listId,quantity,notes){var showToast=arguments.length>4&&arguments[4]!==undefined?arguments[4]:false;return function(dispatch,getState){var defaultList=getFavoritesDefaultList(getState());dispatch(addProductToFavorites(productId,listId||defaultList.id,quantity,notes,showToast));};});/**
* Adds a product to the favorite list (debounced and buffered).
* @param {string} productId Product identifier.
* @return {Function}
* @deprecated Please use `addFavorite` instead.
*/export var addFavorites=addFavorite;/**
* Removes a product and optionally its relatives from the favorite list (debounced and buffered).
* @mixes {MutableFunction}
* @param {string} productId Product identifier.
* @param {boolean} withRelatives When true relatives which are on list are also removed.
* @param {string} listId List identifier.
* @returns {Function}
*/export var removeFavorites=mutable(function(productId){var withRelatives=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;var listId=arguments.length>2?arguments[2]:undefined;return function(dispatch,getState){var defaultList=getFavoritesDefaultList(getState());dispatch(removeProductFromFavorites(productId,withRelatives,listId||defaultList.id));};});/**
* Triggers a sync of favorites by immediately flushing buffered favorite update actions.
* @mixes {MutableFunction}
* @return {Function}
*/export var requestSync=mutable(function(listId){return function(dispatch){dispatch(requestFlushFavoritesBuffer(listId));};});/**
* Adds a product to the favorite list (debounced and buffered).
* @param {string} productId Product identifier.
* @param {string} listId List identifier.
* @param {boolean} withRelatives When true relatives which are on list are also removed.
* @return {Function}
*/export var toggleFavorite=mutable(function(productId,listId){var withRelatives=arguments.length>2&&arguments[2]!==undefined?arguments[2]:false;return function(dispatch,getState){var state=getState();// With quantity enabled the favorites button always adds (increases quantity)
var wishlistItemQuantityEnabled=getWishlistItemQuantityEnabled(state);var loadWishlistOnAppStartEnabled=getLoadWishlistOnAppStartEnabled(state);if(wishlistItemQuantityEnabled||!loadWishlistOnAppStartEnabled){dispatch(addFavorite(productId,listId,null,null,true));}else{var isOnList=makeIsProductOnSpecificFavoriteList(function(){return productId;},function(){return listId;})(state);dispatch(!isOnList?addFavorite(productId,listId):removeFavorites(productId,withRelatives,listId));}};});/**
* Updatest a product in the favorite list (debounced and buffered).
* @param {string} productId Product identifier.
* @param {string} listId List identifier.
* @param {number} quantity
* @param {string} notes
* @return {Function}
*/export var updateFavorite=mutable(function(productId,listId,quantity,notes){return function(dispatch,getState){var defaultList=getFavoritesDefaultList(getState());dispatch(updateProductInFavorites(productId,listId||defaultList.id,quantity,notes));};});/**
* Adds a product to a wishlist by opening a chooser if user has multiple lists.
* @mixes {MutableFunction}
* @param {string} productId Product identifier.
* @param {boolean} withRelatives When true relatives which are on list are also removed.
* @return {Function}
*/export var toggleFavoriteWithListChooser=mutable(function(productId){var withRelatives=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;return(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch,getState){var state,lists;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_context.next=2;return dispatch(fetchFavoritesListsWithItems());case 2:state=getState();lists=getFavoritesLists(state);// Only one list available therefore we just add/remove it the product there.
if(!(lists.length<=1)){_context.next=7;break;}dispatch(toggleFavorite(productId,lists[0].id,withRelatives));return _context.abrupt("return");case 7:dispatch(openFavoritesListChooser(productId,withRelatives));case 8:case"end":return _context.stop();}},_callee);}));return function(_x,_x2){return _ref.apply(this,arguments);};}());});