@primarix/easy-consent
Version:
A lightweight consent management solution for Google Analytics and related services. This is a beta version and should be used with caution.
38 lines (37 loc) • 864 B
JavaScript
// src/reactHooks/useConsent.ts
import { useState } from "react";
var useConsent = (consent) => {
const [choices, setChoices] = useState(consent.state);
const changeChoice = (key, value) => {
return () => {
consent.update(key, value);
setChoices({ ...choices, [key]: value });
};
};
const toggleMode = (mode) => mode === "granted" ? "denied" : "granted";
const grantAll = () => {
consent.acceptAll();
setChoices(consent.state);
};
const denyAll = () => {
consent.rejectAll();
setChoices(consent.state);
};
const updateChoices = (new_values) => {
return () => {
consent.updateMultiple(new_values);
setChoices({ ...consent.state, ...new_values });
};
};
return {
choices,
changeChoice,
updateChoices,
toggleMode,
grantAll,
denyAll
};
};
export {
useConsent
};