diff --git a/apps/infoalloggi/src/components/servizio/servizio_actions.tsx b/apps/infoalloggi/src/components/servizio/servizio_actions.tsx index 880d6a0..63dc077 100644 --- a/apps/infoalloggi/src/components/servizio/servizio_actions.tsx +++ b/apps/infoalloggi/src/components/servizio/servizio_actions.tsx @@ -25,7 +25,7 @@ import { AlertDialogTitle, AlertDialogTrigger, } from "~/components/ui/alert-dialog"; -import { Calculator, ClockAlert, Plus } from "lucide-react"; +import { Calculator, ClockAlert, Plus, Wrench } from "lucide-react"; import { FormNewServizio, type FormValues } from "~/forms/FormNewServizio"; import { Credenza, @@ -41,6 +41,10 @@ import { useState } from "react"; import { useServizio } from "~/providers/ServizioProvider"; import type { ServizioServizioId } from "~/schemas/public/Servizio"; import type { UsersId } from "~/schemas/public/Users"; +import { + FormEditServizio, + type FormValues as EditFormValues, +} from "~/forms/FormEditServizioAdmin"; export const ServizioActions = () => { const { servizio, isAdmin, userId } = useServizio(); @@ -62,7 +66,10 @@ export const ServizioActions = () => { )} {isAdmin && ( - + <> + + + )} ); @@ -306,3 +313,63 @@ const EditPreferenze = () => { ); }; + +const EditServizioAdmin = () => { + const { servizioId, userId } = useServizio(); + const [open, setOpen] = useState(false); + const { data, isLoading } = api.servizio.getServizio.useQuery({ servizioId }); + const utils = api.useUtils(); + + const { mutate: update } = api.servizio.updateServizio.useMutation({ + onSuccess: async () => { + toast.success("Servizio modificato con successo"); + await utils.servizio.getAllServizioAnnunci.invalidate({ userId }); + await utils.servizio.getServizio.invalidate({ servizioId }); + await utils.servizio.getCompatibileAnnunci.invalidate({ + servizioId, + }); + setOpen(false); + }, + onError: (error) => { + toast.error(error.message); + }, + }); + + function onSubmit(fields: EditFormValues) { + update({ + servizioId, + data: { + ...fields, + }, + }); + } + + return ( + + + + + + + Modifica Servizio Admin + + Modifica + + + + {isLoading ? ( + + ) : ( + + )} + + + + ); +}; diff --git a/apps/infoalloggi/src/forms/FormEditServizioAdmin.tsx b/apps/infoalloggi/src/forms/FormEditServizioAdmin.tsx new file mode 100644 index 0000000..b821f0f --- /dev/null +++ b/apps/infoalloggi/src/forms/FormEditServizioAdmin.tsx @@ -0,0 +1,274 @@ +import { z } from "zod/v4"; +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "~/components/custom_ui/form"; +import { useZodForm } from "~/lib/zodForm"; +import { Button } from "~/components/ui/button"; +import { useTranslation } from "~/providers/I18nProvider"; +import { cn } from "~/lib/utils"; +import { Switch } from "~/components/ui/switch"; +import type { Servizio } from "~/schemas/public/Servizio"; +import { + Popover, + PopoverContent, + PopoverTrigger, +} from "~/components/ui/popover"; +import { format } from "date-fns"; +import { CalendarIcon, X } from "lucide-react"; +import { Calendar } from "~/components/ui/calendar"; +import { enUS, it } from "date-fns/locale"; +import { Label } from "~/components/ui/label"; + +const Schema = z.object({ + isSent: z.boolean(), + decorrenza: z.date().nullable(), + isInterrotto: z.boolean(), + isOkAcconto: z.boolean(), + isOkSaldo: z.boolean(), + isOkConsulenza: z.boolean(), +}); + +export type FormValues = z.infer; + +export const FormEditServizio = ({ + initialData, + onSubmit, +}: { + initialData: Servizio | undefined; + onSubmit: (fields: FormValues) => void; +}) => { + // This can come from your database or API. + let defaultValues: FormValues; + + if (initialData) { + defaultValues = { + ...initialData, + }; + } else { + defaultValues = { + isSent: false, + decorrenza: null, + isInterrotto: false, + isOkAcconto: false, + isOkSaldo: false, + isOkConsulenza: false, + }; + } + const { locale, t } = useTranslation(); + + z.config(z.locales[locale]()); + + const form = useZodForm(Schema, { + defaultValues: defaultValues, + }); + + const DatePickerLocale = locale === "it" ? it : enUS; + + return ( +
+ +
+ ( + +
+ Inviato + + + + +
+ + + La mail di benvenuto al cliente è stata inviata + +
+ )} + /> + ( + +
+ Interrotto + + + + +
+ + Il servizio è interrotto +
+ )} + /> +
+
+ ( + +
+ + Decorrenza Servizio + + +
+ + + + + + + + + date > new Date() || date < new Date("1900-01-01") + } + // eslint-disable-next-line jsx-a11y/no-autofocus + autoFocus + /> + + +
+ )} + /> + +
+ + +
+
+
+ ( + +
+ Acconto Pagato + + + + +
+ + + I'acconto è stato pagato o attivato manualmente + +
+ )} + /> + ( + +
+ Saldo Pagato + + + + +
+ + + Il saldo è stato pagato o attivato manualmente + +
+ )} + /> + ( + +
+ + Consulenza Pagata + + + + + +
+ + + La consulenza è stata pagata o attivata manualmente + +
+ )} + /> +
+ +
+ + ); +};