177 lines
4.5 KiB
TypeScript
177 lines
4.5 KiB
TypeScript
import { Plus, RefreshCcw } from "lucide-react";
|
|
import { useState } from "react";
|
|
import toast from "react-hot-toast";
|
|
import { AreaRiservataLayout } from "~/components/Layout";
|
|
import { LoadingPage } from "~/components/loading";
|
|
import { Status500 } from "~/components/status-page";
|
|
import { EtichetteTable } from "~/components/tables/etichette";
|
|
import { Button } from "~/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from "~/components/ui/dialog";
|
|
import { FormEtichette } from "~/forms/FormEtichette";
|
|
import type { NextPageWithLayout } from "~/pages/_app";
|
|
import type { Etichette } from "~/schemas/public/Etichette";
|
|
import { api } from "~/utils/api";
|
|
|
|
const AdminEtichette: NextPageWithLayout = () => {
|
|
const { data, isLoading, refetch } = api.settings.getAllEtichette.useQuery();
|
|
const [openEdit, setOpenEdit] = useState(false);
|
|
const [editData, setEditData] = useState<Etichette | null>(null);
|
|
|
|
const handleOpen = (status: boolean) => {
|
|
setOpenEdit(status);
|
|
};
|
|
|
|
const handleEdit = (data: Etichette | null) => {
|
|
setEditData(data);
|
|
};
|
|
|
|
if (isLoading) return <LoadingPage />;
|
|
if (!data) return <Status500 />;
|
|
|
|
return (
|
|
<div className="mx-1 flex-1 overflow-auto">
|
|
<div className="mx-auto pt-4">
|
|
<div className="mx-3 items-start justify-between md:flex">
|
|
<div className="flex max-w-lg items-center gap-3">
|
|
<h3 className="text-accent-foreground text-xl font-bold sm:text-2xl">
|
|
Etichette
|
|
</h3>
|
|
<button
|
|
className="cursor-pointer"
|
|
onClick={async () => await refetch()}
|
|
type="button"
|
|
>
|
|
<RefreshCcw className="size-6" />
|
|
</button>
|
|
</div>
|
|
<div className="mt-3 md:mt-0">
|
|
<EditModal
|
|
initial={editData}
|
|
open={openEdit}
|
|
setOpen={handleOpen}
|
|
/>
|
|
</div>
|
|
<div className="mt-3 md:mt-0">
|
|
<Button
|
|
className="flex items-center gap-2 font-semibold"
|
|
onClick={() => {
|
|
handleEdit(null);
|
|
handleOpen(true);
|
|
}}
|
|
variant="outline"
|
|
>
|
|
<Plus className="size-6" />
|
|
Nuovo
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<EtichetteTable
|
|
data={data}
|
|
setEditData={handleEdit}
|
|
setOpenEdit={handleOpen}
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
export default AdminEtichette;
|
|
|
|
AdminEtichette.getLayout = function getLayout(page) {
|
|
return <AreaRiservataLayout>{page}</AreaRiservataLayout>;
|
|
};
|
|
|
|
const EditModal = ({
|
|
initial,
|
|
open,
|
|
setOpen,
|
|
}: {
|
|
initial: Etichette | null;
|
|
open: boolean;
|
|
setOpen: (status: boolean) => void;
|
|
}) => {
|
|
const utils = api.useUtils();
|
|
const { mutate: edit } = api.settings.updateEtichetta.useMutation({
|
|
onError: (error) => {
|
|
toast.error(error.message);
|
|
},
|
|
onSuccess: async () => {
|
|
toast.success("Successo");
|
|
await utils.settings.getAllEtichette.invalidate();
|
|
await utils.chat.getChatEtichette.invalidate();
|
|
await utils.chat.getActiveChats.invalidate();
|
|
setOpen(false);
|
|
},
|
|
});
|
|
const { mutate: add } = api.settings.addEtichetta.useMutation({
|
|
onError: (error) => {
|
|
toast.error(error.message);
|
|
},
|
|
onSuccess: async () => {
|
|
toast.success("Successo");
|
|
await utils.settings.getAllEtichette.invalidate();
|
|
await utils.chat.getChatEtichette.invalidate();
|
|
await utils.chat.getActiveChats.invalidate();
|
|
setOpen(false);
|
|
},
|
|
});
|
|
const { mutate: del } = api.settings.removeEtichetta.useMutation({
|
|
onError: (error) => {
|
|
toast.error(error.message);
|
|
},
|
|
onSuccess: async () => {
|
|
toast.success("Successo");
|
|
await utils.settings.getAllEtichette.invalidate();
|
|
await utils.chat.getChatEtichette.invalidate();
|
|
await utils.chat.getActiveChats.invalidate();
|
|
setOpen(false);
|
|
},
|
|
});
|
|
|
|
return (
|
|
<Dialog onOpenChange={setOpen} open={open}>
|
|
<DialogTrigger asChild />
|
|
<DialogContent className="sm:max-w-[425px]">
|
|
<DialogHeader>
|
|
<DialogTitle>
|
|
{!initial ? "Aggiungi Etichetta" : "Modifica Valori"}
|
|
</DialogTitle>
|
|
<DialogDescription className="sr-only">Edit</DialogDescription>
|
|
</DialogHeader>
|
|
{!initial ? (
|
|
<div className="py-4">
|
|
<FormEtichette
|
|
initialValues={null}
|
|
submitMutation={(fields) => {
|
|
add({
|
|
...fields,
|
|
});
|
|
}}
|
|
/>
|
|
</div>
|
|
) : (
|
|
<div className="py-4">
|
|
<FormEtichette
|
|
del={(v) => del({ id_etichetta: v })}
|
|
initialValues={{
|
|
...initial,
|
|
}}
|
|
submitMutation={(fields) => {
|
|
edit({
|
|
...fields,
|
|
});
|
|
}}
|
|
/>
|
|
</div>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
};
|