2025-10-17 19:14:27 +02:00
import { CodeSquare , RefreshCcw , TriangleAlert } from "lucide-react" ;
2026-01-19 10:23:16 +01:00
import Head from "next/head" ;
2025-10-15 15:45:24 +02:00
import toast from "react-hot-toast" ;
import { Confirm } from "~/components/confirm" ;
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 { AnnunciTable } from "~/components/tables/annunci-tables" ;
2025-10-15 15:45:24 +02:00
import { Button } from "~/components/ui/button" ;
2025-10-17 19:14:27 +02:00
import {
Popover ,
PopoverContent ,
PopoverTrigger ,
} from "~/components/ui/popover" ;
2025-08-28 18:27:07 +02:00
import type { NextPageWithLayout } from "~/pages/_app" ;
2025-10-17 18:12:10 +02:00
import { generateSSGHelper } 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
2026-03-09 17:33:02 +01:00
/ * *
* Pagina di gestione annunci per admin : / a r e a - r i s e r v a t a / a d m i n / a n n u n c i
* /
2025-08-04 17:45:44 +02:00
const Admin_Annunci : NextPageWithLayout = ( ) = > {
2025-08-28 18:27:07 +02:00
const { data , isLoading , refetch } = api . annunci . getAnnunciList . useQuery ( ) ;
2025-10-17 19:48:28 +02:00
const utils = api . useUtils ( ) ;
2025-11-20 12:38:42 +01:00
const { mutateAsync : update } = api . revalidation . updateAllAnnunci . useMutation (
{
2026-03-02 16:33:54 +01:00
onMutate : ( ) = > {
toast . loading (
"Richiesta di aggiornamento inviata, attendere prego..." ,
{
id : "update-annuncio-all" ,
} ,
) ;
} ,
2025-11-20 12:38:42 +01:00
onSettled : async ( ) = > {
await utils . annunci . getAnnunciList . invalidate ( ) ;
2026-03-02 16:33:54 +01:00
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" ) ,
2025-11-20 12:38:42 +01:00
{
2026-03-02 16:33:54 +01:00
id : "update-annuncio-all" ,
2025-11-20 12:38:42 +01:00
} ,
) ;
} ,
2025-10-17 19:14:27 +02:00
} ,
2025-11-20 12:38:42 +01:00
) ;
2025-10-15 15:45:24 +02:00
const { mutateAsync : revalidate } =
2025-11-20 12:38:42 +01:00
api . revalidation . revalidateAllAnnunci . useMutation ( {
2025-10-15 15:45:24 +02:00
onSettled : async ( ) = > {
2025-10-17 19:48:28 +02:00
await utils . annunci . getAnnunciList . invalidate ( ) ;
2025-10-17 19:14:27 +02:00
toast . success ( "Revalidazione completata" ) ;
2025-10-15 15:45:24 +02:00
} ,
} ) ;
2025-08-28 18:27:07 +02:00
if ( isLoading ) return < LoadingPage / > ;
if ( ! data ) return < Status500 / > ;
return (
2026-01-19 10:23:16 +01:00
< >
< 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
2025-10-17 19:14:27 +02:00
< / Button >
2026-01-19 10:23:16 +01:00
< 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 >
2025-08-28 18:27:07 +02:00
< / div >
2026-01-19 10:23:16 +01:00
< AnnunciTable data = { data } / >
2025-08-28 18:27:07 +02:00
< / div >
< / div >
2026-01-19 10:23:16 +01:00
< / >
2025-08-28 18:27:07 +02:00
) ;
2025-08-04 17:45:44 +02:00
} ;
2025-10-17 18:12:10 +02:00
export async function getServerSideProps() {
const helpers = generateSSGHelper ( ) ;
await helpers . annunci . getAnnunciList . prefetch ( ) ;
return {
props : {
trpcState : helpers.dehydrate ( ) ,
} ,
} ;
}
2025-08-04 17:45:44 +02:00
export default Admin_Annunci ;
Admin_Annunci . getLayout = function getLayout ( page ) {
2025-08-28 18:27:07 +02:00
return < AreaRiservataLayout > { page } < / AreaRiservataLayout > ;
2025-08-04 17:45:44 +02:00
} ;