-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React, {ReactElement} from "react"; | ||
import Script from "next/script"; | ||
|
||
export type UsercentricsProps = Readonly<{ | ||
id: string; | ||
}>; | ||
|
||
export function Usercentrics (props: UsercentricsProps): ReactElement | null | ||
{ | ||
return ( | ||
<Script | ||
id="usercentrics-cmp" | ||
data-settings-id={props.id} | ||
src="https://app.usercentrics.eu/browser-ui/latest/loader.js" | ||
async | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"use client"; | ||
|
||
import {useEffect, useState} from "react"; | ||
|
||
type WindowWithUsercentrics = Window & Readonly<{ | ||
UC_UI?: { | ||
showFirstLayer(): void; | ||
}; | ||
}>; | ||
|
||
/** | ||
* The essential key | ||
*/ | ||
type UsercentricsConsentCategories = "essential" | "marketing" | "functional" | "statictics" | string; | ||
type UsercentricsEvent = CustomEvent<{ | ||
ucCategory: Partial<Record<UsercentricsConsentCategories, boolean>>; | ||
}>; | ||
|
||
|
||
|
||
type UsercentricsSettings = Readonly<{ | ||
openConsentManager(): void; | ||
hasAnyConsent: boolean; | ||
/** | ||
* Contains all categories with consent apart from the essential categories | ||
*/ | ||
consent: Record<UsercentricsConsentCategories, boolean>; | ||
}>; | ||
|
||
|
||
/** | ||
* Custom hook to integrate with user centrics | ||
*/ | ||
export function useUsercentrics () : UsercentricsSettings | ||
{ | ||
const [consent, setConsent] = useState<Record<UsercentricsConsentCategories, boolean>>({ | ||
marketing: false, | ||
functional: false, | ||
statistics: false, | ||
}); | ||
|
||
useEffect(() => | ||
{ | ||
const onUsercentricsEvent = (event: UsercentricsEvent) => | ||
{ | ||
const newConsent: Partial<Record<UsercentricsConsentCategories, boolean>> = event.detail.ucCategory; | ||
|
||
setConsent({ | ||
...newConsent, | ||
marketing: newConsent.marketing ?? false, | ||
functional: newConsent.functional ?? false, | ||
statistics: newConsent.statistics ?? false, | ||
}); | ||
}; | ||
|
||
window.addEventListener("ucEvent", onUsercentricsEvent as EventListener); | ||
|
||
return () => | ||
{ | ||
window.removeEventListener("ucEvent", onUsercentricsEvent as EventListener); | ||
} | ||
}, []); | ||
|
||
return { | ||
consent, | ||
hasAnyConsent: Object.keys(consent).some(category => "essential" !== category && consent[category]), | ||
openConsentManager (): void | ||
{ | ||
(window as WindowWithUsercentrics).UC_UI?.showFirstLayer(); | ||
}, | ||
} | ||
} |