@shopgate/engage
Version:
Shopgate's ENGAGE library.
33 lines • 5.39 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{historyPush,grantAppTrackingTransparencyPermission}from'@shopgate/engage/core/actions';import{softOptInSelected}from'@shopgate/engage/core/action-creators';import{updateCookieConsent,hideCookieConsentModal}from"../action-creators";import{PRIVACY_SETTINGS_PATTERN}from"../constants";import{getCookieConsentTrackingMeta,getIsCookieConsentModalVisible,getIsCookieConsentHandled}from"../selectors/cookieConsent";/**
* Broadcasts updates of the cookie consent states and dispatches related tracking events.
* @param {Object} update Updated cookie consent states
* @param {boolean} update.comfortCookiesAccepted Whether comfort cookies
* are accepted.
* @param {boolean} update.statisticsCookiesAccepted Whether statistics cookies
* are accepted.
* @returns {Function}
*/var updateCookieConsentLocal=function updateCookieConsentLocal(update){return function(dispatch,getState){var comfortCookiesAccepted=update.comfortCookiesAccepted,statisticsCookiesAccepted=update.statisticsCookiesAccepted;var state=getState();// Whether the update was triggered by a button from within the modal
var updateFromModal=getIsCookieConsentModalVisible(state);// Whether the cookie consent was already handled when the update came in
var cookieConsentHandled=getIsCookieConsentHandled(state);if(!cookieConsentHandled){// Prepare tracking events when cookie consent wasn't handled yet
var meta=getCookieConsentTrackingMeta(state,{allowComfort:comfortCookiesAccepted,allowStatistics:statisticsCookiesAccepted});var selection='manage';if(updateFromModal){selection=comfortCookiesAccepted&&statisticsCookiesAccepted?'approvedAll':'deniedAll';}dispatch(softOptInSelected({selection:selection,meta:meta}));}// Broadcast the actual state update
dispatch(updateCookieConsent(update));};};/**
* action to be dispatched when the user accepted all cookies in the custom modal
* and native modal should be triggered for setting the permission
* @returns {Function}
*/export var acceptAllCookies=function acceptAllCookies(){return(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch,getState){var meta;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:meta=getCookieConsentTrackingMeta(getState());_context.next=3;return dispatch(grantAppTrackingTransparencyPermission({meta:meta}));case 3:dispatch(updateCookieConsentLocal({comfortCookiesAccepted:true,statisticsCookiesAccepted:true}));dispatch(hideCookieConsentModal());case 5:case"end":return _context.stop();}},_callee);}));return function(_x,_x2){return _ref.apply(this,arguments);};}());};/**
* action to be dispatched when the user accepted the selected cookies in the custom modal
* and native modal should be triggered for setting the permission
* @param {Object} params Action params
* @param {boolean|null} params.comfortCookiesAccepted whether this cookie type was accepted
* by user
* @param {boolean|null} params.statisticsCookiesAccepted whether this cookie type was accepted
* by user
* @returns {Function}
*/export var acceptSelectedCookies=function acceptSelectedCookies(_ref2){var comfortCookiesAccepted=_ref2.comfortCookiesAccepted,statisticsCookiesAccepted=_ref2.statisticsCookiesAccepted;return(/*#__PURE__*/function(){var _ref3=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(dispatch,getState){var meta;return _regeneratorRuntime.wrap(function _callee2$(_context2){while(1)switch(_context2.prev=_context2.next){case 0:if(!(comfortCookiesAccepted||statisticsCookiesAccepted)){_context2.next=4;break;}meta=getCookieConsentTrackingMeta(getState());_context2.next=4;return dispatch(grantAppTrackingTransparencyPermission({meta:meta}));case 4:dispatch(updateCookieConsentLocal({comfortCookiesAccepted:comfortCookiesAccepted,statisticsCookiesAccepted:statisticsCookiesAccepted}));dispatch(hideCookieConsentModal());case 6:case"end":return _context2.stop();}},_callee2);}));return function(_x3,_x4){return _ref3.apply(this,arguments);};}());};/**
* action to be dispatched when the user selected only the required cookies in the custom modal
* and native modal should be triggered for setting the permission
* @returns {Function}
*/export var acceptRequiredCookies=function acceptRequiredCookies(){return function(dispatch){dispatch(updateCookieConsentLocal({comfortCookiesAccepted:false,statisticsCookiesAccepted:false}));dispatch(hideCookieConsentModal());};};/**
* action to be dispatched when the user opted to configure cookie settings in the custom modal
* @returns {Function}
*/export var openPrivacySettings=function openPrivacySettings(){return function(dispatch){dispatch(historyPush({pathname:PRIVACY_SETTINGS_PATTERN}));};};