2025-08-28 18:27:07 +02:00
|
|
|
import { Plus, RefreshCcw } from "lucide-react";
|
2025-10-17 18:12:10 +02:00
|
|
|
import type { GetServerSideProps } from "next";
|
2025-08-04 17:45:44 +02:00
|
|
|
import { useState } from "react";
|
2025-08-28 18:27:07 +02:00
|
|
|
import toast from "react-hot-toast";
|
2025-08-04 17:45:44 +02:00
|
|
|
import { AreaRiservataLayout } from "~/components/Layout";
|
|
|
|
|
import { LoadingPage } from "~/components/loading";
|
|
|
|
|
import { Status500 } from "~/components/status-page";
|
|
|
|
|
import { StringheTable } from "~/components/tables/stringhe-table";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { Button } from "~/components/ui/button";
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
DialogTrigger,
|
|
|
|
|
} from "~/components/ui/dialog";
|
2025-08-04 17:45:44 +02:00
|
|
|
import { FormStringhe } from "~/forms/FormStringhe";
|
2025-08-28 18:27:07 +02:00
|
|
|
import type { NextPageWithLayout } from "~/pages/_app";
|
|
|
|
|
import type { TestiEStringhe } from "~/schemas/public/TestiEStringhe";
|
2025-10-17 18:12:10 +02:00
|
|
|
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
|
2025-08-28 18:27:07 +02:00
|
|
|
import { api } from "~/utils/api";
|
2025-08-04 17:45:44 +02:00
|
|
|
|
|
|
|
|
const AdminTestiStringhe: NextPageWithLayout = () => {
|
2025-11-20 12:38:42 +01:00
|
|
|
const { data, isLoading, refetch } = api.strings.getStringhe.useQuery();
|
2025-08-28 18:27:07 +02:00
|
|
|
const [openEdit, setOpenEdit] = useState(false);
|
|
|
|
|
const [editData, setEditData] = useState<TestiEStringhe | null>(null);
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
const handleOpen = (status: boolean) => {
|
|
|
|
|
setOpenEdit(status);
|
|
|
|
|
};
|
|
|
|
|
const handleEdit = (data: TestiEStringhe | null) => {
|
|
|
|
|
setEditData(data);
|
|
|
|
|
};
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
if (isLoading) return <LoadingPage />;
|
|
|
|
|
if (!data) return <Status500 />;
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
return (
|
|
|
|
|
<div className="mx-1 flex-1 overflow-auto">
|
|
|
|
|
<div className="mx-auto pt-4">
|
|
|
|
|
<div className="mx-auto items-start justify-between px-2 md:flex">
|
|
|
|
|
<div className="flex items-center gap-3">
|
2025-10-10 16:18:43 +02:00
|
|
|
<h3 className="font-bold text-accent-foreground text-xl sm:text-2xl">
|
2025-08-28 18:27:07 +02:00
|
|
|
Testi e Stringhe
|
|
|
|
|
</h3>
|
|
|
|
|
<button
|
|
|
|
|
className="cursor-pointer"
|
2025-08-29 16:18:32 +02:00
|
|
|
onClick={async () => await refetch()}
|
|
|
|
|
type="button"
|
2025-08-28 18:27:07 +02:00
|
|
|
>
|
|
|
|
|
<RefreshCcw className="size-6" />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
<div className="mt-3 md:mt-0">
|
|
|
|
|
<EditModal
|
|
|
|
|
initial={editData}
|
|
|
|
|
open={openEdit}
|
|
|
|
|
setOpen={handleOpen}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="mt-3 md:mt-0">
|
|
|
|
|
<Button
|
2025-11-03 10:59:45 +01:00
|
|
|
className="font-semibold"
|
2025-08-28 18:27:07 +02:00
|
|
|
onClick={() => {
|
|
|
|
|
handleEdit(null);
|
|
|
|
|
handleOpen(true);
|
|
|
|
|
}}
|
|
|
|
|
variant="outline"
|
|
|
|
|
>
|
|
|
|
|
<Plus className="size-6" />
|
|
|
|
|
Nuovo
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-28 18:27:07 +02:00
|
|
|
<StringheTable
|
|
|
|
|
data={data}
|
|
|
|
|
setEditData={handleEdit}
|
2025-08-29 16:18:32 +02:00
|
|
|
setOpenEdit={handleOpen}
|
2025-08-28 18:27:07 +02:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
2025-10-17 18:12:10 +02:00
|
|
|
|
|
|
|
|
export const getServerSideProps = (async (context) => {
|
|
|
|
|
const access_token = context.req.cookies.access_token;
|
|
|
|
|
|
|
|
|
|
const helpers = await TrpcAuthedFetchingIstance({ access_token });
|
|
|
|
|
if (helpers) {
|
2025-11-20 12:38:42 +01:00
|
|
|
await helpers.trpc.strings.getStringhe.prefetch();
|
2025-10-17 18:12:10 +02:00
|
|
|
return {
|
|
|
|
|
props: {
|
|
|
|
|
trpcState: helpers.trpc.dehydrate(),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
props: {},
|
|
|
|
|
};
|
|
|
|
|
}) satisfies GetServerSideProps;
|
2025-08-04 17:45:44 +02:00
|
|
|
export default AdminTestiStringhe;
|
|
|
|
|
|
|
|
|
|
AdminTestiStringhe.getLayout = function getLayout(page) {
|
2025-08-28 18:27:07 +02:00
|
|
|
return <AreaRiservataLayout>{page}</AreaRiservataLayout>;
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const EditModal = ({
|
2025-08-28 18:27:07 +02:00
|
|
|
initial,
|
|
|
|
|
open,
|
|
|
|
|
setOpen,
|
2025-08-04 17:45:44 +02:00
|
|
|
}: {
|
2025-08-28 18:27:07 +02:00
|
|
|
initial: TestiEStringhe | null;
|
|
|
|
|
open: boolean;
|
|
|
|
|
setOpen: (status: boolean) => void;
|
2025-08-04 17:45:44 +02:00
|
|
|
}) => {
|
2025-08-28 18:27:07 +02:00
|
|
|
const utils = api.useUtils();
|
2025-11-20 12:38:42 +01:00
|
|
|
const { mutate: edit } = api.strings.updateStringa.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("Successo");
|
2025-11-20 12:38:42 +01:00
|
|
|
await utils.strings.getStringhe.invalidate();
|
2025-08-28 18:27:07 +02:00
|
|
|
setOpen(false);
|
|
|
|
|
},
|
2025-08-29 16:18:32 +02:00
|
|
|
});
|
2025-11-20 12:38:42 +01:00
|
|
|
const { mutate: add } = api.strings.newStringa.useMutation({
|
2025-08-28 18:27:07 +02:00
|
|
|
onError: (error) => {
|
|
|
|
|
toast.error(error.message);
|
|
|
|
|
},
|
|
|
|
|
onSuccess: async () => {
|
|
|
|
|
toast.success("Successo");
|
2025-11-20 12:38:42 +01:00
|
|
|
await utils.strings.getStringhe.invalidate();
|
2025-08-28 18:27:07 +02:00
|
|
|
setOpen(false);
|
|
|
|
|
},
|
2025-08-29 16:18:32 +02:00
|
|
|
});
|
2025-11-20 12:38:42 +01:00
|
|
|
const { mutate: del } = api.strings.deleteStringa.useMutation({
|
2025-08-28 18:27:07 +02:00
|
|
|
onError: (error) => {
|
|
|
|
|
toast.error(error.message);
|
|
|
|
|
},
|
|
|
|
|
onSuccess: async () => {
|
|
|
|
|
toast.success("Successo");
|
2025-11-20 12:38:42 +01:00
|
|
|
await utils.strings.getStringhe.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
|
|
|
return (
|
2025-08-29 16:18:32 +02:00
|
|
|
<Dialog onOpenChange={setOpen} open={open}>
|
2025-08-28 18:27:07 +02:00
|
|
|
<DialogTrigger asChild />
|
2025-11-14 17:21:21 +01:00
|
|
|
<DialogContent className="sm:max-w-8xl">
|
2025-08-28 18:27:07 +02:00
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>
|
|
|
|
|
{!initial ? "Aggiungi Banner" : "Modifica Valori"}
|
|
|
|
|
</DialogTitle>
|
|
|
|
|
<DialogDescription className="sr-only">Edit</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
{!initial ? (
|
|
|
|
|
<div className="py-4">
|
|
|
|
|
<FormStringhe
|
2025-08-29 16:18:32 +02:00
|
|
|
initialValues={null}
|
2025-08-28 18:27:07 +02:00
|
|
|
submitMutation={(fields) => {
|
|
|
|
|
add({
|
|
|
|
|
stringaId: fields.stinga_id,
|
|
|
|
|
stringaValue: fields.stringa_value,
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="py-4">
|
|
|
|
|
<FormStringhe
|
2025-08-29 16:18:32 +02:00
|
|
|
del={(v) => del({ stringaId: v })}
|
|
|
|
|
initialValues={{
|
|
|
|
|
...initial,
|
|
|
|
|
}}
|
2025-08-28 18:27:07 +02:00
|
|
|
submitMutation={(fields) => {
|
|
|
|
|
edit({
|
|
|
|
|
stringaId: fields.stinga_id,
|
|
|
|
|
stringaValue: fields.stringa_value,
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
2025-08-04 17:45:44 +02:00
|
|
|
};
|