From 9f05c194946d590e0d4a4c0feb3221926ec22455 Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Thu, 28 Aug 2025 13:19:43 +0200 Subject: [PATCH] Implement code changes to enhance functionality and improve performance --- .../src/forms/FormEditAnnuncio.tsx | 1709 +++++++++++++++++ .../admin/edit-annuncio/[id].tsx | 1707 +--------------- 2 files changed, 1711 insertions(+), 1705 deletions(-) create mode 100644 apps/infoalloggi/src/forms/FormEditAnnuncio.tsx diff --git a/apps/infoalloggi/src/forms/FormEditAnnuncio.tsx b/apps/infoalloggi/src/forms/FormEditAnnuncio.tsx new file mode 100644 index 0000000..0baafe2 --- /dev/null +++ b/apps/infoalloggi/src/forms/FormEditAnnuncio.tsx @@ -0,0 +1,1709 @@ +import z from "zod"; +import { api } from "~/utils/api"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "~/components/custom_ui/form"; +import { useZodForm } from "~/lib/zodForm"; +import { useTranslation } from "~/providers/I18nProvider"; +import { CarouselAnnuncio } from "~/components/annuncio_card"; +import { zAnnuncioId } from "~/server/utils/zod_types"; +import toast from "react-hot-toast"; +import type { Annunci } from "~/schemas/public/Annunci"; +import { DualInputLayout } from "~/components/custom_ui/inputLayouts"; +import { PhoneInput } from "~/components/phone-input"; +import type { Caratteristiche } from "~/utils/kanel-types"; +import { + Popover, + PopoverContent, + PopoverTrigger, +} from "~/components/ui/popover"; +import { format } from "date-fns"; +import { Calendar } from "~/components/ui/calendar"; +import { it } from "date-fns/locale"; +import { + MultiSelect, + type ListOption, +} from "~/components/custom_ui/multiselect"; +import { DevTool } from "@hookform/devtools"; +import { Switch } from "~/components/ui/switch"; +import { filteredCaratteristiche } from "~/hooks/schedaAnnuncioUtils"; +import { Button } from "~/components/ui/button"; +import { Card, CardContent, CardHeader, CardTitle } from "~/components/ui/card"; +import { Tabs, TabsContent, TabsList, TabsTrigger } from "~/components/ui/tabs"; +import Input from "~/components/custom_ui/input"; +import { Textarea } from "~/components/custom_ui/textarea"; +import { CalendarIcon, Eye, RotateCw } from "lucide-react"; +import Link from "next/link"; +import { StatusBadge } from "~/pages/area-riservata/admin/edit-annuncio/[id]"; +import { cn } from "~/lib/utils"; + +export const AnnuncioEditForm = ({ data }: { data: Annunci }) => { + const schema = z.object({ + //BASE + id: zAnnuncioId, + titolo_it: z.string().nullable(), + desc_it: z.string().nullable(), + titolo_en: z.string().nullable(), + desc_en: z.string().nullable(), + stato: z.string().nullable(), + web: z.boolean().nullable(), + homepage: z.boolean().nullable(), + codice: z.string(), + + // DATI LOCATORE + locatore: z.string().nullable(), + email: z.email().optional(), + numero: z.string().nullable(), + + // DATI IMMOBILE + indirizzo: z.string().nullable(), + civico: z.string().nullable(), + comune: z.string().nullable(), + cap: z.string().nullable(), + provincia: z.string().nullable(), + regione: z.string().nullable(), + lat: z.string().nullable(), + lon: z.string().nullable(), + indirizzo_secondario: z.string().nullable(), + civico_secondario: z.string().nullable(), + lat_secondario: z.string().nullable(), + lon_secondario: z.string().nullable(), + + // DATI CONTRATTO + tipo: z.string().nullable(), + categorie: z.string().array().nullable(), + prezzo: z.number(), + disponibile_da: z.date().nullable(), + consegna: z.number().nullable(), + persone: z.string().array().nullable(), + permanenza: z.number().array().nullable(), + + // ALTRI DATI IMMOBILE + anno: z.string().nullable(), + classe: z.string().nullable(), + mq: z.string().nullable(), + piano: z.string().nullable(), + piano_palazzo: z.number().nullable(), + unita_condominio: z.number().nullable(), + numero_vani: z.number().nullable(), + numero_camere: z.number().nullable(), + numero_bagni: z.number().nullable(), + numero_balconi: z.number().nullable(), + numero_terrazzi: z.number().nullable(), + numero_box: z.number().nullable(), + numero_postiauto: z.number().nullable(), + accessori: z.string().array().nullable(), + caratteristiche: z.custom().nullable(), + + // MEDIA + //url_immagini: z.string().array().nullable(), + //url_video: z.string().array().nullable(), + //og_url: z.string().nullable(), + + //creato_il: z.date().nullable(), + //modificato_il: z.date().nullable(), + }); + + type FormValues = z.infer; + const { locale } = useTranslation(); + const utils = api.useUtils(); + + const { mutate: edit } = api.annunci.editAnnuncio.useMutation({ + onSettled: async () => { + await utils.annunci.getAnnuncio.invalidate({ cod: data.codice }); + await utils.annunci.getAnnuncioByIdForEdit.invalidate({ id: data.id }); + await utils.annunci.getAnnunciList.invalidate(); + toast.success("Annuncio modificato con successo"); + }, + }); + + const defaultValues: FormValues = { + ...data, + email: data.email || undefined, + }; + + z.config(z.locales[locale]()); + + const form = useZodForm(schema, { + defaultValues: defaultValues, + }); + + function onSubmit(fields: FormValues) { + edit({ + annuncioId: data.id, + data: { ...fields, email: fields.email || null }, + }); + } + + const personeOptions: ListOption[] = [ + "single", + "coppia", + "colleghi", + "famiglia", + ].map((p) => ({ + value: p, + label: p, + })); + + const permanenzaOptions: ListOption[] = [ + { value: "1", label: "1 mese" }, + { value: "3", label: "3 mesi" }, + { value: "6", label: "6 mesi" }, + { value: "9", label: "9 mesi" }, + { value: "12", label: "12 mesi" }, + { value: "18", label: "18 mesi" }, + { value: "24", label: "24 mesi" }, + ]; + const tipoOptions: ListOption[] = [ + "Transitorio", + "Stabile", + "Vendita", + "Cessione", + "Errore", + ].map((o) => ({ value: o, label: o })); + + const statoOptions = ["Attivo", "Sospeso", "Trattativa"].map((o) => ({ + value: o, + label: o, + })); + + const consegnaOptions: ListOption[] = [ + { value: "1", label: "Gennaio" }, + { value: "2", label: "Febbraio" }, + { value: "3", label: "Marzo" }, + { value: "4", label: "Aprile" }, + { value: "5", label: "Maggio" }, + { value: "6", label: "Giugno" }, + { value: "7", label: "Luglio" }, + { value: "8", label: "Agosto" }, + { value: "9", label: "Settembre" }, + { value: "10", label: "Ottobre" }, + { value: "11", label: "Novembre" }, + { value: "12", label: "Dicembre" }, + ]; + return ( + <> +
+ + +
+
+

+ Annuncio {data.codice} +

+ + + + + +
+ + +
+
+
+
+ + + + + Italiano + Inglese + + + + + Descrizione ITA + + +
+
+ ( + +
+ + Titolo ITA + + +
+ + + + +
+ )} + /> +
+
+ ( + +
+ + Descrizione ITA + + +
+ + +