feat: refactor expires_date validation and update default values in FormAddCodaEmail

This commit is contained in:
Marco Pedone 2026-04-20 12:19:02 +02:00
parent c1c0ad2a2f
commit e46af92bb3

View file

@ -29,8 +29,6 @@ import {
type TemplateType, type TemplateType,
} from "~/server/services/mail-templates"; } from "~/server/services/mail-templates";
type FormProps = { type FormProps = {
submitMutation: (values: { submitMutation: (values: {
lock_expires: Date; lock_expires: Date;
@ -41,9 +39,16 @@ type FormProps = {
const Schema = z const Schema = z
.object({ .object({
expires_date: z.date().refine((d) => isAfter(d, new Date()) || d.toDateString() === new Date().toDateString(), { expires_date: z
message: "La data deve essere nel futuro o oggi", .date()
}), .refine(
(d) =>
isAfter(d, new Date()) ||
d.toDateString() === new Date().toDateString(),
{
message: "La data deve essere nel futuro o oggi",
},
),
expires_time: z.string(), expires_time: z.string(),
mailType: z.enum(TemplateKeys), mailType: z.enum(TemplateKeys),
mailProps: z.record(z.string(), z.unknown()).optional(), mailProps: z.record(z.string(), z.unknown()).optional(),
@ -66,11 +71,11 @@ const Schema = z
type FormValues = z.infer<typeof Schema>; type FormValues = z.infer<typeof Schema>;
export const FormAddCodaEmail = ({ submitMutation }: FormProps) => { export const FormAddCodaEmail = ({ submitMutation }: FormProps) => {
z.config(z.locales.it()); z.config(z.locales.it());
const now = new Date();
const form = useZodForm(Schema, { const form = useZodForm(Schema, {
defaultValues: { defaultValues: {
expires_date: new Date(), expires_date: now,
expires_time: "00:00:00", expires_time: format(now, "HH:mm:ss"),
mailType: TemplateKeys[0], mailType: TemplateKeys[0],
mailProps: undefined, mailProps: undefined,
subject: "", subject: "",
@ -118,7 +123,6 @@ export const FormAddCodaEmail = ({ submitMutation }: FormProps) => {
return ( return (
<> <>
<Form {...form}> <Form {...form}>
<form <form
className="space-y-8 px-0.5" className="space-y-8 px-0.5"
onSubmit={form.handleSubmit(onSubmit)} onSubmit={form.handleSubmit(onSubmit)}
@ -178,7 +182,7 @@ export const FormAddCodaEmail = ({ submitMutation }: FormProps) => {
<FormControl> <FormControl>
<Input <Input
className="appearance-none [&::-webkit-calendar-picker-indicator]:hidden [&::-webkit-calendar-picker-indicator]:appearance-none" className="appearance-none [&::-webkit-calendar-picker-indicator]:hidden [&::-webkit-calendar-picker-indicator]:appearance-none"
defaultValue="10:30:00" defaultValue={field.value}
id="time-picker-optional" id="time-picker-optional"
onChange={(e) => field.onChange(e.target.value)} onChange={(e) => field.onChange(e.target.value)}
step="1" step="1"