@shopgate/engage
Version:
Shopgate's ENGAGE library.
13 lines • 3.89 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{appDidStart$}from'@shopgate/engage/core/streams';import{appSupportsCookieConsent}from'@shopgate/engage/core/helpers';import{grantAppTrackingTransparencyPermission,historyReset,historyPop}from'@shopgate/engage/core/actions';import{softOptInShown}from'@shopgate/engage/core/action-creators';import{handleCookieConsent,showCookieConsentModal,hideCookieConsentModal}from"../action-creators";import{getIsCookieConsentHandled,getAreComfortCookiesAccepted,getAreStatisticsCookiesAccepted,getCookieConsentTrackingMeta}from"../selectors/cookieConsent";import{cookieConsentInitializedByUserInternal$,privacySettingsConfirmedWithoutChangeInternal$,cookieConsentModalShouldToggleInternal$}from"../streams/cookieConsent";/**
* determine whether to show the cookie consent modal at app start
* @param {Function} subscribe The subscribe function
*/export default function cookieConsent(subscribe){subscribe(appDidStart$,/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref){var dispatch,getState,state,isCookieConsentHandled,trackingMeta,comfortCookiesAccepted,statisticsCookiesAccepted;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:dispatch=_ref.dispatch,getState=_ref.getState;state=getState();isCookieConsentHandled=getIsCookieConsentHandled(state);trackingMeta=getCookieConsentTrackingMeta(state);/**
* if merchant has not activated the cookie feature OR if merchant has activated cookie feature
* and user has chosen cookies already trigger stream to run code that depends on the cookie
* consent.
*/if(!isCookieConsentHandled){_context.next=13;break;}comfortCookiesAccepted=getAreComfortCookiesAccepted(state);statisticsCookiesAccepted=getAreStatisticsCookiesAccepted(state);dispatch(handleCookieConsent({comfortCookiesAccepted:comfortCookiesAccepted,statisticsCookiesAccepted:statisticsCookiesAccepted}));if(!(appSupportsCookieConsent()&&(comfortCookiesAccepted||statisticsCookiesAccepted))){_context.next=11;break;}_context.next=11;return dispatch(grantAppTrackingTransparencyPermission({meta:trackingMeta}));case 11:_context.next=15;break;case 13:// if merchant has activated cookie feature but user has not chosen cookies yet:
// show cookie consent modal to make user choose them
dispatch(softOptInShown({meta:trackingMeta}));dispatch(showCookieConsentModal());case 15:case"end":return _context.stop();}},_callee);}));return function(_x){return _ref2.apply(this,arguments);};}());subscribe(cookieConsentInitializedByUserInternal$,function(_ref3){var dispatch=_ref3.dispatch;// Reset history after consent initialization to guarantee an empty history
dispatch(historyReset());});subscribe(privacySettingsConfirmedWithoutChangeInternal$,function(_ref4){var dispatch=_ref4.dispatch;// Remove privacy settings route from route stack when any button was clicked but no settings
// where changed. When something was changed, app will reset via the "analytics" streams.
dispatch(historyPop());});subscribe(cookieConsentModalShouldToggleInternal$,function(_ref5){var dispatch=_ref5.dispatch,showConsentModal=_ref5.showConsentModal;dispatch(showConsentModal?showCookieConsentModal():hideCookieConsentModal());});}