All files / apps/huella-legal/layers/tracking/app/plugins consent.client.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 8/8
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      40x 40x   40x   1x   1x 1x         1x   1x               1x         1x 1x 1x                                
import type { TrackerKey } from "../trackers";
 
export default defineNuxtPlugin(({ $i18n }) => {
    const { trackers } = useRuntimeConfig().public;
    const active = (Object.keys(trackers) as TrackerKey[]).filter((key) => trackers[key].id);
 
    if (!active.length) return;
 
    const policyHref = useRouter().resolve(useAppConfig().nav.cookies).href;
 
    onNuxtReady(async () => {
        const [CookieConsent, { TRACKERS }] = await Promise.all([
            import("vanilla-cookieconsent"),
            import("../trackers"),
            import("../assets/consent.css"),
        ]);
        const categories = [...new Set(active.map((key) => TRACKERS[key].category))];
 
        await CookieConsent.run({
            guiOptions: {
                consentModal: { layout: "box", position: "bottom left", equalWeightButtons: true },
                preferencesModal: { layout: "box", equalWeightButtons: true },
            },
            categories: {
                necessary: { enabled: true, readOnly: true },
                ...Object.fromEntries(
                    categories.map((category) => [
                        category,
                        {
                            autoClear: {
                                cookies: active
                                    .filter((key) => TRACKERS[key].category === category)
                                    .flatMap((key) => TRACKERS[key].cookies)
                                    .map((name) => ({ name })),
                                reloadPage: true,
                            },
                        },
                    ]),
                ),
            },
            language: {
                default: "es",
                translations: {
                    es: consentTranslation($i18n.t, categories, policyHref),
                },
            },
        });
    });
});