UNPKG

@shopgate/engage

Version:
46 lines 7.08 kB
var _excluded=["type"];function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}function _objectWithoutProperties(source,excluded){if(source==null)return{};var target=_objectWithoutPropertiesLoose(source,excluded);var key,i;if(Object.getOwnPropertySymbols){var sourceSymbolKeys=Object.getOwnPropertySymbols(source);for(i=0;i<sourceSymbolKeys.length;i++){key=sourceSymbolKeys[i];if(excluded.indexOf(key)>=0)continue;if(!Object.prototype.propertyIsEnumerable.call(source,key))continue;target[key]=source[key];}}return target;}function _objectWithoutPropertiesLoose(source,excluded){if(source==null)return{};var target={};var sourceKeys=Object.keys(source);var key,i;for(i=0;i<sourceKeys.length;i++){key=sourceKeys[i];if(excluded.indexOf(key)>=0)continue;target[key]=source[key];}return target;}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{main$,routeWillEnter$}from'@shopgate/engage/core/streams';import'rxjs/add/observable/of';import{Observable}from'rxjs/Observable';import{makeGetPrivacyPolicyLink}from'@shopgate/engage/page/selectors';import{COOKIE_CONSENT_HANDLED,UPDATE_COOKIE_CONSENT,PRIVACY_SETTINGS_PATTERN}from"../constants";import{getIsCookieConsentHandled}from"../selectors/cookieConsent";/** * Gets triggered when the cookie consent has been updated by the user or handled already. * This stream is only for internal usage and will be triggered independent of changed payload. * @type {Observable} */var cookieConsentSetInternal$=main$.filter(function(_ref){var action=_ref.action;return action.type===UPDATE_COOKIE_CONSENT||action.type===COOKIE_CONSENT_HANDLED;});/** * Gets triggered when the cookie consent has been initially configured by the user or is * handled already. * @type {Observable} */export var cookieConsentInitialized$=cookieConsentSetInternal$.first();/** * Gets triggered when the cookie consent was initialized / handled by user. In that case * the UPDATE_COOKIE_CONSENT action is dispatched. When handled automatically the * COOKIE_CONSENT_HANDLED action is dispatched. */export var cookieConsentInitializedByUserInternal$=cookieConsentInitialized$.filter(function(_ref2){var action=_ref2.action;return action.type===UPDATE_COOKIE_CONSENT;});/** * Gets triggered when the user interacted with the buttons on the privacy settings page, * but no settings was changed. Needed to navigate back to the previous page. * When a setting was changed, the "cookieConsentUpdated$" stream triggers which will cause * an app reload. * @type {Observable} */export var privacySettingsConfirmedWithoutChangeInternal$=cookieConsentSetInternal$.pairwise().filter(function(_ref3){var _ref4=_slicedToArray(_ref3,2),actionPrev=_ref4[0].action,actionCurrent=_ref4[1].action;return actionPrev.comfortCookiesAccepted===actionCurrent.comfortCookiesAccepted&&actionPrev.statisticsCookiesAccepted===actionCurrent.statisticsCookiesAccepted;}).switchMap(function(_ref5){var _ref6=_slicedToArray(_ref5,2),latest=_ref6[1];return Observable.of(latest);});/** * Gets triggered when cookie consent settings changed after initialization * @type {Observable} */export var cookieConsentUpdated$=cookieConsentSetInternal$.pairwise().filter(function(_ref7){var _ref8=_slicedToArray(_ref7,2),actionPrev=_ref8[0].action,actionCurrent=_ref8[1].action;return actionPrev.comfortCookiesAccepted!==actionCurrent.comfortCookiesAccepted||actionPrev.statisticsCookiesAccepted!==actionCurrent.statisticsCookiesAccepted;}).switchMap(function(_ref9){var _ref10=_slicedToArray(_ref9,2),previous=_ref10[0],latest=_ref10[1];var _previous$action=previous.action,type=_previous$action.type,prevPayload=_objectWithoutProperties(_previous$action,_excluded);return Observable.of(_extends({},latest,{action:_extends({},latest.action,{previous:prevPayload})}));});/** * Gets triggered when the cookie consent has been updated by the user or handled already. * @type {Observable} */export var cookieConsentSet$=cookieConsentInitialized$.merge(cookieConsentUpdated$);/** * Gets triggered when the cookie consent has been set either by user or merchant. * @type {Observable} */export var comfortCookiesAccepted$=cookieConsentSet$.filter(function(_ref11){var action=_ref11.action;return action.comfortCookiesAccepted===true;});/** * Gets triggered when the cookie consent has been set either by user or merchant. * @type {Observable} */export var statisticsCookiesAccepted$=cookieConsentSet$.filter(function(_ref12){var action=_ref12.action;return action.statisticsCookiesAccepted===true;});/** * Gets triggered when comfort cookies where accepted first and declined afterwards during an * app session. * @type {Observable} */export var comfortCookiesDeclined$=comfortCookiesAccepted$.switchMap(function(){return cookieConsentSet$.filter(function(_ref13){var action=_ref13.action;return action.comfortCookiesAccepted===false;}).first();});/** * Gets triggered when statistics cookies where accepted first and declined afterwards during an * app session. * @type {Observable} */export var statisticsCookiesDeclined$=statisticsCookiesAccepted$.switchMap(function(){return cookieConsentSet$.filter(function(_ref14){var action=_ref14.action;return action.statisticsCookiesAccepted===false;}).first();});/** * Emit when the cookie consent modal is supposed to be shown / hidden on route changes. * * When cookie consent is not handled yet, the modal should overlay every route, except the * pages with privacy polity and privacy settings when extended consent decisions are taken. */export var cookieConsentModalShouldToggleInternal$=routeWillEnter$// Stop the stream after cookie consent is handled .takeUntil(cookieConsentInitialized$).switchMap(function(input){var action=input.action,getState=input.getState;var isCookieConsentHandled=getIsCookieConsentHandled(getState());var showConsentModal=false;// Only run extended logic when cookie consent is active and still need to be handled if(!isCookieConsentHandled){var _action$route;var pagesWithoutModal=[PRIVACY_SETTINGS_PATTERN,makeGetPrivacyPolicyLink()(getState())];showConsentModal=!pagesWithoutModal.includes(action===null||action===void 0?void 0:(_action$route=action.route)===null||_action$route===void 0?void 0:_action$route.pathname);}return Observable.of(_extends({},input,{showConsentModal:showConsentModal}));});