-
Notifications
You must be signed in to change notification settings - Fork 1
/
consent.ts
52 lines (45 loc) · 1.34 KB
/
consent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Controller } from "@hotwired/stimulus";
import { ConsentCategory } from "../consent";
export default class extends Controller {
static targets = ["check"];
declare checkTargets: HTMLInputElement[];
connect(): void {
this.checkTargets.forEach((input) => {
input.checked =
window.ui?.consent.permitted.includes(input.name as ConsentCategory) ||
input.name === "essential";
});
}
permitAll(event: Event): void {
event.preventDefault();
window.ui?.consent.permitAll();
this.closeAll();
}
denyAll(event: Event): void {
event.preventDefault();
window.ui?.consent.denyAll();
this.closeAll();
}
save(event: Event): void {
event.preventDefault();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
window.ui!.consent.permitted = this.checkTargets
.filter((e) => e.checked)
.map((e) => e.name) as ConsentCategory[];
this.closeAll();
}
manage(event: Event): void {
event.preventDefault();
const div = document.body.querySelector(
"#personalization-settings"
) as HTMLDivElement;
if (!div) return;
(this.element as HTMLElement).hidden = true;
div.hidden = false;
}
closeAll(): void {
document
.querySelectorAll("[data-controller='consent']")
.forEach((e: HTMLElement) => (e.hidden = true));
}
}