132 lines
3.8 KiB
TypeScript
132 lines
3.8 KiB
TypeScript
import { CodeSquare, RefreshCcw, TriangleAlert } from "lucide-react";
|
|
import Head from "next/head";
|
|
import toast from "react-hot-toast";
|
|
import { Confirm } from "~/components/confirm";
|
|
import { AreaRiservataLayout } from "~/components/Layout";
|
|
import { LoadingPage } from "~/components/loading";
|
|
import { Status500 } from "~/components/status-page";
|
|
import { AnnunciTable } from "~/components/tables/annunci-tables";
|
|
import { Button } from "~/components/ui/button";
|
|
import {
|
|
Popover,
|
|
PopoverContent,
|
|
PopoverTrigger,
|
|
} from "~/components/ui/popover";
|
|
import type { NextPageWithLayout } from "~/pages/_app";
|
|
import { generateSSGHelper } from "~/server/utils/ssgHelper";
|
|
import { api } from "~/utils/api";
|
|
|
|
const Admin_Annunci: NextPageWithLayout = () => {
|
|
const { data, isLoading, refetch } = api.annunci.getAnnunciList.useQuery();
|
|
const utils = api.useUtils();
|
|
const { mutateAsync: update } = api.revalidation.updateAllAnnunci.useMutation(
|
|
{
|
|
onMutate: () => {
|
|
toast.loading(
|
|
"Richiesta di aggiornamento inviata, attendere prego...",
|
|
{
|
|
id: "update-annuncio-all",
|
|
},
|
|
);
|
|
},
|
|
|
|
onSettled: async () => {
|
|
await utils.annunci.getAnnunciList.invalidate();
|
|
toast.success("Aggiornamento completato", {
|
|
id: "update-annuncio-all",
|
|
});
|
|
},
|
|
onError: async (err) => {
|
|
toast.error(
|
|
"Si è verificato un errore durante l'aggiornamento degli annunci: " +
|
|
(err instanceof Error ? err.message : "Errore sconosciuto"),
|
|
{
|
|
id: "update-annuncio-all",
|
|
},
|
|
);
|
|
},
|
|
},
|
|
);
|
|
const { mutateAsync: revalidate } =
|
|
api.revalidation.revalidateAllAnnunci.useMutation({
|
|
onSettled: async () => {
|
|
await utils.annunci.getAnnunciList.invalidate();
|
|
toast.success("Revalidazione completata");
|
|
},
|
|
});
|
|
if (isLoading) return <LoadingPage />;
|
|
if (!data) return <Status500 />;
|
|
return (
|
|
<>
|
|
<Head>
|
|
<title>Annunci - Infoalloggi.it</title>
|
|
</Head>
|
|
|
|
<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="font-bold text-2xl">Annunci</h3>
|
|
<button
|
|
className="cursor-pointer"
|
|
onClick={async () => await refetch()}
|
|
type="button"
|
|
>
|
|
<RefreshCcw className="size-6" />
|
|
</button>
|
|
<Popover>
|
|
<PopoverTrigger asChild>
|
|
<Button size="sm" type="button">
|
|
<CodeSquare /> Strumenti avanzati
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="flex flex-col items-center gap-2">
|
|
<Button
|
|
className="bg-purple-500"
|
|
onClick={async () => {
|
|
await revalidate();
|
|
}}
|
|
size="sm"
|
|
type="button"
|
|
>
|
|
<RefreshCcw /> Rigenera pagine annnunci
|
|
</Button>
|
|
<Confirm
|
|
description="Sei sicuro di voler richiedere l'aggiornamento degli annunci? Questa operazione potrebbe richiedere alcuni minuti prima che le modifiche siano visibili e resetterà eventuali modifiche effettuate dopo la sincronizzazione giornaliera."
|
|
onConfirm={async () => {
|
|
await update();
|
|
}}
|
|
title="Aggiornamento Annunci"
|
|
>
|
|
<Button size="sm" type="button" variant="warning">
|
|
<TriangleAlert /> Aggiorna tutti gli annunci
|
|
</Button>
|
|
</Confirm>
|
|
</PopoverContent>
|
|
</Popover>
|
|
</div>
|
|
</div>
|
|
<AnnunciTable data={data} />
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export async function getServerSideProps() {
|
|
const helpers = generateSSGHelper();
|
|
|
|
await helpers.annunci.getAnnunciList.prefetch();
|
|
|
|
return {
|
|
props: {
|
|
trpcState: helpers.dehydrate(),
|
|
},
|
|
};
|
|
}
|
|
|
|
export default Admin_Annunci;
|
|
|
|
Admin_Annunci.getLayout = function getLayout(page) {
|
|
return <AreaRiservataLayout>{page}</AreaRiservataLayout>;
|
|
};
|