infoalloggi-monorepo/apps/infoalloggi/src/forms/FormBanners.tsx

388 lines
9.5 KiB
TypeScript
Raw Normal View History

import { z } from "zod/v4";
2025-08-04 17:45:44 +02:00
import {
2025-08-28 18:27:07 +02:00
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
2025-08-04 17:45:44 +02:00
} from "~/components/custom_ui/form";
import Input from "~/components/custom_ui/input";
2025-08-28 18:27:07 +02:00
import { IconMatrixTooltip } from "~/components/IconComponents";
2025-08-04 17:45:44 +02:00
import { Button } from "~/components/ui/button";
import {
2025-08-28 18:27:07 +02:00
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
2025-08-04 17:45:44 +02:00
} from "~/components/ui/select";
import { Switch } from "~/components/ui/switch";
2025-08-28 18:27:07 +02:00
import { useZodForm } from "~/lib/zodForm";
import type { Banners, BannersIdbanner } from "~/schemas/public/Banners";
2025-08-04 17:45:44 +02:00
type FormProps = {
2025-08-28 18:27:07 +02:00
initialValues: Banners | null;
submitMutation: (values: Banners) => void;
del?: (idbanner: BannersIdbanner) => void;
2025-08-04 17:45:44 +02:00
};
export const FormBanners = ({
2025-08-28 18:27:07 +02:00
initialValues,
submitMutation,
del,
2025-08-04 17:45:44 +02:00
}: FormProps) => {
2025-08-28 18:27:07 +02:00
const Schema = z.object({
2025-08-29 16:18:32 +02:00
color: z.string().nullable(),
2025-08-28 18:27:07 +02:00
cta_href: z.string().nullable(),
cta_icon: z.string().nullable(),
cta_text: z.string().nullable(),
2025-08-29 16:18:32 +02:00
has_cta: z.boolean(),
hide_duration: z.number().nullable(),
idbanner: z.custom<BannersIdbanner>(),
2025-08-28 18:27:07 +02:00
is_active: z.boolean(),
2025-08-29 16:18:32 +02:00
is_unskippable: z.boolean(),
2025-08-28 18:27:07 +02:00
show_private: z.boolean(),
2025-08-29 16:18:32 +02:00
show_public: z.boolean(),
testo: z.string().nullable(),
titolo: z.string().nullable(),
2025-08-28 18:27:07 +02:00
});
type FormValues = z.infer<typeof Schema>;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
let defaultValues: FormValues;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (!initialValues) {
defaultValues = {
2025-08-29 16:18:32 +02:00
color: "",
2025-08-28 18:27:07 +02:00
cta_href: "",
cta_icon: "arrowR",
cta_text: "",
2025-08-29 16:18:32 +02:00
has_cta: false,
hide_duration: 1,
idbanner: "" as BannersIdbanner,
2025-08-28 18:27:07 +02:00
is_active: false,
2025-08-29 16:18:32 +02:00
is_unskippable: false,
2025-08-28 18:27:07 +02:00
show_private: false,
2025-08-29 16:18:32 +02:00
show_public: true,
testo: "",
titolo: "",
2025-08-28 18:27:07 +02:00
};
} else {
defaultValues = {
...initialValues,
};
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
z.config(z.locales.it());
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const form = useZodForm(Schema, {
defaultValues: defaultValues,
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
function onSubmit(fields: FormValues) {
const values = {
...fields,
};
submitMutation(values);
}
const COLORI_OPTIONS = ["blu", "rosso", "verde", "indaco"];
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return (
<>
<Form {...form}>
<form
className="space-y-8 px-0.5"
2025-08-29 16:18:32 +02:00
onSubmit={form.handleSubmit(onSubmit)}
2025-08-28 18:27:07 +02:00
>
<FormField
control={form.control}
name="idbanner"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="idbanner">Codice</FormLabel>
<FormMessage />
</div>
<FormControl>
<Input placeholder="" {...field} id="idbanner" />
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="titolo"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="titolo">Titolo</FormLabel>
<FormMessage />
</div>
<FormControl>
<Input
placeholder=""
{...field}
id="titolo"
value={field.value || ""}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="testo"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="testo">Testo</FormLabel>
<FormMessage />
</div>
<FormControl>
<Input
placeholder=""
{...field}
id="testo"
value={field.value || ""}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="is_unskippable"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="unskippable">Non Saltabile</FormLabel>
<FormControl>
<Switch
checked={field.value}
className="data-[state=checked]:bg-neutral-700"
2025-08-29 16:18:32 +02:00
id="unskippable"
onCheckedChange={field.onChange}
2025-08-28 18:27:07 +02:00
/>
</FormControl>
<FormMessage />
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="has_cta"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="hcta">Ha Link</FormLabel>
<FormControl>
<Switch
checked={field.value}
className="data-[state=checked]:bg-neutral-700"
2025-08-29 16:18:32 +02:00
id="hcta"
onCheckedChange={field.onChange}
2025-08-28 18:27:07 +02:00
/>
</FormControl>
<FormMessage />
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="hide_duration"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="durata">
Per quanto il banner stà nascosto
</FormLabel>
<FormMessage />
</div>
<FormControl>
<Input
placeholder=""
2025-08-29 16:18:32 +02:00
type="number"
2025-08-28 18:27:07 +02:00
{...field}
id="durata"
onChange={(e) => {
field.onChange(Number.parseInt(e.target.value));
}}
2025-08-29 16:18:32 +02:00
value={field.value || 1}
2025-08-28 18:27:07 +02:00
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="cta_href"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="cta_href">Indirizzo Link</FormLabel>
<FormMessage />
</div>
<FormControl>
<Input
placeholder=""
{...field}
id="cta_href"
value={field.value || ""}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="cta_icon"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
2025-08-29 16:18:32 +02:00
<FormLabel className="flex flex-row gap-1" htmlFor="cta_icon">
2025-08-28 18:27:07 +02:00
Icona Link - <IconMatrixTooltip />
</FormLabel>
<FormMessage />
</div>
<FormControl>
<Input
placeholder=""
{...field}
id="cta_icon"
value={field.value || ""}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="cta_text"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="cta_text">Testo Link</FormLabel>
<FormMessage />
</div>
<FormControl>
<Input
placeholder=""
{...field}
id="cta_text"
value={field.value || ""}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="color"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="select-color">Colore</FormLabel>
<FormMessage />
</div>
<Select
defaultValue={field.value || ""}
2025-08-29 16:18:32 +02:00
onValueChange={field.onChange}
2025-08-28 18:27:07 +02:00
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a verified email to display" />
</SelectTrigger>
</FormControl>
<SelectContent id="select-color">
{COLORI_OPTIONS.map((option) => (
<SelectItem key={option} value={option}>
{option}
</SelectItem>
))}
</SelectContent>
</Select>
</FormItem>
)}
/>
<FormField
control={form.control}
name="is_active"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="attivo">Attivo</FormLabel>
<FormControl>
<Switch
checked={field.value}
className="data-[state=checked]:bg-neutral-700"
2025-08-29 16:18:32 +02:00
id="attivo"
onCheckedChange={field.onChange}
2025-08-28 18:27:07 +02:00
/>
</FormControl>
<FormMessage />
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="show_public"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="pub">Mostra nel pubblico</FormLabel>
<FormControl>
<Switch
checked={field.value}
className="data-[state=checked]:bg-neutral-700"
2025-08-29 16:18:32 +02:00
id="pub"
onCheckedChange={field.onChange}
2025-08-28 18:27:07 +02:00
/>
</FormControl>
<FormMessage />
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="show_private"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="priv">Mostra in area riservata</FormLabel>
<FormControl>
<Switch
checked={field.value}
className="data-[state=checked]:bg-neutral-700"
2025-08-29 16:18:32 +02:00
defaultChecked={field.value}
id="priv"
onCheckedChange={field.onChange}
2025-08-28 18:27:07 +02:00
/>
</FormControl>
<FormMessage />
</div>
</FormItem>
)}
/>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<div className="flex flex-row items-center justify-between">
<Button type="submit">Salva</Button>
{initialValues?.idbanner && del && (
<Button
2025-08-29 16:18:32 +02:00
onClick={() => del(initialValues.idbanner)}
2025-08-28 18:27:07 +02:00
type="button"
variant="destructive"
>
Cancella
</Button>
)}
</div>
</form>
</Form>
</>
);
2025-08-04 17:45:44 +02:00
};