@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
9 lines • 3.16 kB
JavaScript
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{mutable}from'@shopgate/pwa-common/helpers/redux';import PipelineRequest from'@shopgate/pwa-core/classes/PipelineRequest';import isNumber from'lodash/isNumber';import isString from'lodash/isString';import{SHOPGATE_USER_UPDATE_FAVORITES}from"../constants/Pipelines";import{errorUpdateFavorites,successUpdateFavorites}from"../action-creators";import{getHasMultipleFavoritesListsSupport,getFavoritesDefaultList}from"../selectors";/**
* Updates a single product on the favorite list using the `updateFavorites` pipeline.
* @param {string} productId The id of the product.
* @param {string} listId Id of the list to be updated.
* @param {number} quantity New favorites quantity to set
* @param {string} notes ew favorites notes to set
* @returns {Function} A redux thunk.
*/function updateFavorites(productId){var listId=arguments.length>1&&arguments[1]!==undefined?arguments[1]:null;var quantity=arguments.length>2?arguments[2]:undefined;var notes=arguments.length>3?arguments[3]:undefined;return(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch,getState){var state,hasMultiSupport,defaultList,takenListId,request;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:state=getState();hasMultiSupport=getHasMultipleFavoritesListsSupport(state);// Fallback for deprecated calls without list id.
defaultList=getFavoritesDefaultList(state);takenListId=listId||defaultList.id;request=new PipelineRequest(SHOPGATE_USER_UPDATE_FAVORITES).setInput(_extends({productId:productId},hasMultiSupport?{favoritesListId:takenListId}:null,{},isNumber(quantity)?{quantity:quantity}:{},{},isString(notes)?{notes:notes}:{})).setRetries(0).dispatch();_context.prev=5;_context.next=8;return request;case 8:dispatch(successUpdateFavorites(productId,takenListId));_context.next=14;break;case 11:_context.prev=11;_context.t0=_context["catch"](5);dispatch(errorUpdateFavorites(productId,_context.t0,takenListId));case 14:return _context.abrupt("return",request);case 15:case"end":return _context.stop();}},_callee,null,[[5,11]]);}));return function(_x,_x2){return _ref.apply(this,arguments);};}());}export default mutable(updateFavorites);