feat: add revalidation functionality for announcements with confirmation prompts
This commit is contained in:
parent
37842177a6
commit
0e157b8ddd
3 changed files with 130 additions and 3 deletions
|
|
@ -1,10 +1,17 @@
|
||||||
import { format } from "date-fns";
|
import { format } from "date-fns";
|
||||||
import { it } from "date-fns/locale";
|
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 Link from "next/link";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import z from "zod";
|
import z from "zod";
|
||||||
import { CarouselAnnuncio } from "~/components/annuncio_card";
|
import { CarouselAnnuncio } from "~/components/annuncio_card";
|
||||||
|
import { Confirm } from "~/components/confirm";
|
||||||
import {
|
import {
|
||||||
Form,
|
Form,
|
||||||
FormControl,
|
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 = {
|
const defaultValues: FormValues = {
|
||||||
...data,
|
...data,
|
||||||
email: data.email || undefined,
|
email: data.email || undefined,
|
||||||
|
|
@ -223,7 +242,23 @@ export const AnnuncioEditForm = ({ data }: { data: Annunci }) => {
|
||||||
<Printer /> Scheda
|
<Printer /> Scheda
|
||||||
</Button>
|
</Button>
|
||||||
</Link>
|
</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">
|
<Button size="sm" type="button" variant="outline">
|
||||||
Annulla
|
Annulla
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
||||||
|
|
@ -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 { AreaRiservataLayout } from "~/components/Layout";
|
||||||
import { LoadingPage } from "~/components/loading";
|
import { LoadingPage } from "~/components/loading";
|
||||||
import { Status500 } from "~/components/status-page";
|
import { Status500 } from "~/components/status-page";
|
||||||
import { AnnunciTable } from "~/components/tables/annunci-tables";
|
import { AnnunciTable } from "~/components/tables/annunci-tables";
|
||||||
|
import { Button } from "~/components/ui/button";
|
||||||
import type { NextPageWithLayout } from "~/pages/_app";
|
import type { NextPageWithLayout } from "~/pages/_app";
|
||||||
import { api } from "~/utils/api";
|
import { api } from "~/utils/api";
|
||||||
|
|
||||||
const Admin_Annunci: NextPageWithLayout = () => {
|
const Admin_Annunci: NextPageWithLayout = () => {
|
||||||
const { data, isLoading, refetch } = api.annunci.getAnnunciList.useQuery();
|
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 (isLoading) return <LoadingPage />;
|
||||||
if (!data) return <Status500 />;
|
if (!data) return <Status500 />;
|
||||||
return (
|
return (
|
||||||
|
|
@ -23,6 +38,22 @@ const Admin_Annunci: NextPageWithLayout = () => {
|
||||||
>
|
>
|
||||||
<RefreshCcw className="size-6" />
|
<RefreshCcw className="size-6" />
|
||||||
</button>
|
</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>
|
||||||
</div>
|
</div>
|
||||||
<AnnunciTable data={data} />
|
<AnnunciTable data={data} />
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
|
import { TRPCError } from "@trpc/server";
|
||||||
import { z } from "zod/v4";
|
import { z } from "zod/v4";
|
||||||
|
import { env } from "~/env.mjs";
|
||||||
import type { BannersIdbanner } from "~/schemas/public/Banners";
|
import type { BannersIdbanner } from "~/schemas/public/Banners";
|
||||||
import {
|
import {
|
||||||
adminProcedure,
|
adminProcedure,
|
||||||
|
|
@ -248,4 +250,63 @@ export const settingsRouter = createTRPCRouter({
|
||||||
stringaValue: input.stringaValue,
|
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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue