@shopgate/engage
Version:
Shopgate's ENGAGE library.
28 lines • 5.16 kB
JavaScript
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 appConfig from'@shopgate/pwa-common/helpers/config';import{appDidStart$}from'@shopgate/pwa-common/streams/app';import{checkoutSuccess$}from'@shopgate/engage/checkout/streams';import event from'@shopgate/pwa-core/classes/Event';import{hasWebBridge}from'@shopgate/engage/core';import{increaseAppStartCount,resetAppStartCount}from"../action-creators/appStart";import{increaseOrdersPlacedCount,resetOrdersPlacedCount}from"../action-creators/ordersPlaced";import{resetTimerState,setTimerStartTime}from"../action-creators/timer";import{APP_RATING_TIMER_TIMESPAN}from"../constants";import{getAppRatingState}from"../selectors/appRating";import{showModal}from"../actions/showModal";/**
* App rating subscriptions
* @param {Function} subscribe The subscribe function
*/export default function appRating(subscribe){if(!hasWebBridge){return;}var _appConfig$appRating=appConfig.appRating,appStarts=_appConfig$appRating.appStarts,ordersPlaced=_appConfig$appRating.ordersPlaced,timeInterval=_appConfig$appRating.timeInterval,rejectionMaxCount=_appConfig$appRating.rejectionMaxCount,bundleId=_appConfig$appRating.bundleId;// even subscriber to handle app start ratings
// and also time interval ratings
subscribe(appDidStart$,/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref){var dispatch,getState,state,mustShowModal,hasRepeats,resetAction,increaseAction;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:dispatch=_ref.dispatch,getState=_ref.getState;if(!(!bundleId||!bundleId.android||!bundleId.ios)){_context.next=3;break;}return _context.abrupt("return");case 3:// every time the app starts
// we increase the start count
dispatch(increaseAppStartCount());state=getAppRatingState(getState());// if the user has already rated the app
// we'll cancel the operations as we
// don't have to show the modal once more
if(!state.alreadyRated){_context.next=7;break;}return _context.abrupt("return");case 7:if(!(state.rejectionCount>=rejectionMaxCount)){_context.next=9;break;}return _context.abrupt("return");case 9:// initiate the first start time
if(state.timerStartTimestamp===null){dispatch(setTimerStartTime());}mustShowModal=false;hasRepeats=false;resetAction=null;increaseAction=null;if(timeInterval&&Number(timeInterval.value)>0&&Number(state.timerStartTimestamp)>0&&Date.now()-state.timerStartTimestamp>=timeInterval.value*APP_RATING_TIMER_TIMESPAN){mustShowModal=true;hasRepeats=timeInterval.repeats===null||state.timerRepeatsCount<=timeInterval.repeats;resetAction=resetTimerState;// since the time is elapsed
// we reset the starting time
increaseAction=setTimerStartTime;}else if(appStarts){mustShowModal=Number(appStarts.value)>0&&state.appStartCount>=appStarts.value;hasRepeats=appStarts.repeats===null||state.appStartResetCount<=appStarts.repeats;resetAction=resetAppStartCount;increaseAction=null;}// the actual show modal logic
dispatch(showModal(resetAction,increaseAction,mustShowModal,hasRepeats));case 16:case"end":return _context.stop();}},_callee);}));return function(_x){return _ref2.apply(this,arguments);};}());/**
* Handle checkout success logic
* @param {Function} dispatch dispatch
* @param {Function} getState getState
*/var handleCheckoutSuccess=function handleCheckoutSuccess(dispatch,getState){if(!bundleId||!bundleId.android||!bundleId.ios){return;}var state=getAppRatingState(getState());// if the user has already rated the app
// we'll cancel the operations as we
// don't have to show the modal once more
if(state.alreadyRated){return;}// cancel the process if user has
// already rejected rating the app
// many times before
if(state.rejectionCount>=rejectionMaxCount){return;}if(!ordersPlaced){return;}// orders placed count starts from 0
var mustShowModal=state.ordersPlacedCount===ordersPlaced.value-1;var hasRepeats=ordersPlaced.repeats===null||state.ordersPlacedResetCount<=ordersPlaced.repeats;dispatch(showModal(resetOrdersPlacedCount,increaseOrdersPlacedCount,mustShowModal,hasRepeats));};// subscriber to handle order placed ratings from native checkout
subscribe(checkoutSuccess$,function(_ref3){var dispatch=_ref3.dispatch,getState=_ref3.getState;handleCheckoutSuccess(dispatch,getState);});// event subscriber to handle order placed ratings from webcheckout
subscribe(appDidStart$,function(_ref4){var dispatch=_ref4.dispatch,getState=_ref4.getState;event.addCallback('checkoutSuccess',function(){handleCheckoutSuccess(dispatch,getState);});});}