2025-08-07 14:50:40 +02:00
|
|
|
import { z } from "zod/v4";
|
2025-08-28 18:27:07 +02:00
|
|
|
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";
|
2025-08-28 18:27:07 +02:00
|
|
|
import Tiptap from "~/components/tip-tap/editor";
|
2025-08-04 17:45:44 +02:00
|
|
|
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
|
|
|
TestiEStringhe,
|
|
|
|
|
TestiEStringheStingaId,
|
2025-08-04 17:45:44 +02:00
|
|
|
} from "~/schemas/public/TestiEStringhe";
|
|
|
|
|
|
|
|
|
|
type FormProps = {
|
2025-08-28 18:27:07 +02:00
|
|
|
initialValues: TestiEStringhe | null;
|
|
|
|
|
submitMutation: (values: TestiEStringhe) => void;
|
|
|
|
|
del?: (stringheId: TestiEStringheStingaId) => void;
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
|
|
|
|
export const FormStringhe = ({
|
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({
|
|
|
|
|
stinga_id: z.string(),
|
|
|
|
|
stringa_value: z.string().nullable(),
|
|
|
|
|
});
|
|
|
|
|
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 = {
|
|
|
|
|
stinga_id: "",
|
|
|
|
|
stringa_value: null,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
defaultValues = {
|
|
|
|
|
stinga_id: initialValues.stinga_id,
|
|
|
|
|
stringa_value: initialValues.stringa_value,
|
|
|
|
|
};
|
|
|
|
|
}
|
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 = {
|
|
|
|
|
stinga_id: fields.stinga_id as TestiEStringheStingaId,
|
|
|
|
|
stringa_value: fields.stringa_value,
|
|
|
|
|
};
|
|
|
|
|
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="stinga_id"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-2">
|
|
|
|
|
<FormLabel htmlFor="stinga_id">Codice</FormLabel>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</div>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Input placeholder="" {...field} id="stinga_id" />
|
|
|
|
|
</FormControl>
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="stringa_value"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-2">
|
|
|
|
|
<FormLabel htmlFor="stringa_value">Valore testuale</FormLabel>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</div>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Tiptap
|
|
|
|
|
id="stringa_value"
|
2025-08-29 16:18:32 +02:00
|
|
|
onChange={field.onChange}
|
|
|
|
|
value={field.value || ""}
|
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?.stinga_id && del && (
|
|
|
|
|
<Confirm
|
|
|
|
|
description="Questa azione è irreversibile"
|
|
|
|
|
onConfirm={() => del(initialValues.stinga_id)}
|
2025-08-29 16:18:32 +02:00
|
|
|
title="Cancellare la stringa?"
|
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
|
|
|
};
|