Refactor cookie banner forms to react-hook-form
Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -22,8 +22,7 @@ import { graphql } from "relay-runtime";
|
|||||||
import type { CookieBannerCookiesPageDeleteMutation } from "#/__generated__/core/CookieBannerCookiesPageDeleteMutation.graphql";
|
import type { CookieBannerCookiesPageDeleteMutation } from "#/__generated__/core/CookieBannerCookiesPageDeleteMutation.graphql";
|
||||||
import type { CookieBannerCookiesPageQuery } from "#/__generated__/core/CookieBannerCookiesPageQuery.graphql";
|
import type { CookieBannerCookiesPageQuery } from "#/__generated__/core/CookieBannerCookiesPageQuery.graphql";
|
||||||
|
|
||||||
import { CategoryDialog } from "../_components/CategoryDialog";
|
import { CategoryDialog } from "./_components/CategoryDialog";
|
||||||
|
|
||||||
import { CategorySection } from "./_components/CategorySection";
|
import { CategorySection } from "./_components/CategorySection";
|
||||||
|
|
||||||
export const cookieBannerCookiesPageQuery = graphql`
|
export const cookieBannerCookiesPageQuery = graphql`
|
||||||
|
|||||||
@@ -15,10 +15,16 @@
|
|||||||
import { toMaxAgeSeconds } from "@probo/helpers";
|
import { toMaxAgeSeconds } from "@probo/helpers";
|
||||||
import { useTranslate } from "@probo/i18n";
|
import { useTranslate } from "@probo/i18n";
|
||||||
import { Button, DurationInput, Input, Td, Tr } from "@probo/ui";
|
import { Button, DurationInput, Input, Td, Tr } from "@probo/ui";
|
||||||
import { useState } from "react";
|
import { Controller, useForm } from "react-hook-form";
|
||||||
|
|
||||||
import type { CookieEntry } from "./CategorySection";
|
import type { CookieEntry } from "./CategorySection";
|
||||||
|
|
||||||
|
interface CookieFormValues {
|
||||||
|
name: string;
|
||||||
|
duration: { value: string; unit: string };
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface AddCookieRowProps {
|
interface AddCookieRowProps {
|
||||||
isUpdating: boolean;
|
isUpdating: boolean;
|
||||||
onSave: (cookie: CookieEntry) => void;
|
onSave: (cookie: CookieEntry) => void;
|
||||||
@@ -31,16 +37,20 @@ export function AddCookieRow({
|
|||||||
onCancel,
|
onCancel,
|
||||||
}: AddCookieRowProps) {
|
}: AddCookieRowProps) {
|
||||||
const { __ } = useTranslate();
|
const { __ } = useTranslate();
|
||||||
const [name, setName] = useState("");
|
|
||||||
const [durationValue, setDurationValue] = useState("");
|
|
||||||
const [durationUnit, setDurationUnit] = useState("days");
|
|
||||||
const [description, setDescription] = useState("");
|
|
||||||
|
|
||||||
const handleSave = () => {
|
const { register, handleSubmit, control } = useForm<CookieFormValues>({
|
||||||
|
defaultValues: {
|
||||||
|
name: "",
|
||||||
|
duration: { value: "", unit: "days" },
|
||||||
|
description: "",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const onSubmit = (data: CookieFormValues) => {
|
||||||
onSave({
|
onSave({
|
||||||
name,
|
name: data.name,
|
||||||
maxAgeSeconds: toMaxAgeSeconds(durationValue, durationUnit),
|
maxAgeSeconds: toMaxAgeSeconds(data.duration.value, data.duration.unit),
|
||||||
description,
|
description: data.description,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -48,30 +58,34 @@ export function AddCookieRow({
|
|||||||
<Tr>
|
<Tr>
|
||||||
<Td className="pr-3">
|
<Td className="pr-3">
|
||||||
<Input
|
<Input
|
||||||
value={name}
|
{...register("name")}
|
||||||
onChange={e => setName(e.target.value)}
|
|
||||||
placeholder={__("Cookie name")}
|
placeholder={__("Cookie name")}
|
||||||
/>
|
/>
|
||||||
</Td>
|
</Td>
|
||||||
<Td className="pr-3">
|
<Td className="pr-3">
|
||||||
<DurationInput
|
<Controller
|
||||||
value={durationValue}
|
name="duration"
|
||||||
unit={durationUnit}
|
control={control}
|
||||||
onValueChange={setDurationValue}
|
render={({ field }) => (
|
||||||
onUnitChange={setDurationUnit}
|
<DurationInput
|
||||||
|
value={field.value.value}
|
||||||
|
unit={field.value.unit}
|
||||||
|
onValueChange={v => field.onChange({ ...field.value, value: v })}
|
||||||
|
onUnitChange={u => field.onChange({ ...field.value, unit: u })}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
/>
|
/>
|
||||||
</Td>
|
</Td>
|
||||||
<Td className="pr-3">
|
<Td className="pr-3">
|
||||||
<Input
|
<Input
|
||||||
value={description}
|
{...register("description")}
|
||||||
onChange={e => setDescription(e.target.value)}
|
|
||||||
placeholder={__("Description")}
|
placeholder={__("Description")}
|
||||||
/>
|
/>
|
||||||
</Td>
|
</Td>
|
||||||
<Td>
|
<Td>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<Button
|
<Button
|
||||||
onClick={handleSave}
|
onClick={() => void handleSubmit(onSubmit)()}
|
||||||
disabled={isUpdating}
|
disabled={isUpdating}
|
||||||
>
|
>
|
||||||
{__("Save")}
|
{__("Save")}
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ import {
|
|||||||
Textarea,
|
Textarea,
|
||||||
useToast,
|
useToast,
|
||||||
} from "@probo/ui";
|
} from "@probo/ui";
|
||||||
import { useState } from "react";
|
import { useEffect } from "react";
|
||||||
|
import { useForm, useWatch } from "react-hook-form";
|
||||||
import { useMutation } from "react-relay";
|
import { useMutation } from "react-relay";
|
||||||
import { graphql } from "relay-runtime";
|
import { graphql } from "relay-runtime";
|
||||||
|
|
||||||
@@ -60,6 +61,12 @@ const createMutation = graphql`
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
interface CategoryFormValues {
|
||||||
|
name: string;
|
||||||
|
slug: string;
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface CategoryDialogProps {
|
interface CategoryDialogProps {
|
||||||
cookieBannerId: string;
|
cookieBannerId: string;
|
||||||
connectionId: string;
|
connectionId: string;
|
||||||
@@ -78,38 +85,33 @@ export function CategoryDialog({
|
|||||||
|
|
||||||
const [create, isCreating] = useMutation<CategoryDialogCreateMutation>(createMutation);
|
const [create, isCreating] = useMutation<CategoryDialogCreateMutation>(createMutation);
|
||||||
|
|
||||||
const [name, setName] = useState("");
|
const { register, handleSubmit, setValue, control, formState } = useForm<CategoryFormValues>({
|
||||||
const [slug, setSlug] = useState("");
|
defaultValues: { name: "", slug: "", description: "" },
|
||||||
const [slugTouched, setSlugTouched] = useState(false);
|
});
|
||||||
const [description, setDescription] = useState("");
|
|
||||||
|
|
||||||
const handleNameChange = (value: string) => {
|
const nameValue = useWatch({ control, name: "name" });
|
||||||
setName(value);
|
|
||||||
if (!slugTouched) {
|
useEffect(() => {
|
||||||
setSlug(
|
if (!formState.dirtyFields.slug) {
|
||||||
value
|
setValue(
|
||||||
|
"slug",
|
||||||
|
nameValue
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.replace(/[^a-z0-9]+/g, "-")
|
.replace(/[^a-z0-9]+/g, "-")
|
||||||
.replace(/^-|-$/g, ""),
|
.replace(/^-|-$/g, ""),
|
||||||
|
{ shouldDirty: false },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
}, [nameValue, formState.dirtyFields.slug, setValue]);
|
||||||
|
|
||||||
const handleSlugChange = (value: string) => {
|
|
||||||
setSlugTouched(true);
|
|
||||||
setSlug(value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmit = (e: React.FormEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
|
|
||||||
|
const onSubmit = (data: CategoryFormValues) => {
|
||||||
create({
|
create({
|
||||||
variables: {
|
variables: {
|
||||||
input: {
|
input: {
|
||||||
cookieBannerId,
|
cookieBannerId,
|
||||||
name,
|
name: data.name,
|
||||||
slug,
|
slug: data.slug,
|
||||||
description,
|
description: data.description,
|
||||||
rank: nextRank,
|
rank: nextRank,
|
||||||
},
|
},
|
||||||
connections: [connectionId],
|
connections: [connectionId],
|
||||||
@@ -131,18 +133,23 @@ export function CategoryDialog({
|
|||||||
title={__("Add Category")}
|
title={__("Add Category")}
|
||||||
className="max-w-lg"
|
className="max-w-lg"
|
||||||
>
|
>
|
||||||
<form onSubmit={handleSubmit}>
|
<form onSubmit={e => void handleSubmit(onSubmit)(e)}>
|
||||||
<DialogContent padded className="space-y-4">
|
<DialogContent padded className="space-y-4">
|
||||||
<Field label={__("Name")}>
|
<Field label={__("Name")}>
|
||||||
<Input value={name} onChange={e => handleNameChange(e.target.value)} required />
|
<Input {...register("name")} required />
|
||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
<Field label={__("Slug")} help={__("Used as the data-cookie-consent attribute value")}>
|
<Field label={__("Slug")} help={__("Used as the data-cookie-consent attribute value")}>
|
||||||
<Input value={slug} onChange={e => handleSlugChange(e.target.value)} required pattern="[a-z0-9]+(-[a-z0-9]+)*" />
|
<Input
|
||||||
|
{...register("slug", {
|
||||||
|
pattern: /^[a-z0-9]+(-[a-z0-9]+)*$/,
|
||||||
|
})}
|
||||||
|
required
|
||||||
|
/>
|
||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
<Field label={__("Description")}>
|
<Field label={__("Description")}>
|
||||||
<Textarea value={description} onChange={e => setDescription(e.target.value)} required rows={2} />
|
<Textarea {...register("description")} required rows={2} />
|
||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
import { useTranslate } from "@probo/i18n";
|
import { useTranslate } from "@probo/i18n";
|
||||||
import { Button, Input, Textarea } from "@probo/ui";
|
import { Button, Input, Textarea } from "@probo/ui";
|
||||||
import { useState } from "react";
|
import { Controller, useForm } from "react-hook-form";
|
||||||
|
|
||||||
const GCM_CONSENT_TYPES = [
|
const GCM_CONSENT_TYPES = [
|
||||||
"analytics_storage",
|
"analytics_storage",
|
||||||
@@ -26,6 +26,14 @@ const GCM_CONSENT_TYPES = [
|
|||||||
"security_storage",
|
"security_storage",
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
|
interface CategoryFormValues {
|
||||||
|
name: string;
|
||||||
|
slug: string;
|
||||||
|
description: string;
|
||||||
|
gcmConsentTypes: string[];
|
||||||
|
posthogConsent: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
interface EditCategoryFormProps {
|
interface EditCategoryFormProps {
|
||||||
name: string;
|
name: string;
|
||||||
slug: string;
|
slug: string;
|
||||||
@@ -50,36 +58,35 @@ export function EditCategoryForm({
|
|||||||
onCancel,
|
onCancel,
|
||||||
}: EditCategoryFormProps) {
|
}: EditCategoryFormProps) {
|
||||||
const { __ } = useTranslate();
|
const { __ } = useTranslate();
|
||||||
const [editName, setEditName] = useState(name);
|
|
||||||
const [editSlug, setEditSlug] = useState(slug);
|
|
||||||
const [editDescription, setEditDescription] = useState(description);
|
|
||||||
const [editGcmTypes, setEditGcmTypes] = useState<string[]>(gcmConsentTypes);
|
|
||||||
const [editPosthogConsent, setEditPosthogConsent] = useState(posthogConsent);
|
|
||||||
|
|
||||||
const toggleGcmType = (type: string) => {
|
const { register, handleSubmit, control } = useForm<CategoryFormValues>({
|
||||||
setEditGcmTypes(prev =>
|
defaultValues: {
|
||||||
prev.includes(type)
|
name,
|
||||||
? prev.filter(t => t !== type)
|
slug,
|
||||||
: [...prev, type],
|
description,
|
||||||
);
|
gcmConsentTypes,
|
||||||
|
posthogConsent,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const onSubmit = (data: CategoryFormValues) => {
|
||||||
|
onSave(data.name, data.slug, data.description, data.gcmConsentTypes, data.posthogConsent);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
<Input
|
<Input
|
||||||
value={editName}
|
{...register("name")}
|
||||||
onChange={e => setEditName(e.target.value)}
|
|
||||||
placeholder={__("Category name")}
|
placeholder={__("Category name")}
|
||||||
/>
|
/>
|
||||||
<Input
|
<Input
|
||||||
value={editSlug}
|
{...register("slug", {
|
||||||
onChange={e => setEditSlug(e.target.value)}
|
pattern: /^[a-z0-9]+(-[a-z0-9]+)*$/,
|
||||||
|
})}
|
||||||
placeholder={__("Category slug")}
|
placeholder={__("Category slug")}
|
||||||
pattern="[a-z0-9]+(-[a-z0-9]+)*"
|
|
||||||
/>
|
/>
|
||||||
<Textarea
|
<Textarea
|
||||||
value={editDescription}
|
{...register("description")}
|
||||||
onChange={e => setEditDescription(e.target.value)}
|
|
||||||
placeholder={__("Category description")}
|
placeholder={__("Category description")}
|
||||||
rows={2}
|
rows={2}
|
||||||
/>
|
/>
|
||||||
@@ -91,20 +98,33 @@ export function EditCategoryForm({
|
|||||||
{__("Select the Google Consent Mode signals this category controls.")}
|
{__("Select the Google Consent Mode signals this category controls.")}
|
||||||
</p>
|
</p>
|
||||||
<div className="flex flex-wrap gap-2">
|
<div className="flex flex-wrap gap-2">
|
||||||
{GCM_CONSENT_TYPES.map(type => (
|
<Controller
|
||||||
<label
|
name="gcmConsentTypes"
|
||||||
key={type}
|
control={control}
|
||||||
className="flex items-center gap-1.5 text-xs cursor-pointer"
|
render={({ field }) => (
|
||||||
>
|
<>
|
||||||
<input
|
{GCM_CONSENT_TYPES.map(type => (
|
||||||
type="checkbox"
|
<label
|
||||||
checked={editGcmTypes.includes(type)}
|
key={type}
|
||||||
onChange={() => toggleGcmType(type)}
|
className="flex items-center gap-1.5 text-xs cursor-pointer"
|
||||||
className="rounded"
|
>
|
||||||
/>
|
<input
|
||||||
<code className="font-mono">{type}</code>
|
type="checkbox"
|
||||||
</label>
|
checked={field.value.includes(type)}
|
||||||
))}
|
onChange={() => {
|
||||||
|
const next = field.value.includes(type)
|
||||||
|
? field.value.filter(t => t !== type)
|
||||||
|
: [...field.value, type];
|
||||||
|
field.onChange(next);
|
||||||
|
}}
|
||||||
|
className="rounded"
|
||||||
|
/>
|
||||||
|
<code className="font-mono">{type}</code>
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{kind === "NORMAL" && (
|
{kind === "NORMAL" && (
|
||||||
@@ -118,8 +138,7 @@ export function EditCategoryForm({
|
|||||||
<label className="flex items-center gap-1.5 text-xs cursor-pointer">
|
<label className="flex items-center gap-1.5 text-xs cursor-pointer">
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
checked={editPosthogConsent}
|
{...register("posthogConsent")}
|
||||||
onChange={() => setEditPosthogConsent(prev => !prev)}
|
|
||||||
className="rounded"
|
className="rounded"
|
||||||
/>
|
/>
|
||||||
<span>{__("Opt in/out of PostHog tracking")}</span>
|
<span>{__("Opt in/out of PostHog tracking")}</span>
|
||||||
@@ -128,10 +147,7 @@ export function EditCategoryForm({
|
|||||||
)}
|
)}
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => void handleSubmit(onSubmit)()}
|
||||||
if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(editSlug)) return;
|
|
||||||
onSave(editName, editSlug, editDescription, editGcmTypes, editPosthogConsent);
|
|
||||||
}}
|
|
||||||
disabled={isUpdating}
|
disabled={isUpdating}
|
||||||
>
|
>
|
||||||
{isUpdating ? __("Saving...") : __("Save")}
|
{isUpdating ? __("Saving...") : __("Save")}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
import { fromMaxAgeSeconds, toMaxAgeSeconds } from "@probo/helpers";
|
import { fromMaxAgeSeconds, toMaxAgeSeconds } from "@probo/helpers";
|
||||||
import { useTranslate } from "@probo/i18n";
|
import { useTranslate } from "@probo/i18n";
|
||||||
import { Button, DurationInput, Input, Td, Tr } from "@probo/ui";
|
import { Button, DurationInput, Input, Td, Tr } from "@probo/ui";
|
||||||
import { useState } from "react";
|
import { Controller, useForm } from "react-hook-form";
|
||||||
import { useFragment } from "react-relay";
|
import { useFragment } from "react-relay";
|
||||||
import { graphql } from "relay-runtime";
|
import { graphql } from "relay-runtime";
|
||||||
|
|
||||||
@@ -31,6 +31,12 @@ export const editCookieRowFragment = graphql`
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
interface CookieFormValues {
|
||||||
|
name: string;
|
||||||
|
duration: { value: string; unit: string };
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface EditCookieRowProps {
|
interface EditCookieRowProps {
|
||||||
cookieKey: EditCookieRowFragment$key;
|
cookieKey: EditCookieRowFragment$key;
|
||||||
isUpdating: boolean;
|
isUpdating: boolean;
|
||||||
@@ -47,16 +53,20 @@ export function EditCookieRow({
|
|||||||
const { __ } = useTranslate();
|
const { __ } = useTranslate();
|
||||||
const cookie = useFragment(editCookieRowFragment, cookieKey);
|
const cookie = useFragment(editCookieRowFragment, cookieKey);
|
||||||
const initial = fromMaxAgeSeconds(cookie.maxAgeSeconds ?? null);
|
const initial = fromMaxAgeSeconds(cookie.maxAgeSeconds ?? null);
|
||||||
const [name, setName] = useState(cookie.displayName);
|
|
||||||
const [durationValue, setDurationValue] = useState(initial.value);
|
|
||||||
const [durationUnit, setDurationUnit] = useState(initial.unit);
|
|
||||||
const [description, setDescription] = useState(cookie.description);
|
|
||||||
|
|
||||||
const handleSave = () => {
|
const { register, handleSubmit, control } = useForm<CookieFormValues>({
|
||||||
|
defaultValues: {
|
||||||
|
name: cookie.displayName,
|
||||||
|
duration: initial,
|
||||||
|
description: cookie.description,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const onSubmit = (data: CookieFormValues) => {
|
||||||
onSave({
|
onSave({
|
||||||
name,
|
name: data.name,
|
||||||
maxAgeSeconds: toMaxAgeSeconds(durationValue, durationUnit),
|
maxAgeSeconds: toMaxAgeSeconds(data.duration.value, data.duration.unit),
|
||||||
description,
|
description: data.description,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -64,30 +74,34 @@ export function EditCookieRow({
|
|||||||
<Tr>
|
<Tr>
|
||||||
<Td className="pr-3">
|
<Td className="pr-3">
|
||||||
<Input
|
<Input
|
||||||
value={name}
|
{...register("name")}
|
||||||
onChange={e => setName(e.target.value)}
|
|
||||||
placeholder={__("Cookie name")}
|
placeholder={__("Cookie name")}
|
||||||
/>
|
/>
|
||||||
</Td>
|
</Td>
|
||||||
<Td className="pr-3">
|
<Td className="pr-3">
|
||||||
<DurationInput
|
<Controller
|
||||||
value={durationValue}
|
name="duration"
|
||||||
unit={durationUnit}
|
control={control}
|
||||||
onValueChange={setDurationValue}
|
render={({ field }) => (
|
||||||
onUnitChange={setDurationUnit}
|
<DurationInput
|
||||||
|
value={field.value.value}
|
||||||
|
unit={field.value.unit}
|
||||||
|
onValueChange={v => field.onChange({ ...field.value, value: v })}
|
||||||
|
onUnitChange={u => field.onChange({ ...field.value, unit: u })}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
/>
|
/>
|
||||||
</Td>
|
</Td>
|
||||||
<Td className="pr-3">
|
<Td className="pr-3">
|
||||||
<Input
|
<Input
|
||||||
value={description}
|
{...register("description")}
|
||||||
onChange={e => setDescription(e.target.value)}
|
|
||||||
placeholder={__("Description")}
|
placeholder={__("Description")}
|
||||||
/>
|
/>
|
||||||
</Td>
|
</Td>
|
||||||
<Td>
|
<Td>
|
||||||
<div className="flex items-center gap-1">
|
<div className="flex items-center gap-1">
|
||||||
<Button
|
<Button
|
||||||
onClick={handleSave}
|
onClick={() => void handleSubmit(onSubmit)()}
|
||||||
disabled={isUpdating}
|
disabled={isUpdating}
|
||||||
>
|
>
|
||||||
{__("Save")}
|
{__("Save")}
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ import { graphql } from "relay-runtime";
|
|||||||
|
|
||||||
import type { CookieBannerDisplayPageQuery } from "#/__generated__/core/CookieBannerDisplayPageQuery.graphql";
|
import type { CookieBannerDisplayPageQuery } from "#/__generated__/core/CookieBannerDisplayPageQuery.graphql";
|
||||||
|
|
||||||
import { CategoryList } from "../_components/CategoryList";
|
import { CategoryList } from "./_components/CategoryList";
|
||||||
|
|
||||||
import { ThemePreview } from "./_components/ThemePreview";
|
import { ThemePreview } from "./_components/ThemePreview";
|
||||||
|
|
||||||
export const cookieBannerDisplayPageQuery = graphql`
|
export const cookieBannerDisplayPageQuery = graphql`
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ import { graphql } from "relay-runtime";
|
|||||||
|
|
||||||
import type { CookieBannerSettingsPageQuery } from "#/__generated__/core/CookieBannerSettingsPageQuery.graphql";
|
import type { CookieBannerSettingsPageQuery } from "#/__generated__/core/CookieBannerSettingsPageQuery.graphql";
|
||||||
|
|
||||||
import { BannerSettingsForm } from "../_components/BannerSettingsForm";
|
import { BannerSettingsForm } from "./_components/BannerSettingsForm";
|
||||||
|
|
||||||
import { CodeSnippets } from "./_components/CodeSnippets";
|
import { CodeSnippets } from "./_components/CodeSnippets";
|
||||||
|
|
||||||
export const cookieBannerSettingsPageQuery = graphql`
|
export const cookieBannerSettingsPageQuery = graphql`
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
import { formatError, type GraphQLError } from "@probo/helpers";
|
import { formatError, type GraphQLError } from "@probo/helpers";
|
||||||
import { useTranslate } from "@probo/i18n";
|
import { useTranslate } from "@probo/i18n";
|
||||||
import { Button, Card, Field, Input, Label, Option, Select, useToast } from "@probo/ui";
|
import { Button, Card, Field, Input, Label, Option, Select, useToast } from "@probo/ui";
|
||||||
import { useState } from "react";
|
import { Controller, useForm } from "react-hook-form";
|
||||||
import { useFragment, useMutation } from "react-relay";
|
import { useFragment, useMutation } from "react-relay";
|
||||||
import { graphql } from "relay-runtime";
|
import { graphql } from "relay-runtime";
|
||||||
|
|
||||||
@@ -56,6 +56,15 @@ const updateBannerMutation = graphql`
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
interface BannerSettingsFormValues {
|
||||||
|
name: string;
|
||||||
|
cookiePolicyUrl: string;
|
||||||
|
privacyPolicyUrl: string;
|
||||||
|
consentExpiryDays: string;
|
||||||
|
consentMode: "OPT_IN" | "OPT_OUT";
|
||||||
|
defaultLanguage: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface BannerSettingsFormProps {
|
interface BannerSettingsFormProps {
|
||||||
cookieBannerKey: BannerSettingsForm_cookieBanner$key;
|
cookieBannerKey: BannerSettingsForm_cookieBanner$key;
|
||||||
}
|
}
|
||||||
@@ -68,26 +77,28 @@ export function BannerSettingsForm({ cookieBannerKey }: BannerSettingsFormProps)
|
|||||||
|
|
||||||
const [updateBanner, isUpdating] = useMutation<BannerSettingsFormMutation>(updateBannerMutation);
|
const [updateBanner, isUpdating] = useMutation<BannerSettingsFormMutation>(updateBannerMutation);
|
||||||
|
|
||||||
const [name, setName] = useState(banner.name);
|
const { register, handleSubmit, control } = useForm<BannerSettingsFormValues>({
|
||||||
const [cookiePolicyUrl, setCookiePolicyUrl] = useState(banner.cookiePolicyUrl);
|
defaultValues: {
|
||||||
const [privacyPolicyUrl, setPrivacyPolicyUrl] = useState(banner.privacyPolicyUrl ?? "");
|
name: banner.name,
|
||||||
const [consentExpiryDays, setConsentExpiryDays] = useState(String(banner.consentExpiryDays));
|
cookiePolicyUrl: banner.cookiePolicyUrl,
|
||||||
const [consentMode, setConsentMode] = useState(banner.consentMode);
|
privacyPolicyUrl: banner.privacyPolicyUrl ?? "",
|
||||||
const [defaultLanguage, setDefaultLanguage] = useState(banner.defaultLanguage);
|
consentExpiryDays: String(banner.consentExpiryDays),
|
||||||
|
consentMode: banner.consentMode,
|
||||||
const handleSubmit = (e: React.FormEvent) => {
|
defaultLanguage: banner.defaultLanguage,
|
||||||
e.preventDefault();
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const onSubmit = (data: BannerSettingsFormValues) => {
|
||||||
updateBanner({
|
updateBanner({
|
||||||
variables: {
|
variables: {
|
||||||
input: {
|
input: {
|
||||||
cookieBannerId: banner.id,
|
cookieBannerId: banner.id,
|
||||||
name,
|
name: data.name,
|
||||||
cookiePolicyUrl,
|
cookiePolicyUrl: data.cookiePolicyUrl,
|
||||||
privacyPolicyUrl: privacyPolicyUrl || undefined,
|
privacyPolicyUrl: data.privacyPolicyUrl || undefined,
|
||||||
consentExpiryDays: parseInt(consentExpiryDays, 10),
|
consentExpiryDays: parseInt(data.consentExpiryDays, 10),
|
||||||
consentMode: consentMode,
|
consentMode: data.consentMode,
|
||||||
defaultLanguage,
|
defaultLanguage: data.defaultLanguage,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
onCompleted() {
|
onCompleted() {
|
||||||
@@ -103,9 +114,9 @@ export function BannerSettingsForm({ cookieBannerKey }: BannerSettingsFormProps)
|
|||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<h3 className="font-medium">{__("Settings")}</h3>
|
<h3 className="font-medium">{__("Settings")}</h3>
|
||||||
<Card className="border p-4">
|
<Card className="border p-4">
|
||||||
<form className="space-y-4" onSubmit={handleSubmit}>
|
<form className="space-y-4" onSubmit={e => void handleSubmit(onSubmit)(e)}>
|
||||||
<Field label={__("Name")}>
|
<Field label={__("Name")}>
|
||||||
<Input value={name} onChange={e => setName(e.target.value)} required />
|
<Input {...register("name")} required />
|
||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
<Field label={__("Origin URL")}>
|
<Field label={__("Origin URL")}>
|
||||||
@@ -113,11 +124,11 @@ export function BannerSettingsForm({ cookieBannerKey }: BannerSettingsFormProps)
|
|||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
<Field label={__("Cookie Policy URL")}>
|
<Field label={__("Cookie Policy URL")}>
|
||||||
<Input value={cookiePolicyUrl} onChange={e => setCookiePolicyUrl(e.target.value)} required />
|
<Input {...register("cookiePolicyUrl")} required />
|
||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
<Field label={__("Privacy Policy URL")}>
|
<Field label={__("Privacy Policy URL")}>
|
||||||
<Input value={privacyPolicyUrl} onChange={e => setPrivacyPolicyUrl(e.target.value)} />
|
<Input {...register("privacyPolicyUrl")} />
|
||||||
</Field>
|
</Field>
|
||||||
|
|
||||||
<div className="grid grid-cols-3 gap-4">
|
<div className="grid grid-cols-3 gap-4">
|
||||||
@@ -125,27 +136,38 @@ export function BannerSettingsForm({ cookieBannerKey }: BannerSettingsFormProps)
|
|||||||
<Label>{__("Consent Expiry (days)")}</Label>
|
<Label>{__("Consent Expiry (days)")}</Label>
|
||||||
<Input
|
<Input
|
||||||
type="number"
|
type="number"
|
||||||
value={consentExpiryDays}
|
{...register("consentExpiryDays")}
|
||||||
onChange={e => setConsentExpiryDays(e.target.value)}
|
|
||||||
min="1"
|
min="1"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>{__("Consent Mode")}</Label>
|
<Label>{__("Consent Mode")}</Label>
|
||||||
<Select value={consentMode} onValueChange={v => setConsentMode(v as "OPT_IN" | "OPT_OUT")}>
|
<Controller
|
||||||
<Option value="OPT_IN">{__("Opt-in")}</Option>
|
name="consentMode"
|
||||||
<Option value="OPT_OUT">{__("Opt-out")}</Option>
|
control={control}
|
||||||
</Select>
|
render={({ field }) => (
|
||||||
|
<Select value={field.value} onValueChange={field.onChange}>
|
||||||
|
<Option value="OPT_IN">{__("Opt-in")}</Option>
|
||||||
|
<Option value="OPT_OUT">{__("Opt-out")}</Option>
|
||||||
|
</Select>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>{__("Default Language")}</Label>
|
<Label>{__("Default Language")}</Label>
|
||||||
<Select value={defaultLanguage} onValueChange={setDefaultLanguage}>
|
<Controller
|
||||||
<Option value="en">{__("English")}</Option>
|
name="defaultLanguage"
|
||||||
<Option value="fr">{__("French")}</Option>
|
control={control}
|
||||||
<Option value="de">{__("German")}</Option>
|
render={({ field }) => (
|
||||||
<Option value="es">{__("Spanish")}</Option>
|
<Select value={field.value} onValueChange={field.onChange}>
|
||||||
</Select>
|
<Option value="en">{__("English")}</Option>
|
||||||
|
<Option value="fr">{__("French")}</Option>
|
||||||
|
<Option value="de">{__("German")}</Option>
|
||||||
|
<Option value="es">{__("Spanish")}</Option>
|
||||||
|
</Select>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -12,31 +12,22 @@
|
|||||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||||
// PERFORMANCE OF THIS SOFTWARE.
|
// PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
const UNITS: [number, string, string][] = [
|
export const DURATION_UNITS: { value: string; label: string; singular: string; seconds: number }[] = [
|
||||||
[365 * 24 * 3600, "year", "years"],
|
{ value: "seconds", label: "seconds", singular: "second", seconds: 1 },
|
||||||
[30 * 24 * 3600, "month", "months"],
|
{ value: "minutes", label: "minutes", singular: "minute", seconds: 60 },
|
||||||
[7 * 24 * 3600, "week", "weeks"],
|
{ value: "hours", label: "hours", singular: "hour", seconds: 3600 },
|
||||||
[24 * 3600, "day", "days"],
|
{ value: "days", label: "days", singular: "day", seconds: 86400 },
|
||||||
[3600, "hour", "hours"],
|
{ value: "weeks", label: "weeks", singular: "week", seconds: 604800 },
|
||||||
[60, "minute", "minutes"],
|
{ value: "months", label: "months", singular: "month", seconds: 2592000 },
|
||||||
];
|
{ value: "years", label: "years", singular: "year", seconds: 31536000 },
|
||||||
|
|
||||||
export const DURATION_UNITS: { value: string; label: string; seconds: number }[] = [
|
|
||||||
{ value: "seconds", label: "seconds", seconds: 1 },
|
|
||||||
{ value: "minutes", label: "minutes", seconds: 60 },
|
|
||||||
{ value: "hours", label: "hours", seconds: 3600 },
|
|
||||||
{ value: "days", label: "days", seconds: 86400 },
|
|
||||||
{ value: "weeks", label: "weeks", seconds: 604800 },
|
|
||||||
{ value: "months", label: "months", seconds: 2592000 },
|
|
||||||
{ value: "years", label: "years", seconds: 31536000 },
|
|
||||||
];
|
];
|
||||||
|
|
||||||
export function humanizeSeconds(seconds: number | null): string {
|
export function humanizeSeconds(seconds: number | null): string {
|
||||||
if (seconds === null || seconds <= 0) return "session";
|
if (seconds === null || seconds <= 0) return "session";
|
||||||
for (const [unit, singular, plural] of UNITS) {
|
for (const u of [...DURATION_UNITS].reverse()) {
|
||||||
if (seconds >= unit && seconds % unit === 0) {
|
if (seconds >= u.seconds && seconds % u.seconds === 0) {
|
||||||
const count = seconds / unit;
|
const count = seconds / u.seconds;
|
||||||
return `${count} ${count === 1 ? singular : plural}`;
|
return `${count} ${count === 1 ? u.singular : u.label}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return `${seconds} ${seconds === 1 ? "second" : "seconds"}`;
|
return `${seconds} ${seconds === 1 ? "second" : "seconds"}`;
|
||||||
|
|||||||
Reference in New Issue
Block a user