@shopgate/engage
Version:
Shopgate's ENGAGE library.
4 lines • 5.81 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";var _excluded=["marketingOptIn"];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 _objectWithoutProperties(source,excluded){if(source==null)return{};var target=_objectWithoutPropertiesLoose(source,excluded);var key,i;if(Object.getOwnPropertySymbols){var sourceSymbolKeys=Object.getOwnPropertySymbols(source);for(i=0;i<sourceSymbolKeys.length;i++){key=sourceSymbolKeys[i];if(excluded.indexOf(key)>=0)continue;if(!Object.prototype.propertyIsEnumerable.call(source,key))continue;target[key]=source[key];}}return target;}function _objectWithoutPropertiesLoose(source,excluded){if(source==null)return{};var target={};var sourceKeys=Object.keys(source);var key,i;for(i=0;i<sourceKeys.length;i++){key=sourceKeys[i];if(excluded.indexOf(key)>=0)continue;target[key]=source[key];}return target;}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{EVALIDATION}from'@shopgate/engage/core';import{updateCheckoutOrder}from'@shopgate/engage/checkout/actions';import{extractAttributes,convertPipelineValidationErrors}from'@shopgate/engage/account/helper/form';import{getMerchantCustomerAttributes}from'@shopgate/engage/core/selectors/merchantSettings';/**
* Submits guest registration form data.
* @returns {Function}
*/export var submitGuestRegistration=function submitGuestRegistration(_ref){var billingFormData=_ref.billingFormData,shippingFormData=_ref.shippingFormData,pickupFormData=_ref.pickupFormData,extraFormData=_ref.extraFormData,processShipping=_ref.processShipping;return(/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch,getState){var customerAttributes,marketingOptIn,attributeData,attributes,pickupPerson,restPickupFormData,_pickupFormData,shippingFormVisible,pickupFormVisible,addressSequences,order,errors,response,code,validationErrors,converted,_converted$validation,_converted$validation2,sequenceErrors,shippingIndex,pickupIndex,billing,shipping,pickup,extra;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:customerAttributes=getMerchantCustomerAttributes(getState());marketingOptIn=extraFormData.marketingOptIn,attributeData=_objectWithoutProperties(extraFormData,_excluded);attributes=extractAttributes(customerAttributes,attributeData);restPickupFormData={};if(pickupFormData){_pickupFormData=pickupFormData;pickupPerson=_pickupFormData.pickupPerson;restPickupFormData=_objectWithoutPropertiesLoose(_pickupFormData,["pickupPerson"]);_pickupFormData;}shippingFormVisible=processShipping&&!!shippingFormData;pickupFormVisible=pickupFormData&&pickupPerson!=='me';addressSequences=[_extends({type:'billing'},billingFormData)].concat(processShipping?_extends({type:'shipping'},shippingFormData||billingFormData):[],pickupFormData?_extends({},pickupPerson==='me'?_extends({type:'pickup'},billingFormData):_extends({type:'pickup'},restPickupFormData)):[]);order=_extends({addressSequences:addressSequences,primaryBillToAddressSequenceIndex:0,primaryShipToAddressSequenceIndex:1},(attributes===null||attributes===void 0?void 0:attributes.length)?{customer:{attributes:attributes}}:{});_context.prev=9;_context.next=12;return dispatch(updateCheckoutOrder(order));case 12:response=_context.sent;_context.next=23;break;case 15:_context.prev=15;_context.t0=_context["catch"](9);code=_context.t0.code,validationErrors=_context.t0.errors;if(!(code===EVALIDATION)){_context.next=22;break;}errors=validationErrors;_context.next=23;break;case 22:throw _context.t0;case 23:converted=convertPipelineValidationErrors(errors,attributes);if(!((converted===null||converted===void 0?void 0:converted.validation)&&Object.keys(converted.validation).length>0)){_context.next=33;break;}sequenceErrors=converted===null||converted===void 0?void 0:(_converted$validation=converted.validation)===null||_converted$validation===void 0?void 0:_converted$validation.addressSequences;shippingIndex=addressSequences.findIndex(function(item){return item.type==='shipping';});pickupIndex=addressSequences.findIndex(function(item){return item.type==='pickup';});billing=(sequenceErrors===null||sequenceErrors===void 0?void 0:sequenceErrors['0'])||{};shipping=shippingFormVisible?(sequenceErrors===null||sequenceErrors===void 0?void 0:sequenceErrors[shippingIndex])||{}:{};pickup=pickupFormVisible?(sequenceErrors===null||sequenceErrors===void 0?void 0:sequenceErrors[pickupIndex])||{}:{};extra=(converted===null||converted===void 0?void 0:(_converted$validation2=converted.validation)===null||_converted$validation2===void 0?void 0:_converted$validation2.attributes)||{};return _context.abrupt("return",{response:response,errors:{billingFormData:_extends({},billing),shippingFormData:_extends({},shipping),pickupFormData:_extends({},pickup),extraFormData:_extends({},extra)}});case 33:return _context.abrupt("return",{response:response});case 34:case"end":return _context.stop();}},_callee,null,[[9,15]]);}));return function(_x,_x2){return _ref2.apply(this,arguments);};}());};