2025-08-28 18:27:07 +02:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import toast from "react-hot-toast";
|
2026-03-17 18:46:43 +01:00
|
|
|
import { NewRinnovoModal } from "~/components/servizio/rinnovo";
|
2026-03-12 16:26:01 +01:00
|
|
|
import { FormServizio, type FormValues } from "~/forms/FormServizio";
|
2025-12-18 15:12:05 +01:00
|
|
|
import type { UsersId } from "~/schemas/public/Users";
|
|
|
|
|
import { api } from "~/utils/api";
|
|
|
|
|
import { Button } from "../ui/button";
|
2025-08-04 17:45:44 +02:00
|
|
|
import {
|
2025-08-28 18:27:07 +02:00
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
DialogTrigger,
|
2025-12-18 15:12:05 +01:00
|
|
|
} from "../ui/dialog";
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-12-18 15:12:05 +01:00
|
|
|
export const ServiziHeader = ({ userId }: { userId: UsersId }) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
return (
|
2026-02-25 15:30:53 +01:00
|
|
|
<div className="flex w-full flex-wrap items-center justify-between gap-2">
|
|
|
|
|
<h3 className="font-semibold text-xl">Servizi cliente</h3>
|
2026-03-17 18:46:43 +01:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<NewServizioModal userId={userId} />
|
|
|
|
|
<NewRinnovoModal userId={userId} />
|
|
|
|
|
</div>
|
2025-08-28 18:27:07 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
|
|
|
|
|
2026-03-17 18:46:43 +01:00
|
|
|
const NewServizioModal = ({ userId }: { userId: UsersId }) => {
|
|
|
|
|
const [open, setOpen] = useState(false);
|
2025-08-28 18:27:07 +02:00
|
|
|
const utils = api.useUtils();
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
const { mutate: add } = api.servizio.addServizio.useMutation({
|
2025-08-29 16:18:32 +02:00
|
|
|
onError: (error) => {
|
|
|
|
|
toast.error(error.message);
|
|
|
|
|
},
|
2025-08-28 18:27:07 +02:00
|
|
|
onSuccess: async () => {
|
|
|
|
|
toast.success("Servizio creato con successo");
|
2026-03-08 01:02:57 +01:00
|
|
|
await utils.servizio.invalidate();
|
2026-03-06 18:23:35 +01:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
setOpen(false);
|
|
|
|
|
},
|
|
|
|
|
});
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
function onSubmit(fields: FormValues) {
|
2026-03-17 18:46:43 +01:00
|
|
|
add({ data: { ...fields, user_id: userId } });
|
2025-08-28 18:27:07 +02:00
|
|
|
}
|
|
|
|
|
return (
|
2025-08-29 16:18:32 +02:00
|
|
|
<Dialog onOpenChange={setOpen} open={open}>
|
2025-08-28 18:27:07 +02:00
|
|
|
<DialogTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setOpen(true);
|
|
|
|
|
}}
|
2025-08-29 16:18:32 +02:00
|
|
|
variant="outline"
|
2025-08-28 18:27:07 +02:00
|
|
|
>
|
|
|
|
|
Nuovo Servizio
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogTrigger>
|
2025-11-04 19:18:13 +01:00
|
|
|
<DialogContent className="max-h-[80%] w-full overflow-auto p-4 sm:max-w-5xl">
|
2025-08-28 18:27:07 +02:00
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>Creazione Servizio</DialogTitle>
|
|
|
|
|
<DialogDescription className="sr-only">desc</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
<div className="overflow-y-auto">
|
2026-03-12 16:26:01 +01:00
|
|
|
<FormServizio
|
2026-03-17 18:46:43 +01:00
|
|
|
initialData={undefined}
|
2025-08-28 18:27:07 +02:00
|
|
|
isLimited={false}
|
2025-08-29 16:18:32 +02:00
|
|
|
onSubmit={onSubmit}
|
2025-08-28 18:27:07 +02:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|