UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

9 lines 3.31 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{PROCESS_SEQUENTIAL}from'@shopgate/pwa-core/constants/ProcessTypes';import{mutable}from'@shopgate/pwa-common/helpers/redux';import configuration from'@shopgate/pwa-common/collections/Configuration';import{PIPELINES}from'@shopgate/pwa-common/constants/Configuration';import{SHOPGATE_CART_UPDATE_PRODUCTS}from"../constants/Pipelines";import createPipelineErrorList from"../helpers/createPipelineErrorList";import{ECART}from"../constants/PipelineErrors";import updateProducts from"../action-creators/updateProductsInCart";import successUpdateProductsInCart from"../action-creators/successUpdateProductsInCart";import errorUpdateProductsInCart from"../action-creators/errorUpdateProductsInCart";import{messagesHaveErrors,createErrorMessageList}from"../helpers";/** * Updates a product in the cart. * @param {Array} updateData The data for the updateProductsInCart request. * @return {Function} A redux thunk. */function updateProductsInCart(updateData){return(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch){var _configuration$get,_configuration$get$SH,pipeline,request;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_configuration$get=configuration.get(PIPELINES,{}),_configuration$get$SH=_configuration$get[SHOPGATE_CART_UPDATE_PRODUCTS],pipeline=_configuration$get$SH===void 0?SHOPGATE_CART_UPDATE_PRODUCTS:_configuration$get$SH;dispatch(updateProducts(updateData));request=new PipelineRequest(pipeline).setInput({cartItems:updateData}).setResponseProcessed(PROCESS_SEQUENTIAL).setErrorBlacklist(ECART).dispatch();request.then(function(result){/** * @deprecated: The property "messages" is not supposed to be part of the pipeline response. * Specification demands errors to be returned as response object with an "error" property. * This code snippet needs to be removed after fixing the `@shopgate/legacy-cart` extension. */if(result.messages&&messagesHaveErrors(result.messages)){dispatch(errorUpdateProductsInCart(updateData,createErrorMessageList(SHOPGATE_CART_UPDATE_PRODUCTS,result.messages)));return;}dispatch(successUpdateProductsInCart());})["catch"](function(error){dispatch(errorUpdateProductsInCart(updateData,createPipelineErrorList(SHOPGATE_CART_UPDATE_PRODUCTS,error)));});return _context.abrupt("return",request);case 5:case"end":return _context.stop();}},_callee);}));return function(_x){return _ref.apply(this,arguments);};}());}/** @mixes {MutableFunction} */export default mutable(updateProductsInCart);