From c783dfed0356318a362c08f3644d54d9db6fd3c4 Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Tue, 16 Dec 2025 16:43:42 +0100 Subject: [PATCH] refactor: enhance AdminLavoraConferma component with GeneralSection and improve form handling in AnnuncioEditForm --- .../components/servizio/admin_conferma.tsx | 143 +++++++++++++++++- .../src/forms/FormEditAnnuncio.tsx | 76 ++++++---- apps/infoalloggi/src/lib/annuncio_details.ts | 4 + .../server/controllers/servizio.controller.ts | 6 + 4 files changed, 194 insertions(+), 35 deletions(-) diff --git a/apps/infoalloggi/src/components/servizio/admin_conferma.tsx b/apps/infoalloggi/src/components/servizio/admin_conferma.tsx index c3dbc2f..1acc2bb 100644 --- a/apps/infoalloggi/src/components/servizio/admin_conferma.tsx +++ b/apps/infoalloggi/src/components/servizio/admin_conferma.tsx @@ -12,6 +12,8 @@ import { useState } from "react"; import toast from "react-hot-toast"; import { useDebounce } from "react-use"; import z from "zod"; +import { it } from "~/i18n/it"; +import { handleConsegna } from "~/lib/annuncio_details"; import { formatCurrency } from "~/lib/utils"; import { useTranslation } from "~/providers/I18nProvider"; import { useServizio, useServizioAnnuncio } from "~/providers/ServizioProvider"; @@ -19,6 +21,7 @@ import TipologiaPosizioneEnum from "~/schemas/public/TipologiaPosizioneEnum"; import type { UsersStorageUserStorageId } from "~/schemas/public/UsersStorage"; import { api } from "~/utils/api"; import { Confirm } from "../confirm"; +import { Counter } from "../counter"; import { Credenza, CredenzaBody, @@ -548,6 +551,12 @@ const TransitorioSection = () => { }); }; const preferenzeDescrizione = data.desc_it?.match(/Permanenza:.*$/m); + + const permanenzaAnnuncio = data.permanenza + ? `${data.permanenza.join(" - ")} mesi` + : (preferenzeDescrizione?.[0] ?? + "errore: testo non trovato in descrizione"); + return (
@@ -575,10 +584,8 @@ const TransitorioSection = () => { />

- Permanenza in descrizione: - {preferenzeDescrizione - ? preferenzeDescrizione[0] - : "errore: testo non trovato in descrizione"} + Permanenza in annuncio: + {permanenzaAnnuncio}

); @@ -639,6 +646,131 @@ const StabileSection = () => { ); }; +const GeneralSection = () => { + const { servizio } = useServizio(); + const { data } = useServizioAnnuncio(); + const { t } = useTranslation(); + + const { servizioId, userId } = useServizio(); + const utils = api.useUtils(); + const { mutate: update } = api.servizio.updateServizio.useMutation({ + onError: (error) => { + toast.error(error.message); + }, + onSuccess: async () => { + toast.success("Servizio modificato con successo"); + await utils.servizio.getUserServizi.invalidate(); + await utils.servizio.getAllServizioAnnunci.invalidate({ userId }); + }, + }); + + const handleDisponibilitàChange = (value: ListOption) => { + update({ + servizioId, + data: { + entromese: value.value !== "" ? parseInt(value.value) : null, + }, + }); + }; + + const handleAdultiChange = (value: number) => { + update({ + servizioId, + data: { + n_adulti: value, + }, + }); + }; + const handleMinoriChange = (value: number) => { + update({ + servizioId, + data: { + n_minori: value, + }, + }); + }; + + const personeDescrizione = data.desc_it?.match(/Preferenza:.*$/m); + + const personeAnnuncio = data.persone + ? data.persone.join(", ").toUpperCase() + : (personeDescrizione?.[0] ?? "errore: testo non trovato in descrizione"); + + const dispAnnuncio = data.disponibile_da + ? format(new Date(data.disponibile_da), "dd/MM/yyyy") + : handleConsegna({ + consegna: data.consegna, + aggiornamento: it.card.in_aggiornamento, + + consegna_da: it.card.consegna_da, + mesi: it.preferenze.mesi, + subito: it.card.consegna_subito, + }); + + return ( + <> +
+ Persone: +
+
+ + handleAdultiChange(Number(v))} + value={servizio.n_adulti || 0} + /> +
+ +
+ + handleMinoriChange(Number(v))} + value={servizio.n_minori || 0} + /> +
+
+

+ Persone in annuncio: + {personeAnnuncio} +

+
+
+ Entro mese: +
+ + + label: t.preferenze.mesi[servizio.entromese]!, + value: String(servizio.entromese), + } + : undefined + } + elementId="select-mesi" + elementName="entro_mese" + isMulti={false} + onValueChange={handleDisponibilitàChange} + options={UnpackOptions({ + options: t.preferenze.mesi, + })} + placeholder="Seleziona..." + /> +
+ +

+ Disponibilità in annuncio: + {dispAnnuncio} +

+
+ + ); +}; + const SendConferma = () => { const { servizio } = useServizio(); const { data } = useServizioAnnuncio(); @@ -667,7 +799,7 @@ const SendConferma = () => { -
+

Tipologia servizio: {servizio.tipologia} @@ -677,6 +809,7 @@ const SendConferma = () => { ) : ( )} +

{ name="disponibile_da" render={({ field }) => ( -
+
Disponibile Da + {field.value !== null && ( + + )}
@@ -1096,36 +1110,38 @@ export const AnnuncioEditForm = ({ data }: { data: AnnunciWithMedia }) => { ( - -
- - Consegna - - -
+ render={({ field: { value, onChange } }) => { + const currVal = + value !== null + ? consegnaOptions.filter( + (o) => value.toString() === o.value, + )[0] + : undefined; + return ( + +
+ + Consegna + + +
- - - field.value?.toString().includes(o.value), - ) - : undefined - } - elementId="select-consegna" - elementName="consegna" - isMulti={false} - onValueChange={async (value) => { - field.onChange(parseInt(value.value)); - }} - options={consegnaOptions} - placeholder="Seleziona..." - /> - -
- )} + + { + onChange(parseInt(value.value)); + }} + options={consegnaOptions} + placeholder="Seleziona..." + /> + +
+ ); + }} /> } children2={ diff --git a/apps/infoalloggi/src/lib/annuncio_details.ts b/apps/infoalloggi/src/lib/annuncio_details.ts index f94bd13..1415855 100644 --- a/apps/infoalloggi/src/lib/annuncio_details.ts +++ b/apps/infoalloggi/src/lib/annuncio_details.ts @@ -14,6 +14,10 @@ export const handleConsegna = ({ aggiornamento: string; }) => { const currentMonth = new Date().getMonth() + 1; + console.log("handleConsegna", { + consegna: consegna, + currentMonth: currentMonth, + }); if (consegna !== null) { if (consegna !== currentMonth) { return `${mesi[consegna]}`; diff --git a/apps/infoalloggi/src/server/controllers/servizio.controller.ts b/apps/infoalloggi/src/server/controllers/servizio.controller.ts index 4f2d110..98e18f1 100644 --- a/apps/infoalloggi/src/server/controllers/servizio.controller.ts +++ b/apps/infoalloggi/src/server/controllers/servizio.controller.ts @@ -635,6 +635,9 @@ export const getAllServizioAnnunci = async (userId: UsersId) => { "servizio.skipPayment", "servizio.skipControlloDoc", "servizio.skipDocMotivazione", + "servizio.n_adulti", + "servizio.n_minori", + "servizio.entromese", jsonObjectFrom( eb .selectFrom("users_storage") @@ -699,6 +702,9 @@ export const getAllServizioAnnunci = async (userId: UsersId) => { "annunci.media_updated_at", "annunci.homepage", "annunci.web", + "annunci.disponibile_da", + "annunci.persone", + "annunci.permanenza", withImages(), withVideos(), ])