UNPKG

react-cookie-consent_teset2

Version:

React components to integrate TrustArc Cookie Consent Manager into React and Next.js apps

78 lines (77 loc) 3.39 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { useEffect } from "react"; import { TrustArcVersion } from "./enum/trustarc-version.enum.js"; import { TrustArcBannerID } from "./enum/trustarc-banner.enum.js"; import { TrustArcHelper } from "./helper.js"; import { appSettings } from "./const/appsettings.const.js"; window.trusteSettings = { URL_ADV: appSettings.URL_ADV, URL_PRO: appSettings.URL_PRO }; export const createTrustArcComponents = (CMPID, options = {}) => { options = TrustArcHelper.setDefaultValues(options); const containerSelector = 'script[data-trustarc="true"]'; const isProVersion = options.ccmVersion === TrustArcVersion.Pro; const bannerId = (isProVersion ? TrustArcBannerID.Pro : TrustArcBannerID.Advanced); const TrustArcScript = () => { useEffect(() => { if (document.querySelector(containerSelector)) { TrustArcHelper.logDebug(options.debug, "TrustArc script already exists, skipping injection."); return; } const script = document.createElement("script"); if (options.loadScriptAsync) script.async = true; script.src = TrustArcHelper.getScriptSource(options, CMPID); if (script.src === "") return; script.setAttribute("data-trustarc", "true"); document.head.prepend(script); script.onload = () => { TrustArcHelper.logDebug(options.debug, "TrustArc script loaded."); TrustArcHelper.logDebug(options.debug, "window.truste object:", window.truste); }; return () => { TrustArcHelper.logDebug(options.debug, "TrustArcScript component unmounted, but script remains."); }; }, []); return null; }; const TrustArcConsentBanner = () => { useEffect(() => { TrustArcHelper.logDebug(options.debug, "Consent banner mounted"); TrustArcHelper.repopulateDiv(options, bannerId, bannerId); return () => TrustArcHelper.logDebug(options.debug, "Consent banner unmounted"); }, []); return _jsx("div", { id: bannerId }); }; const TrustArcCookiePreferencesLink = () => { if (options.cookiePreferencesContainer == "") { return null; } useEffect(() => { TrustArcHelper.logDebug(options.debug, "Cookie preferences link mounted"); TrustArcHelper.repopulateDiv(options, bannerId, options.cookiePreferencesContainer); return () => TrustArcHelper.logDebug(options.debug, "Cookie preferences link unmounted"); }, []); return _jsx("div", { id: options.cookiePreferencesContainer }); }; const TrustArcIRMLink = () => { if (options.irmContainer == "") { return null; } useEffect(() => { TrustArcHelper.logDebug(options.debug, "UseEffect TrustArcIRMLink"); TrustArcHelper.repopulateDiv(options, bannerId, options.irmContainer); return () => { }; }, []); TrustArcHelper.logDebug(options.debug, "Loading TrustArcIRMLink"); return _jsx("div", { id: options.irmContainer }); }; return { TrustArcScript, TrustArcConsentBanner, TrustArcCookiePreferencesLink, TrustArcIRMLink, }; };