@shopgate/engage
Version:
Shopgate's ENGAGE library.
12 lines • 6.43 kB
JavaScript
import _regeneratorRuntime from"@babel/runtime/regenerator";function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj;};}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};}return _typeof(obj);}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/engage';import{main$}from'@shopgate/engage/core/streams';import{event}from'@shopgate/engage/core/classes';import{appSupportsPushOptIn,logger}from'@shopgate/engage/core/helpers';import{PERMISSION_ID_PUSH,PERMISSION_STATUS_NOT_DETERMINED}from'@shopgate/engage/core/constants';import{softOptInShown}from'@shopgate/engage/core/action-creators';import{requestAppPermissionStatus}from'@shopgate/engage/core/actions';import{cookieConsentInitialized$}from'@shopgate/engage/tracking/streams';import{increaseAppStartCount,resetAppStartCount,increaseOrdersPlacedCount,resetOrdersPlacedCount,setLastPopupTimestamp,increaseRejectionCount,showPushOptInModal}from"../action-creators";import{PUSH_OPT_IN_OPT_IN_POSTPONED}from"../constants";import{getPushOptInTriggerState,getPushOptInTrackingMeta}from"../selectors";var DAY_IN_MS=1000*60*60*24;var increaseRejectionCount$=main$.filter(function(_ref){var action=_ref.action;return action.type===PUSH_OPT_IN_OPT_IN_POSTPONED;});/**
* Push opt in subscriptions
* @param {Function} subscribe The subscribe function
*/export default function pushOptIn(subscribe){/**
* @param {Object} subscriptionParams Params from the subscription callback
* @param {string} configKey The "pushOptIn" key to be used
* @param {Function} increaseCountAction Action to increase the count in Redux for the configKey
* @returns {Promise<void>}
*/var showOptInAfterChecks=/*#__PURE__*/function(){var _ref3=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref2,configKey,increaseCountAction){var dispatch,getState,_appConfig$pushOptIn,_appConfig$pushOptIn2,appStarts,ordersPlaced,rejectionMaxCount,minDaysBetweenOptIns,_ref4,pushStatus,state,configValue,resetAction,resetCountState,configCountState,mustShowModal,hasRepeats,minDaysElapsed,meta;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:dispatch=_ref2.dispatch,getState=_ref2.getState;if(appSupportsPushOptIn()){_context.next=3;break;}return _context.abrupt("return");case 3:_appConfig$pushOptIn=appConfig.pushOptIn,_appConfig$pushOptIn2=_appConfig$pushOptIn===void 0?{}:_appConfig$pushOptIn,appStarts=_appConfig$pushOptIn2.appStarts,ordersPlaced=_appConfig$pushOptIn2.ordersPlaced,rejectionMaxCount=_appConfig$pushOptIn2.rejectionMaxCount,minDaysBetweenOptIns=_appConfig$pushOptIn2.minDaysBetweenOptIns;// Deactivate feature when config is invalid
if(!(typeof minDaysBetweenOptIns!=='number'||typeof rejectionMaxCount!=='number'||_typeof(ordersPlaced)!=='object'||_typeof(appStarts)!=='object')){_context.next=7;break;}logger.error('PushOptInTrigger - Config invalid',appConfig===null||appConfig===void 0?void 0:appConfig.pushOptIn);return _context.abrupt("return");case 7:_context.next=9;return dispatch(requestAppPermissionStatus({permissionId:PERMISSION_ID_PUSH}));case 9:_ref4=_context.sent;pushStatus=_ref4.status;if(!(pushStatus!==PERMISSION_STATUS_NOT_DETERMINED)){_context.next=13;break;}return _context.abrupt("return");case 13:dispatch(increaseCountAction());state=getPushOptInTriggerState(getState());configValue=appStarts;resetAction=resetAppStartCount;resetCountState=state.appStartResetCount;configCountState=state.appStartCount;if(configKey==='ordersPlaced'){configValue=ordersPlaced;resetAction=resetOrdersPlacedCount;resetCountState=state.ordersPlacedResetCount;configCountState=state.ordersPlacedCount;}if(!(state.rejectionCount>=rejectionMaxCount)){_context.next=22;break;}return _context.abrupt("return");case 22:mustShowModal=Number(configValue.value)>0&&configCountState>=configValue.value;hasRepeats=configValue.repeats===null||resetCountState<=configValue.repeats;minDaysElapsed=Date.now()-new Date(state.lastPopupAt)>=minDaysBetweenOptIns*DAY_IN_MS;if(mustShowModal&&hasRepeats&&minDaysElapsed){meta=getPushOptInTrackingMeta(getState());dispatch(softOptInShown({meta:meta}));dispatch(setLastPopupTimestamp());dispatch(resetAction());dispatch(showPushOptInModal(configKey));}case 26:case"end":return _context.stop();}},_callee);}));return function showOptInAfterChecks(_x,_x2,_x3){return _ref3.apply(this,arguments);};}();// event subscriber to handle app start based push opt in
subscribe(cookieConsentInitialized$,/*#__PURE__*/function(){var _ref6=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(_ref5){var dispatch,getState;return _regeneratorRuntime.wrap(function _callee2$(_context2){while(1)switch(_context2.prev=_context2.next){case 0:dispatch=_ref5.dispatch,getState=_ref5.getState;_context2.next=3;return showOptInAfterChecks({dispatch:dispatch,getState:getState},'appStarts',increaseAppStartCount);case 3:case"end":return _context2.stop();}},_callee2);}));return function(_x4){return _ref6.apply(this,arguments);};}());// event subscriber to handle order based push opt in
subscribe(cookieConsentInitialized$,function(_ref7){var dispatch=_ref7.dispatch,getState=_ref7.getState;event.addCallback('checkoutSuccess',/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(){return _regeneratorRuntime.wrap(function _callee3$(_context3){while(1)switch(_context3.prev=_context3.next){case 0:_context3.next=2;return showOptInAfterChecks({dispatch:dispatch,getState:getState},'ordersPlaced',increaseOrdersPlacedCount);case 2:case"end":return _context3.stop();}},_callee3);})));});subscribe(increaseRejectionCount$,function(_ref9){var dispatch=_ref9.dispatch;dispatch(increaseRejectionCount());});}