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