@shopgate/engage
Version:
Shopgate's ENGAGE library.
17 lines • 3.05 kB
JavaScript
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{showModal,historyResetTo,historyPop,INDEX_PATH}from'@shopgate/engage/core';import{CART_PATH}from'@shopgate/engage/cart';import{MODAL_PIPELINE_ERROR}from'@shopgate/pwa-common/constants/ModalTypes';import{getRouterStackIndex,makeGetPrevRouteIndexByPattern}from'@shopgate/pwa-common/selectors/router';import*as Sentry from'@sentry/browser';var getPrevRouteIndexByPatternCart=makeGetPrevRouteIndexByPattern(CART_PATH);/**
* Redirects the user to the cart
* @param {Function} dispatch The Redux dispatch function
* @param {Function} getState he Redux getState function
*/var gotoCart=function gotoCart(dispatch,getState){var state=getState();var currentRouteIndex=getRouterStackIndex(state);var nextRouteIndex=getPrevRouteIndexByPatternCart(state);if(nextRouteIndex!==-1){var steps=currentRouteIndex-nextRouteIndex;// Navigate back to a previous history entry if possible
dispatch(historyPop({steps:steps}));}else{// Reset the history stack to the desired page when there is no old page within the stack
dispatch(historyResetTo(CART_PATH));}};/**
* Handles checkout errors.
* @param {string} message Message shown to the user.
* @param {string} pipeline Pipeline name.
* @param {Object} error Error.
* @param {boolean} redirect Whether it should redirect.
* @param {Array} gotoCartErrorCodes A list of error codes which should redirect to the cart
* @return {Object}
*/export var errorCheckout=function errorCheckout(message,pipeline,error,redirect){var gotoCartErrorCodes=arguments.length>4&&arguments[4]!==undefined?arguments[4]:[];return function(dispatch,getState){var subCode;if(error===null||error===void 0?void 0:error.errors){var _error$errors2=_slicedToArray(error.errors,1),first=_error$errors2[0];subCode=first.code;}var errorObject={code:error.code||'NOT SET',message:JSON.stringify(error,null,2)};// Go back to homepage and inform shopper.
if(redirect){dispatch(historyResetTo(INDEX_PATH));}else if(['shopgate.checkout.initialize'].includes(pipeline)||gotoCartErrorCodes.includes(subCode)){gotoCart(dispatch,getState);}dispatch(showModal({type:MODAL_PIPELINE_ERROR,title:null,confirm:null,dismiss:'modal.ok',message:message,params:{pipeline:pipeline,message:JSON.stringify(errorObject,null,2)}}));// Log to sentry.
Sentry.withScope(function(scope){scope.setLevel(Sentry.Severity.Critical);scope.setExtra('origin','checkout');Sentry.captureException(errorObject);});return{needsPayment:false,success:false};};};