UNPKG

@shopgate/engage

Version:
11 lines • 7.92 kB
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);});};}function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}import React,{useCallback,useMemo,useState,useEffect}from'react';import PropTypes from'prop-types';import{connect}from'react-redux';import{LoadingProvider,historyPopToRoute as historyPopToRouteAction,useRoute}from'@shopgate/engage/core';import{getCheckoutOrder,getCheckoutBillingAddress,getCheckoutShippingAddress,getIsPickupContactSelectionEnabled}from'@shopgate/engage/checkout/selectors/order';import{updateCheckoutOrder as updateOrder,fetchCheckoutOrder as fetchOrder}from'@shopgate/engage/checkout/actions';import Context from"./AddressBookProvider.context";import{ADDRESS_TYPE_BILLING,ADDRESS_TYPE_SHIPPING,CHECKOUT_PATTERN}from"../constants";/** * @param {Object} state The application state. * @returns {Object} */var makeMapStateToProp=function makeMapStateToProp(){return function(state){return{order:getCheckoutOrder(state),billingAddress:getCheckoutBillingAddress(state),shippingAddress:getCheckoutShippingAddress(state),isPickupContactEnabled:getIsPickupContactSelectionEnabled(state)};};};var mapDispatchToProps={updateCheckoutOrder:updateOrder,fetchCheckoutOrder:fetchOrder,historyPopToRoute:historyPopToRouteAction};/** * AddressBookProvider * @param {Object} props The component props * @returns {JSX} */var AddressBookProvider=function AddressBookProvider(_ref){var children=_ref.children,order=_ref.order,billingAddress=_ref.billingAddress,shippingAddress=_ref.shippingAddress,updateCheckoutOrder=_ref.updateCheckoutOrder,fetchCheckoutOrder=_ref.fetchCheckoutOrder,historyPopToRoute=_ref.historyPopToRoute,isPickupContactEnabled=_ref.isPickupContactEnabled;var _useState=useState(false),_useState2=_slicedToArray(_useState,2),isLoading=_useState2[0],setLoading=_useState2[1];var _useRoute=useRoute(),pathname=_useRoute.pathname,routeId=_useRoute.id,_useRoute$params$type=_useRoute.params.type,type=_useRoute$params$type===void 0?ADDRESS_TYPE_BILLING:_useRoute$params$type;var popToCheckout=useCallback(function(){historyPopToRoute({pattern:CHECKOUT_PATTERN,routeId:routeId});},[historyPopToRoute,routeId]);/** * @param {Array} addressSequences Address sequences * @returns {Array} */var createAddressSequenceIndexes=function createAddressSequenceIndexes(addressSequences){var primaryBillToAddressSequenceIndex=addressSequences.findIndex(function(address){return address.type==='billing';});var primaryShipToAddressSequenceIndex=addressSequences.findIndex(function(address){return address.type==='shipping'||address.type==='pickup';});primaryBillToAddressSequenceIndex=primaryBillToAddressSequenceIndex!==-1?primaryBillToAddressSequenceIndex:undefined;primaryShipToAddressSequenceIndex=primaryShipToAddressSequenceIndex!==-1?primaryShipToAddressSequenceIndex:undefined;return{primaryBillToAddressSequenceIndex:primaryBillToAddressSequenceIndex,primaryShipToAddressSequenceIndex:primaryShipToAddressSequenceIndex};};var updateOrderWithContact=useCallback(/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(contactId){var gotoCheckout,addressSequences,billingFromSequence,_args=arguments;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:gotoCheckout=_args.length>1&&_args[1]!==undefined?_args[1]:true;setLoading(true);_context.prev=2;addressSequences=[];if(type===ADDRESS_TYPE_BILLING){addressSequences.push({customerContactId:contactId,type:'billing'});}else if(billingAddress){addressSequences.push({customerContactId:billingAddress.customerContactId,type:'billing'});}if(type===ADDRESS_TYPE_SHIPPING){addressSequences.push({customerContactId:contactId,type:'shipping'});}else if(shippingAddress){addressSequences.push({customerContactId:shippingAddress.customerContactId,type:'shipping'});}billingFromSequence=addressSequences.find(function(address){return address.type==='billing';});if(billingFromSequence&&isPickupContactEnabled){addressSequences.push({customerContactId:billingFromSequence.customerContactId,type:'pickup'});}_context.next=10;return updateCheckoutOrder(_extends({addressSequences:addressSequences},createAddressSequenceIndexes(addressSequences)));case 10:if(gotoCheckout){popToCheckout();}_context.next=15;break;case 13:_context.prev=13;_context.t0=_context["catch"](2);case 15:setLoading(false);case 16:case"end":return _context.stop();}},_callee,null,[[2,13]]);}));return function(_x){return _ref2.apply(this,arguments);};}(),[billingAddress,isPickupContactEnabled,popToCheckout,shippingAddress,type,updateCheckoutOrder]);var deleteContactFromOrder=useCallback(/*#__PURE__*/function(){var _ref3=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(contactId){var addressSequences;return _regeneratorRuntime.wrap(function _callee2$(_context2){while(1)switch(_context2.prev=_context2.next){case 0:if(Array.isArray(order===null||order===void 0?void 0:order.addressSequences)){_context2.next=2;break;}return _context2.abrupt("return");case 2:addressSequences=order.addressSequences.reduce(function(acc,_ref4){var customerContactId=_ref4.customerContactId,addressType=_ref4.type;if(customerContactId!==contactId){acc.push({type:addressType,customerContactId:customerContactId});}return acc;},[]);_context2.prev=3;_context2.next=6;return updateCheckoutOrder(_extends({addressSequences:addressSequences},createAddressSequenceIndexes(addressSequences)));case 6:_context2.next=10;break;case 8:_context2.prev=8;_context2.t0=_context2["catch"](3);case 10:case"end":return _context2.stop();}},_callee2,null,[[3,8]]);}));return function(_x2){return _ref3.apply(this,arguments);};}(),[order,updateCheckoutOrder]);useEffect(function(){if(isLoading){LoadingProvider.setLoading(pathname);}else{LoadingProvider.resetLoading(pathname);}return function(){LoadingProvider.resetLoading(pathname);};},[isLoading,pathname]);useEffect(function(){if(!order){fetchCheckoutOrder();}},[fetchCheckoutOrder,order]);var value=useMemo(function(){return{isCheckout:true,updateOrderWithContact:updateOrderWithContact,deleteContactFromOrder:deleteContactFromOrder,popToCheckout:popToCheckout,type:type};},[updateOrderWithContact,deleteContactFromOrder,popToCheckout,type]);return React.createElement(Context.Provider,{value:value},children);};AddressBookProvider.defaultProps={children:null,order:null,billingAddress:null,shippingAddress:null};export default connect(makeMapStateToProp,mapDispatchToProps)(AddressBookProvider);