2025-08-07 14:50:40 +02:00
|
|
|
import { z } from "zod/v4";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { ColorPicker } from "~/components/color_picker";
|
|
|
|
|
import { Confirm } from "~/components/confirm";
|
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";
|
|
|
|
|
import { Button } from "~/components/ui/button";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { useZodForm } from "~/lib/zodForm";
|
2025-08-04 17:45:44 +02:00
|
|
|
import type {
|
2025-08-28 18:27:07 +02:00
|
|
|
Etichette,
|
|
|
|
|
EtichetteIdEtichetta,
|
2025-08-04 17:45:44 +02:00
|
|
|
} from "~/schemas/public/Etichette";
|
|
|
|
|
import { zEtichettaId } from "~/server/utils/zod_types";
|
2025-08-28 18:27:07 +02:00
|
|
|
|
2025-08-04 17:45:44 +02:00
|
|
|
type FormProps = {
|
2025-08-28 18:27:07 +02:00
|
|
|
initialValues: Etichette | null;
|
|
|
|
|
submitMutation: (values: Etichette) => void;
|
|
|
|
|
del?: (id_etichetta: EtichetteIdEtichetta) => void;
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
|
|
|
|
export const FormEtichette = ({
|
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_hex: z.string().max(7),
|
2025-08-28 18:27:07 +02:00
|
|
|
id_etichetta: zEtichettaId,
|
|
|
|
|
title: z.string(),
|
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
|
// This can come from your database or API.
|
|
|
|
|
if (!initialValues) {
|
|
|
|
|
defaultValues = {
|
2025-08-29 16:18:32 +02:00
|
|
|
color_hex: "",
|
2025-08-28 18:27:07 +02:00
|
|
|
id_etichetta: 0 as EtichetteIdEtichetta,
|
|
|
|
|
title: "",
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
defaultValues = {
|
2025-08-29 16:18:32 +02:00
|
|
|
color_hex: initialValues.color_hex,
|
2025-08-28 18:27:07 +02:00
|
|
|
id_etichetta: initialValues.id_etichetta,
|
|
|
|
|
title: initialValues.title,
|
|
|
|
|
};
|
|
|
|
|
}
|
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 = {
|
2025-08-29 16:18:32 +02:00
|
|
|
color_hex: fields.color_hex,
|
2025-08-28 18:27:07 +02:00
|
|
|
id_etichetta: fields.id_etichetta,
|
|
|
|
|
title: fields.title,
|
|
|
|
|
};
|
|
|
|
|
submitMutation(values);
|
|
|
|
|
}
|
|
|
|
|
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="title"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-2">
|
|
|
|
|
<FormLabel htmlFor="value">Titolo</FormLabel>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</div>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Input
|
|
|
|
|
placeholder=""
|
|
|
|
|
{...field}
|
|
|
|
|
id="value"
|
|
|
|
|
value={field.value || ""}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="color_hex"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-2">
|
|
|
|
|
<FormLabel htmlFor="color_hex">Colore</FormLabel>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</div>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<ColorPicker
|
|
|
|
|
id="color_hex"
|
|
|
|
|
onChange={field.onChange}
|
2025-08-29 16:18:32 +02:00
|
|
|
value={field.value || "#FFFFFF"}
|
2025-08-28 18:27:07 +02:00
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
</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" variant="success">
|
|
|
|
|
Salva
|
|
|
|
|
</Button>
|
|
|
|
|
{initialValues?.id_etichetta && del && (
|
|
|
|
|
<Confirm
|
|
|
|
|
description="Questa azione è irreversibile"
|
|
|
|
|
onConfirm={() => del(initialValues.id_etichetta)}
|
2025-08-29 16:18:32 +02:00
|
|
|
title="Cancellare l'etichetta?"
|
2025-08-28 18:27:07 +02:00
|
|
|
>
|
|
|
|
|
<Button variant="destructive">Cancella</Button>
|
|
|
|
|
</Confirm>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</Form>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|