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

520 lines
14 KiB
TypeScript
Raw Normal View History

import { z } from "zod/v4";
2025-08-28 18:27:07 +02:00
import { Counter } from "~/components/counter";
2025-08-04 17:45:44 +02:00
import {
2025-08-28 18:27:07 +02:00
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
2025-08-04 17:45:44 +02:00
} from "~/components/custom_ui/form";
2025-08-28 18:27:07 +02:00
import { MultiSelect, UnpackOptions } from "~/components/custom_ui/multiselect";
import { Badge } from "~/components/ui/badge";
2025-08-04 17:45:44 +02:00
import { Button } from "~/components/ui/button";
2025-08-28 18:27:07 +02:00
import { Label } from "~/components/ui/label";
2025-08-04 17:45:44 +02:00
import { Slider } from "~/components/ui/slider";
import { Switch } from "~/components/ui/switch";
2025-08-28 18:27:07 +02:00
import { cn } from "~/lib/utils";
import { useZodForm } from "~/lib/zodForm";
import { useTranslation } from "~/providers/I18nProvider";
2025-08-04 17:45:44 +02:00
import type { Servizio } from "~/schemas/public/Servizio";
2025-08-28 18:27:07 +02:00
import TipologiaPosizioneEnum from "~/schemas/public/TipologiaPosizioneEnum";
2025-08-04 17:45:44 +02:00
const Schema = z
2025-08-28 18:27:07 +02:00
.object({
2025-08-29 16:18:32 +02:00
animali: z.boolean(),
arredato: z.boolean(),
ascensore: z.boolean(),
2025-08-28 18:27:07 +02:00
budget: z.number(),
entromese: z.number().nullable(),
2025-08-29 16:18:32 +02:00
fumatori: z.boolean(),
giardino: z.boolean(),
motivazione_transitorio: z.string().nullable(),
2025-08-28 18:27:07 +02:00
n_adulti: z.number().min(1),
n_minori: z.number(),
2025-08-29 16:18:32 +02:00
parcheggio: z.boolean(),
2025-08-28 18:27:07 +02:00
permanenza: z.number().nullable(),
2025-08-29 16:18:32 +02:00
pianoterra: z.boolean(),
2025-08-28 18:27:07 +02:00
reddito: z.string().nullable(),
terrazzo: z.boolean(),
2025-08-29 16:18:32 +02:00
tipologia: z.custom<TipologiaPosizioneEnum>(),
2025-08-28 18:27:07 +02:00
})
.superRefine(({ n_adulti }, refinementContext) => {
if (n_adulti <= 0) {
refinementContext.issues.push({
code: "custom",
2025-08-29 16:18:32 +02:00
input: "",
2025-08-28 18:27:07 +02:00
message: "Inserisci il numero di persone",
path: ["npersone"],
});
}
});
2025-08-04 17:45:44 +02:00
export type FormValues = z.infer<typeof Schema>;
2025-08-04 17:45:44 +02:00
export const FormNewServizio = ({
2025-08-28 18:27:07 +02:00
initialData,
onSubmit,
isLimited,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
initialData: Servizio | undefined;
onSubmit: (fields: FormValues) => void;
isLimited: boolean;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
// This can come from your database or API.
let defaultValues: FormValues;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (initialData) {
defaultValues = {
...initialData,
};
} else {
defaultValues = {
2025-08-29 16:18:32 +02:00
animali: false,
arredato: false,
ascensore: false,
2025-08-28 18:27:07 +02:00
budget: 0,
entromese: null,
2025-08-29 16:18:32 +02:00
fumatori: false,
giardino: false,
motivazione_transitorio: "",
2025-08-28 18:27:07 +02:00
n_adulti: 1,
n_minori: 0,
2025-08-29 16:18:32 +02:00
parcheggio: false,
2025-08-28 18:27:07 +02:00
permanenza: null,
2025-08-29 16:18:32 +02:00
pianoterra: false,
2025-08-28 18:27:07 +02:00
reddito: "",
terrazzo: false,
2025-08-29 16:18:32 +02:00
tipologia: TipologiaPosizioneEnum.Transitorio,
2025-08-28 18:27:07 +02:00
};
}
const { locale, t } = useTranslation();
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
z.config(z.locales[locale]());
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
type altrePrefMappingType = [
["animali", string],
["fumatori", string],
["giardino", string],
["parcheggio", string],
["terrazzo", string],
["ascensore", string],
["pianoterra", string],
];
const altrePrefMapping = t.altrePrefMapping as altrePrefMappingType;
const tipologia = form.watch("tipologia");
return (
<Form {...form}>
2025-08-29 16:18:32 +02:00
<form className="space-y-8 px-0.5" onSubmit={form.handleSubmit(onSubmit)}>
2025-08-28 18:27:07 +02:00
<FormField
control={form.control}
name="tipologia"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="select-tipologia">Tipologia</FormLabel>
<FormMessage />
</div>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
{isLimited ? (
<div className="space-y-0">
2025-10-10 16:18:43 +02:00
<p className="font-semibold text-lg">{field.value}</p>
2025-08-28 18:27:07 +02:00
<p className="text-muted-foreground text-sm">
La tipologia non può essere modificata. Contatta il supporto
se necessario.
</p>
</div>
) : (
<MultiSelect
defaultValue={
field.value === TipologiaPosizioneEnum.Transitorio
? {
label: "Transitorio",
2025-08-29 16:18:32 +02:00
value: TipologiaPosizioneEnum.Transitorio,
2025-08-28 18:27:07 +02:00
}
: {
label: "Stabile",
2025-08-29 16:18:32 +02:00
value: TipologiaPosizioneEnum.Stabile,
2025-08-28 18:27:07 +02:00
}
}
2025-08-29 16:18:32 +02:00
elementId="select-tipologia"
elementName="tipologia"
isMulti={false}
onValueChange={(value) => {
field.onChange(value.value);
form.setValue("motivazione_transitorio", null);
form.setValue("reddito", null);
}}
2025-08-28 18:27:07 +02:00
options={[
{
label: "Transitorio",
2025-08-29 16:18:32 +02:00
value: TipologiaPosizioneEnum.Transitorio,
2025-08-28 18:27:07 +02:00
},
{
label: "Stabile",
2025-08-29 16:18:32 +02:00
value: TipologiaPosizioneEnum.Stabile,
2025-08-28 18:27:07 +02:00
},
]}
2025-08-29 16:18:32 +02:00
placeholder={t.seleziona_placeholder}
2025-08-28 18:27:07 +02:00
/>
)}
</FormItem>
)}
/>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormField
control={form.control}
name="budget"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2 pb-1">
<FormLabel htmlFor="budgetslider">
{`${t.preferenze.budget_label1} - ${field.value.toLocaleString("it")},00 ${
t.preferenze.budget_label2
}`}
</FormLabel>
<FormMessage />
</div>
<FormControl>
<Slider
defaultValue={[field.value]}
2025-08-29 16:18:32 +02:00
id="budgetslider"
max={2000}
min={0}
2025-08-28 18:27:07 +02:00
onValueChange={(vals) => {
field.onChange(Number(vals[0]));
}}
rangeClassName="bg-neutral-700"
2025-08-29 16:18:32 +02:00
step={50}
2025-08-28 18:27:07 +02:00
thumbClassName="border-neutral-700"
value={[form.getValues("budget")]}
/>
</FormControl>
<FormDescription>{t.preferenze.budget_desc}</FormDescription>
</FormItem>
)}
/>
<div className="flex w-full items-start justify-between gap-x-4">
<FormField
control={form.control}
name="entromese"
render={({ field }) => (
<FormItem className="w-full">
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="select-mesi">
{t.preferenze.dalmese_label}...
</FormLabel>
{field.value !== null && !initialData && (
<button
className="text-sm"
onClick={() => form.setValue("entromese", null)}
2025-08-29 16:18:32 +02:00
type="button"
2025-08-28 18:27:07 +02:00
>
Reset
</button>
)}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormMessage />
</div>
<FormControl>
<MultiSelect
defaultValue={
field.value !== null
? {
// biome-ignore lint/style/noNonNullAssertion: <known size>
label: t.preferenze.mesi[field.value]!,
2025-08-29 16:18:32 +02:00
value: String(field.value),
2025-08-28 18:27:07 +02:00
}
: undefined
}
2025-08-29 16:18:32 +02:00
elementId="select-mesi"
elementName="entro_mese"
isMulti={false}
2025-08-28 18:27:07 +02:00
onValueChange={(value) => {
field.onChange(
value.value !== "" ? parseInt(value.value) : null,
);
}}
2025-08-29 16:18:32 +02:00
options={UnpackOptions({
options: t.preferenze.mesi,
})}
2025-08-28 18:27:07 +02:00
placeholder="Seleziona..."
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
disabled={tipologia !== TipologiaPosizioneEnum.Transitorio}
2025-08-29 16:18:32 +02:00
name="permanenza"
2025-08-28 18:27:07 +02:00
render={({ field }) => (
<FormItem className="w-full">
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="permanenza">
{t.preferenze.permanenza_label}
</FormLabel>
{field.value !== null && !initialData && (
<button
className="text-sm"
onClick={() => form.setValue("permanenza", null)}
2025-08-29 16:18:32 +02:00
type="button"
2025-08-28 18:27:07 +02:00
>
Reset
</button>
)}
<FormMessage />
</div>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormControl>
<MultiSelect
defaultValue={
field.value
? {
label: t.preferenze.permanenza[field.value] || "",
2025-08-29 16:18:32 +02:00
value: field.value.toString(),
2025-08-28 18:27:07 +02:00
}
: undefined
}
2025-08-29 16:18:32 +02:00
disabled={tipologia !== TipologiaPosizioneEnum.Transitorio}
elementId="permanenza"
elementName="permanenza"
isMulti={false}
2025-08-28 18:27:07 +02:00
onValueChange={(value) => {
field.onChange(
value.value !== "" ? parseInt(value.value) : null,
);
}}
2025-08-29 16:18:32 +02:00
options={UnpackOptions({
options: t.preferenze.permanenza,
})}
2025-08-28 18:27:07 +02:00
placeholder="Seleziona..."
/>
</FormControl>
<FormDescription>
{t.preferenze.permanenza_desc}
</FormDescription>
</FormItem>
)}
/>
</div>
<div className="flex w-full items-center justify-between gap-x-4">
<FormField
control={form.control}
name="n_adulti"
render={({ field }) => (
<FormItem className={cn("w-full")}>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel
2025-10-10 16:18:43 +02:00
className={cn("font-semibold text-base")}
2025-08-28 18:27:07 +02:00
htmlFor="n_adulti"
>
{t.preferenze.nadulti_label}
</FormLabel>
<FormMessage />
</div>
<FormControl>
<Counter
name="n_adulti"
onlyPositive
2025-08-29 16:18:32 +02:00
setValue={(value) => field.onChange(Number(value))}
value={Number(field.value)}
2025-08-28 18:27:07 +02:00
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="n_minori"
render={({ field }) => (
<FormItem className={cn("w-full")}>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel
2025-10-10 16:18:43 +02:00
className={cn("font-semibold text-base")}
2025-08-28 18:27:07 +02:00
htmlFor="n_minori"
>
{t.preferenze.nminori_label}
</FormLabel>
<FormMessage />
</div>
<FormControl>
<Counter
name="n_minori"
onlyPositive
2025-08-29 16:18:32 +02:00
setValue={(value) => field.onChange(Number(value))}
value={Number(field.value)}
2025-08-28 18:27:07 +02:00
/>
</FormControl>
</FormItem>
)}
/>
</div>
<FormField
control={form.control}
name="arredato"
render={({ field }) => (
<FormItem className="w-full">
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="arredato">
{t.preferenze.arredato_label}
</FormLabel>
<FormControl>
<Switch
checked={field.value}
className="data-[state=checked]:bg-neutral-700"
2025-08-29 16:18:32 +02:00
id="arredato"
onCheckedChange={field.onChange}
2025-08-28 18:27:07 +02:00
/>
</FormControl>
<FormMessage />
</div>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormDescription>{t.preferenze.arredato_desc}</FormDescription>
</FormItem>
)}
/>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
{tipologia === TipologiaPosizioneEnum.Transitorio ? (
<FormField
control={form.control}
2025-08-29 16:18:32 +02:00
key={tipologia}
2025-08-28 18:27:07 +02:00
name="motivazione_transitorio"
render={({ field }) => {
const existingValue = t.preferenze.motivazioni_mappings.find(
(m) => m.id === field.value,
);
return (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="motivazione_transitorio">
{t.preferenze.motivazione_label}
</FormLabel>
<FormMessage />
</div>
{isLimited ? (
<div className="space-y-0">
2025-10-10 16:18:43 +02:00
<p className="font-semibold text-lg">
2025-08-28 18:27:07 +02:00
{existingValue?.title}
</p>
<p className="text-muted-foreground text-sm">
La motivazione non può essere modificata. Contatta il
supporto se necessario.
</p>
</div>
) : (
<>
<MultiSelect
defaultValue={
existingValue
? {
label: existingValue.title,
2025-08-29 16:18:32 +02:00
value: existingValue.id,
2025-08-28 18:27:07 +02:00
}
: undefined
}
2025-08-29 16:18:32 +02:00
elementId="select-motivazione_transitorio"
elementName="motivazione_transitorio"
isMulti={false}
onValueChange={(value) => {
field.onChange(value.value);
}}
2025-08-28 18:27:07 +02:00
options={t.preferenze.motivazioni_mappings.map(
(option) => ({
label: option.title,
2025-08-29 16:18:32 +02:00
value: option.id,
2025-08-28 18:27:07 +02:00
}),
)}
2025-08-29 16:18:32 +02:00
placeholder={t.seleziona_placeholder}
2025-08-28 18:27:07 +02:00
/>
<FormDescription>
{t.preferenze.motivazione_desc}
</FormDescription>
</>
)}
</FormItem>
);
}}
/>
) : (
<FormField
control={form.control}
2025-08-29 16:18:32 +02:00
key={tipologia}
2025-08-28 18:27:07 +02:00
name="reddito"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="select-reddito">
{t.preferenze.reddito_label}
</FormLabel>
<FormMessage />
</div>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<MultiSelect
2025-08-29 16:18:32 +02:00
defaultValue={undefined}
2025-08-28 18:27:07 +02:00
elementId="select-reddito"
elementName="reddito"
isMulti={false}
onValueChange={(value) => {
field.onChange(value.value);
}}
2025-08-29 16:18:32 +02:00
options={UnpackOptions({
options: t.preferenze.reddito_options,
})}
placeholder={t.seleziona_placeholder}
2025-08-28 18:27:07 +02:00
/>
</FormItem>
)}
/>
)}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<div className="flex flex-col space-y-2">
<Label htmlFor="altrePrefDialogButton">
{t.preferenze.altrepref_label}
</Label>
<FormDescription>{t.preferenze.altrepref_desc}</FormDescription>
<div className="mt-2 flex flex-wrap gap-2">
{altrePrefMapping.map((pair) => {
2025-08-28 18:27:07 +02:00
if (pair) {
return (
<FormField
control={form.control}
key={pair[0]}
2025-08-28 18:27:07 +02:00
name={pair[0]}
render={({ field }) => (
<FormItem>
<FormControl>
<Badge
className={cn(
"cursor-pointer text-sm",
field.value
? "bg-neutral-800 hover:bg-neutral-800 sm:hover:bg-neutral-900/80"
: "bg-neutral-200 text-neutral-700 hover:bg-neutral-200 sm:hover:bg-neutral-900/80 sm:hover:text-neutral-50",
)}
2025-08-29 16:18:32 +02:00
onClick={() => {
field.onChange(!field.value);
}}
2025-08-28 18:27:07 +02:00
>
{pair[1]}
</Badge>
</FormControl>
</FormItem>
)}
/>
);
}
})}
</div>
</div>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<Button type="submit">Salva</Button>
</form>
</Form>
);
2025-08-04 17:45:44 +02:00
};