feat: add revalidation functionality for announcements with confirmation prompts

This commit is contained in:
Marco Pedone 2025-10-15 15:45:24 +02:00
parent 37842177a6
commit 0e157b8ddd
3 changed files with 130 additions and 3 deletions

View file

@ -1,10 +1,17 @@
import { format } from "date-fns";
import { it } from "date-fns/locale";
import { CalendarIcon, Eye, Printer, RotateCw } from "lucide-react";
import {
CalendarIcon,
Eye,
Printer,
RotateCw,
TriangleAlert,
} from "lucide-react";
import Link from "next/link";
import toast from "react-hot-toast";
import z from "zod";
import { CarouselAnnuncio } from "~/components/annuncio_card";
import { Confirm } from "~/components/confirm";
import {
Form,
FormControl,
@ -123,6 +130,18 @@ export const AnnuncioEditForm = ({ data }: { data: Annunci }) => {
},
});
const { mutateAsync: revalidate } =
api.settings.revalidateAnnuncio.useMutation({
onSettled: async () => {
toast.success(
"La revalidazione dell'annuncio è stata richiesta con successo,\n potrebbero volerci alcuni minuti prima che le modifiche siano visibili.",
{
duration: 5000,
},
);
},
});
const defaultValues: FormValues = {
...data,
email: data.email || undefined,
@ -223,7 +242,23 @@ export const AnnuncioEditForm = ({ data }: { data: Annunci }) => {
<Printer /> Scheda
</Button>
</Link>
<div className="hidden items-center gap-2 md:ml-auto md:flex">
<Confirm
description="Sei sicuro di voler richiedere la revalidazione dell'annuncio? Questa operazione potrebbe richiedere alcuni minuti prima che le modifiche siano visibili e resetterà eventuali modifiche effettuate dopo la sincronizzazione giornaliera."
onConfirm={async () => {
await revalidate({ cod: data.codice });
}}
title="Revalidazione Annuncio"
>
<Button
className="flex items-center gap-2"
size="sm"
type="button"
variant="warning"
>
<TriangleAlert /> Rivalida
</Button>
</Confirm>
<div className="flex items-center gap-2 md:ml-auto">
<Button size="sm" type="button" variant="outline">
Annulla
</Button>

View file

@ -1,13 +1,28 @@
import { RefreshCcw } from "lucide-react";
import { RefreshCcw, TriangleAlert } from "lucide-react";
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 type { NextPageWithLayout } from "~/pages/_app";
import { api } from "~/utils/api";
const Admin_Annunci: NextPageWithLayout = () => {
const { data, isLoading, refetch } = api.annunci.getAnnunciList.useQuery();
const { mutateAsync: revalidate } =
api.settings.revalidateAllAnnunci.useMutation({
onSettled: async () => {
toast.success(
"La revalidazione dell'annuncio è stata richiesta con successo,\n potrebbero volerci alcuni minuti prima che le modifiche siano visibili.",
{
duration: 5000,
},
);
},
});
if (isLoading) return <LoadingPage />;
if (!data) return <Status500 />;
return (
@ -23,6 +38,22 @@ const Admin_Annunci: NextPageWithLayout = () => {
>
<RefreshCcw className="size-6" />
</button>
<Confirm
description="Sei sicuro di voler richiedere la revalidazione gli 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 revalidate();
}}
title="Rivalida tutti gli annunci"
>
<Button
className="flex items-center gap-2"
size="sm"
type="button"
variant="warning"
>
<TriangleAlert /> Rivalida tutti gli annunci
</Button>
</Confirm>
</div>
</div>
<AnnunciTable data={data} />

View file

@ -1,4 +1,6 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod/v4";
import { env } from "~/env.mjs";
import type { BannersIdbanner } from "~/schemas/public/Banners";
import {
adminProcedure,
@ -248,4 +250,63 @@ export const settingsRouter = createTRPCRouter({
stringaValue: input.stringaValue,
});
}),
revalidateAllAnnunci: adminProcedure.mutation(async () => {
try {
const response = await fetch(`${env.BACKENDSERVER_URL}/update`);
if (!response.ok) {
const errorData = await response.text().catch(() => "Unknown error");
throw new Error(
`Revalidation failed with status ${response.status}: ${errorData}`,
);
}
const data = await response.json().catch(() => ({}));
return {
status: "success",
details: data,
};
} catch (err) {
console.error("Revalidation error:", err);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: err instanceof Error ? err.message : "Revalidation failed",
cause: err,
});
}
}),
revalidateAnnuncio: adminProcedure
.input(
z.object({
cod: z.string(),
}),
)
.mutation(async ({ input }) => {
try {
const response = await fetch(
`${env.BACKENDSERVER_URL}/update-cod/${input.cod}`,
);
if (!response.ok) {
const errorData = await response.text().catch(() => "Unknown error");
throw new Error(
`Revalidation failed with status ${response.status}: ${errorData}`,
);
}
const data = await response.json().catch(() => ({}));
return {
status: "success",
details: data,
};
} catch (err) {
console.error("Revalidation error:", err);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: err instanceof Error ? err.message : "Revalidation failed",
cause: err,
});
}
}),
});