Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,110 @@
|
||||
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>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { useLocation, useSearchParams, useSubmit } 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 CookiesList from "./CookiesList";
|
||||
import { Button } from "../ui/button";
|
||||
|
||||
interface Props {
|
||||
onUpdated?: () => void;
|
||||
}
|
||||
export default function CookieConsentSettings({ onUpdated }: Props) {
|
||||
const { t } = useTranslation();
|
||||
let { userSession } = useRootData();
|
||||
const submit = useSubmit();
|
||||
let location = useLocation();
|
||||
const [searchParams] = useSearchParams();
|
||||
|
||||
const [selectedCookies, setSelectedCookies] = useState<CookieCategory[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const initial: CookieCategory[] = [];
|
||||
allCookieCategories.forEach((cookie) => {
|
||||
if (userSession.cookies.find((f) => f.allowed && f.category === CookieCategory[cookie])) {
|
||||
initial.push(cookie);
|
||||
}
|
||||
});
|
||||
setSelectedCookies(initial);
|
||||
}, [userSession.cookies]);
|
||||
|
||||
function setCookies(selectedCookies: CookieCategory[]) {
|
||||
const form = CookieHelper.getUpdateCookieConsentForm({ selectedCookies, location, searchParams });
|
||||
submit(form, { method: "post", action: "/preferences", preventScrollReset: true });
|
||||
if (onUpdated) {
|
||||
onUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
function toggle(category: CookieCategory) {
|
||||
if (selectedCookies.includes(category)) {
|
||||
setSelectedCookies(selectedCookies.filter((f) => f !== category));
|
||||
} else {
|
||||
setSelectedCookies([...selectedCookies, category]);
|
||||
}
|
||||
}
|
||||
function deny() {
|
||||
setCookies([CookieCategory.REQUIRED]);
|
||||
}
|
||||
function allowSelected() {
|
||||
setCookies(selectedCookies);
|
||||
}
|
||||
function allowAll() {
|
||||
setCookies(allCookieCategories);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<div className="font-extrabold">{t("cookies.settings")}</div>
|
||||
|
||||
<CookiesList selectedCookies={selectedCookies} toggle={toggle} />
|
||||
|
||||
<div className="grid gap-2 sm:grid-cols-3">
|
||||
<Button variant="outline" onClick={deny}>
|
||||
{t("shared.deny")}
|
||||
</Button>
|
||||
<Button variant="outline" onClick={allowSelected}>
|
||||
{t("shared.allowSelected")}
|
||||
</Button>
|
||||
<Button variant="outline" type="button" onClick={allowAll}>
|
||||
{t("shared.allowAll")}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import "@testing-library/jest-dom";
|
||||
import { fireEvent, render, screen } from "@testing-library/react";
|
||||
import type { ComponentProps } from "react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import CookiesList from "./CookiesList";
|
||||
|
||||
vi.mock("react-router", () => ({
|
||||
Link: ({ children, to, ...props }: ComponentProps<"a"> & { to: string }) => (
|
||||
<a href={to} {...props}>
|
||||
{children}
|
||||
</a>
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string, options?: { count?: number }) => (key === "cookies.count" ? `${options?.count} localized cookies` : key),
|
||||
}),
|
||||
}));
|
||||
|
||||
describe("CookiesList", () => {
|
||||
it("renders cookie category counts through i18n", () => {
|
||||
render(<CookiesList editing />);
|
||||
|
||||
expect(screen.getAllByText(/\(\d+ localized cookies\)/).length).toBeGreaterThan(0);
|
||||
expect(screen.queryByText(/\(\d+ cookies\)/)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders cookie expiry values through i18n", () => {
|
||||
render(<CookiesList editing />);
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: /cookies\.categories\.REQUIRED\.name/i }));
|
||||
|
||||
expect(screen.getByText("cookies.expiry.oneMonth")).toBeInTheDocument();
|
||||
expect(screen.queryByText("1 month")).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,96 @@
|
||||
import { Link } from "react-router";
|
||||
import clsx from "clsx";
|
||||
import { Fragment, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { allApplicationCookies, allCookieCategories } from "~/application/cookies/ApplicationCookies";
|
||||
import { CookieCategory } from "~/application/cookies/CookieCategory";
|
||||
import InputCheckbox from "../ui/input/InputCheckbox";
|
||||
|
||||
export default function CookiesList({
|
||||
selectedCookies,
|
||||
toggle,
|
||||
editing,
|
||||
}: {
|
||||
selectedCookies?: CookieCategory[];
|
||||
toggle?: (category: CookieCategory) => void;
|
||||
editing?: boolean;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [openCategories, setOpenCategories] = useState<CookieCategory[]>([]);
|
||||
function isCategoryOpen(category: CookieCategory) {
|
||||
return openCategories.includes(category);
|
||||
}
|
||||
function toggleOpenCategory(category: CookieCategory) {
|
||||
const newOpenCategories = isCategoryOpen(category) ? openCategories.filter((c) => c !== category) : [...openCategories, category];
|
||||
setOpenCategories(newOpenCategories);
|
||||
}
|
||||
return (
|
||||
<div className={clsx("border-border text-foreground bg-secondary overflow-hidden overflow-y-auto rounded-md border", !editing && "max-h-72")}>
|
||||
{allCookieCategories.map((category, idx) => {
|
||||
return (
|
||||
<Fragment key={category}>
|
||||
<div
|
||||
className={clsx(
|
||||
"hover:bg-secondary border-border bg-background flex items-center justify-between px-4 py-2",
|
||||
idx < allCookieCategories.length - 1 && "border-b"
|
||||
)}
|
||||
>
|
||||
<button type="button" onClick={() => toggleOpenCategory(category)} className="flex w-full items-center space-x-2 px-2 py-3 font-medium">
|
||||
<div>{isCategoryOpen(category) ? "-" : "+"}</div>
|
||||
<div>{t("cookies.categories." + CookieCategory[category] + ".name")}</div>
|
||||
<div className="text-muted-foreground text-xs font-normal">
|
||||
({t("cookies.count", { count: allApplicationCookies.filter((f) => f.category === category).length })})
|
||||
</div>
|
||||
</button>
|
||||
{toggle !== undefined && selectedCookies !== undefined && (
|
||||
<InputCheckbox
|
||||
value={selectedCookies.includes(category) || category === CookieCategory.REQUIRED}
|
||||
setValue={() => toggle(category)}
|
||||
disabled={category === CookieCategory.REQUIRED}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
{isCategoryOpen(category) && (
|
||||
<div className="space-y-2 px-4 py-4 pb-2">
|
||||
<div className="text-muted-foreground text-sm">{t("cookies.categories." + CookieCategory[category] + ".description")}</div>
|
||||
|
||||
{allApplicationCookies
|
||||
.filter((f) => f.category === category)
|
||||
.map((item) => {
|
||||
return (
|
||||
<div key={item.name} className="border-border bg-secondary/90 space-y-2 rounded-md border border-dashed p-2 py-2">
|
||||
<div className="flex items-baseline justify-between font-bold">
|
||||
<div>{item.name}</div>
|
||||
{item.href?.startsWith("http") ? (
|
||||
<a target="_blank" rel="noreferrer" href={item.href} className="text-theme-600 text-xs underline">
|
||||
{t("shared.learnMore")}
|
||||
</a>
|
||||
) : item.href ? (
|
||||
<Link to={item.href} className="text-theme-600 text-xs underline">
|
||||
{t("shared.learnMore")}
|
||||
</Link>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="text-muted-foreground text-sm font-light">{item.descriptionKey ? t(item.descriptionKey) : item.description}</div>
|
||||
<div className="border-border w-full border-t" />
|
||||
<div className="mt-2 grid grid-cols-2 gap-2 text-sm">
|
||||
<div>
|
||||
<span className="font-medium">{t("shared.expiry")}:</span> {item.expiryKey ? t(item.expiryKey) : item.expiry ?? "?"}
|
||||
</div>
|
||||
{item.type && (
|
||||
<div>
|
||||
<span className="font-medium">{t("shared.type")}:</span> {item.type ?? "?"}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</Fragment>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user