|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useState, useEffect } from "react"; |
| 4 | +import { ButtonsGroup } from "../ButtonsGroup"; |
| 5 | +import { fr } from "../fr"; |
| 6 | +import { type GdprService } from "../gdpr"; |
| 7 | +import { getLink } from "../link"; |
| 8 | +import { partition } from "../tools/partition"; |
| 9 | +import { useGdprStore } from "../useGdprStore"; |
| 10 | +import { type ConsentBannerContentProps } from "./ConsentBannerContent"; |
| 11 | +import { useTranslation } from "./i18n"; |
| 12 | + |
| 13 | +export type ConsentManagerProps = Required<Omit<ConsentBannerContentProps, "siteName">>; |
| 14 | +export function ConsentManager({ |
| 15 | + gdprLinkProps, |
| 16 | + services, |
| 17 | + consentModalButtonProps |
| 18 | +}: ConsentManagerProps) { |
| 19 | + const { Link } = getLink(); |
| 20 | + const setConsent = useGdprStore(state => state.setConsent); |
| 21 | + const setFirstChoiceMade = useGdprStore(state => state.setFirstChoiceMade); |
| 22 | + const consents = useGdprStore(state => state.consents); |
| 23 | + const [accepted, setAccepted] = useState<string[]>([]); |
| 24 | + |
| 25 | + const { t } = useTranslation(); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + setAccepted([ |
| 29 | + ...Object.entries(consents) |
| 30 | + .filter(([, consent]) => consent) |
| 31 | + .map(([name]) => name) |
| 32 | + ]); |
| 33 | + }, [consents]); |
| 34 | + |
| 35 | + const accept = (service?: GdprService) => { |
| 36 | + if (service && !service.mandatory && !accepted.includes(service.name)) { |
| 37 | + return setAccepted([...accepted, service.name]); |
| 38 | + } |
| 39 | + |
| 40 | + const filtered = services |
| 41 | + .filter(service => !service.mandatory) |
| 42 | + .map(service => service.name); |
| 43 | + setAccepted([...new Set([...filtered, ...accepted])]); |
| 44 | + }; |
| 45 | + |
| 46 | + const refuse = (service?: GdprService) => { |
| 47 | + if (service && !service.mandatory && accepted.includes(service.name)) |
| 48 | + return setAccepted(accepted.filter(name => service.name !== name)); |
| 49 | + |
| 50 | + setAccepted([]); |
| 51 | + }; |
| 52 | + |
| 53 | + const confirm = () => { |
| 54 | + const [acceptedServices, refusedServices] = partition(services, service => |
| 55 | + accepted.includes(service.name) |
| 56 | + ); |
| 57 | + acceptedServices.forEach(service => setConsent(service.name, true)); |
| 58 | + refusedServices.forEach(service => setConsent(service.name, false)); |
| 59 | + setFirstChoiceMade(); |
| 60 | + }; |
| 61 | + |
| 62 | + return ( |
| 63 | + <div className="fr-consent-manager"> |
| 64 | + <div className={fr.cx("fr-consent-service", "fr-consent-manager__header")}> |
| 65 | + <fieldset |
| 66 | + className={fr.cx("fr-fieldset", "fr-fieldset--inline")} |
| 67 | + aria-describedby="fr-consent-service__title" |
| 68 | + > |
| 69 | + <legend |
| 70 | + className={fr.cx("fr-consent-service__title")} |
| 71 | + id="fr-consent-service__title" |
| 72 | + > |
| 73 | + {t("all services pref")} |
| 74 | + <br /> |
| 75 | + <Link {...gdprLinkProps}>{t("personal data cookies")}</Link> |
| 76 | + </legend> |
| 77 | + <div className={fr.cx("fr-consent-service__radios")}> |
| 78 | + <ButtonsGroup |
| 79 | + inlineLayoutWhen="always" |
| 80 | + alignment="right" |
| 81 | + buttons={[ |
| 82 | + { |
| 83 | + onClick: () => accept(), |
| 84 | + title: t("accept all"), |
| 85 | + children: t("accept all") |
| 86 | + }, |
| 87 | + { |
| 88 | + onClick: () => refuse(), |
| 89 | + title: t("refuse all"), |
| 90 | + children: t("refuse all"), |
| 91 | + priority: "secondary" |
| 92 | + } |
| 93 | + ]} |
| 94 | + /> |
| 95 | + </div> |
| 96 | + </fieldset> |
| 97 | + </div> |
| 98 | + {services.map((service, index) => ( |
| 99 | + <div className={fr.cx("fr-consent-service")} key={`consent-service-${index}`}> |
| 100 | + <fieldset className={fr.cx("fr-fieldset", "fr-fieldset--inline")}> |
| 101 | + <legend |
| 102 | + aria-describedby={`finality-${index}-desc`} |
| 103 | + className={fr.cx("fr-consent-service__title")} |
| 104 | + > |
| 105 | + {service.title} |
| 106 | + </legend> |
| 107 | + <div className={fr.cx("fr-consent-service__radios")}> |
| 108 | + <div className={fr.cx("fr-radio-group")}> |
| 109 | + <input |
| 110 | + type="radio" |
| 111 | + id={`consent-finality-${index}-accept`} |
| 112 | + name={`consent-finality-${index}`} |
| 113 | + {...(service.mandatory |
| 114 | + ? { disabled: true, checked: true } |
| 115 | + : { checked: accepted.includes(service.name) })} |
| 116 | + readOnly |
| 117 | + onClick={() => accept(service)} |
| 118 | + /> |
| 119 | + <label |
| 120 | + htmlFor={`consent-finality-${index}-accept`} |
| 121 | + className={fr.cx("fr-label")} |
| 122 | + > |
| 123 | + {t("accept")} |
| 124 | + </label> |
| 125 | + </div> |
| 126 | + <div className={fr.cx("fr-radio-group")}> |
| 127 | + <input |
| 128 | + {...(service.mandatory |
| 129 | + ? { disabled: true, checked: false } |
| 130 | + : { checked: !accepted.includes(service.name) })} |
| 131 | + type="radio" |
| 132 | + id={`consent-finality-${index}-refuse`} |
| 133 | + name={`consent-finality-${index}`} |
| 134 | + readOnly |
| 135 | + onClick={() => refuse(service)} |
| 136 | + /> |
| 137 | + <label |
| 138 | + htmlFor={`consent-finality-${index}-refuse`} |
| 139 | + className={fr.cx("fr-label")} |
| 140 | + > |
| 141 | + {t("refuse")} |
| 142 | + </label> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + <p |
| 146 | + id={`finality-${index}-desc`} |
| 147 | + className={fr.cx("fr-consent-service__desc")} |
| 148 | + > |
| 149 | + {service.description} |
| 150 | + </p> |
| 151 | + </fieldset> |
| 152 | + </div> |
| 153 | + ))} |
| 154 | + <ButtonsGroup |
| 155 | + className={fr.cx("fr-consent-manager__buttons")} |
| 156 | + alignment="right" |
| 157 | + inlineLayoutWhen="sm and up" |
| 158 | + buttons={[ |
| 159 | + { |
| 160 | + ...consentModalButtonProps, |
| 161 | + children: t("confirm choices"), |
| 162 | + title: t("confirm choices"), |
| 163 | + onClick: () => confirm() |
| 164 | + } |
| 165 | + ]} |
| 166 | + /> |
| 167 | + </div> |
| 168 | + ); |
| 169 | +} |
0 commit comments