111 lines
4.7 KiB
TypeScript
111 lines
4.7 KiB
TypeScript
import { Link, useFetcher, useLocation, useSearchParams } from "react-router";
|
|
import { useEffect, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { allCookieCategories } from "~/application/cookies/ApplicationCookies";
|
|
import { CookieCategory } from "~/application/cookies/CookieCategory";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import CookieHelper from "~/utils/helpers/CookieHelper";
|
|
import OpenModal from "../ui/modals/OpenModal";
|
|
import CookieConsentSettings from "./CookieConsentSettings";
|
|
import { Button } from "../ui/button";
|
|
import { ShieldCheckIcon } from "~/components/ui/icons/vendor";
|
|
|
|
export default function CookieConsentBanner() {
|
|
const { t } = useTranslation();
|
|
const fetcher = useFetcher();
|
|
const location = useLocation();
|
|
const isLoading = fetcher.state === "submitting";
|
|
|
|
const { userSession, appConfiguration } = useRootData();
|
|
const [searchParams] = useSearchParams();
|
|
|
|
const [isOpen, setIsOpen] = useState(CookieHelper.requiresCookieConsent(userSession, searchParams));
|
|
const [showCookieSettingsModal, setShowCookieSettingsModal] = useState(false);
|
|
|
|
useEffect(() => {
|
|
setIsOpen(CookieHelper.requiresCookieConsent(userSession, searchParams));
|
|
}, [userSession, searchParams]);
|
|
|
|
if (!appConfiguration.cookies.enabled) {
|
|
return null;
|
|
}
|
|
|
|
function setCookies(selectedCookies: CookieCategory[]) {
|
|
const form = CookieHelper.getUpdateCookieConsentForm({ selectedCookies, location, searchParams });
|
|
let redirectTo = location.pathname + location.search;
|
|
form.set("redirect", redirectTo);
|
|
fetcher.submit(form, {
|
|
method: "post",
|
|
action: "/preferences",
|
|
preventScrollReset: true,
|
|
});
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{isOpen && (
|
|
<div className="relative">
|
|
<div className="fixed inset-x-0 bottom-0 z-30 pb-2 sm:pb-5">
|
|
<div className="mx-auto max-w-5xl px-2 sm:px-6 lg:px-8">
|
|
<div className="bg-background border-border text-foreground max-h-[70vh] overflow-y-auto rounded-lg border p-2 shadow-lg sm:max-h-[50vh] sm:p-3">
|
|
<div className="flex flex-col items-center justify-between space-y-2 sm:flex-row sm:space-y-0">
|
|
<div className="flex items-start space-x-2">
|
|
<div className="bg-primary rounded-full p-1">
|
|
<ShieldCheckIcon className="text-primary-foreground h-6 w-6" />
|
|
</div>
|
|
<div>
|
|
<p className="text-foreground font-medium">
|
|
<span className="md:hidden"> {t("cookies.titleSmall")} </span>
|
|
<span className="hidden md:inline"> {t("cookies.title")} </span>
|
|
</p>
|
|
<p className="text-muted-foreground text-sm font-light">
|
|
<span className="md:hidden">
|
|
{t("cookies.descriptionSmall")}{" "}
|
|
<Link to="/privacy-policy" className="underline">
|
|
{t("shared.learnMore")}
|
|
</Link>
|
|
.{" "}
|
|
</span>
|
|
<span className="hidden md:inline">
|
|
{" "}
|
|
{t("cookies.description")}{" "}
|
|
<Link to="/privacy-policy" className="underline">
|
|
{t("shared.learnMore")}
|
|
</Link>{" "}
|
|
</span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="border-border w-full border border-t sm:hidden"></div>
|
|
<div className="flex w-full shrink-0 flex-col space-y-2 sm:mt-0 sm:w-auto sm:flex-row sm:space-y-0 sm:space-x-2">
|
|
<Button variant="outline" type="button" onClick={() => setShowCookieSettingsModal(true)}>
|
|
{" "}
|
|
{t("cookies.settings")}{" "}
|
|
</Button>
|
|
<Button
|
|
disabled={isLoading}
|
|
// className={clsx(isLoading && "base-spinner")}
|
|
onClick={() => {
|
|
setCookies(allCookieCategories);
|
|
setIsOpen(false);
|
|
}}
|
|
>
|
|
{t("cookies.accept")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{showCookieSettingsModal && (
|
|
<OpenModal onClose={() => setShowCookieSettingsModal(false)}>
|
|
<CookieConsentSettings />
|
|
</OpenModal>
|
|
)}
|
|
</div>
|
|
)}
|
|
</>
|
|
);
|
|
}
|