infoalloggi-monorepo/apps/infoalloggi/src/components/servizio/servizi_header.tsx

76 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { useState } from "react";
import toast from "react-hot-toast";
import { NewRinnovoModal } from "~/components/servizio/rinnovo";
import { FormServizio, type FormValues } from "~/forms/FormServizio";
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,
} from "../ui/dialog";
2025-08-04 17:45:44 +02:00
export const ServiziHeader = ({ userId }: { userId: UsersId }) => {
2025-08-28 18:27:07 +02:00
return (
<div className="flex w-full flex-wrap items-center justify-between gap-2">
<h3 className="font-semibold text-xl">Servizi cliente</h3>
<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
};
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");
await utils.servizio.invalidate();
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) {
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>
<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">
<FormServizio
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
};