From 47f35c3bbe874a80f1078998954cc8b9a1aef2dd Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Thu, 27 Nov 2025 13:37:37 +0100 Subject: [PATCH] refactor: remove pagination logic and related state management from Annunci and RicercaProvider components --- apps/infoalloggi/src/pages/annunci.tsx | 151 +----------------- .../src/providers/RicercaProvider.tsx | 25 +-- .../src/server/api/routers/annunci.ts | 9 +- .../server/controllers/annunci.controller.ts | 28 +--- 4 files changed, 14 insertions(+), 199 deletions(-) diff --git a/apps/infoalloggi/src/pages/annunci.tsx b/apps/infoalloggi/src/pages/annunci.tsx index 06795f7..5e24b39 100644 --- a/apps/infoalloggi/src/pages/annunci.tsx +++ b/apps/infoalloggi/src/pages/annunci.tsx @@ -75,8 +75,6 @@ const Annunci = ({ options }: AnnunciPageProps) => { {t.annunci.titolo} - - { ); }; -const GoBackButton = () => { - const { page, setPage } = useRicerca(); - if (page >= 2) { - return ( - - ); - } -}; + export default Annunci; Annunci.getLayout = (page: React.ReactNode) => { @@ -127,10 +110,7 @@ export const getServerSideProps = (async () => { }) satisfies GetServerSideProps; const Ricerca = () => { - const { map, page } = useRicerca(); - useEffect(() => { - window.scrollTo({ behavior: "smooth", top: 0 }); - }, [page]); + const { map } = useRicerca(); return (
@@ -343,21 +323,13 @@ const AnnunciList = () => { consegna, sort, map, - page, isLoading: ricercaLoading, - annunciNumber, } = useRicerca(); - const { - data, - status, - //isPlaceholderData - } = api.annunci.getWithCursor.useQuery( + const { data, status } = api.annunci.annunciRicerca.useQuery( { comune: comune || undefined, consegna: consegna || undefined, - page, - pageSize: annunciNumber, sort: sort || undefined, tipologia: tipo || undefined, }, @@ -368,58 +340,6 @@ const AnnunciList = () => { }, ); - /* - DISABILITATO FUNZIONALITA PAGING E PAGE SIZE - - const utils = api.useUtils(); - useEffect(() => { - const prefetchData = async () => { - if (!isPlaceholderData && data?.hasMore) { - await utils.annunci.getWithCursor.prefetch({ - comune: comune || undefined, - consegna: consegna || undefined, - page: page + 1, - pageSize: annunciNumber, - sort: sort || undefined, - tipologia: tipo || undefined, - }); - } - }; - prefetchData().catch((err) => { - console.error("Error prefetching next page:", err); - }); - }, [data, isPlaceholderData, page, annunciNumber]); - - useEffect(() => { - // Reset to page 0 if current page has no data or less than page size - const checkPage = async () => { - if (data) { - if (data.annunci.length === 0 && page !== 0) { - await setPage(0); - } - } - }; - checkPage().catch((err) => { - console.error("Error checking data for page reset:", err); - }); - }, [data, tipo, comune, consegna, sort, setPage, annunciNumber]); - - const [debouncedAnnunciNumber, setDebouncedAnnunciNumber] = - useState(annunciNumber); - - useDebounce( - async () => { - console.log("Setting annunciNumber to", debouncedAnnunciNumber); - console.log("Current annunciNumber is", annunciNumber); - if (debouncedAnnunciNumber !== annunciNumber) { - await setAnnunciNumber(debouncedAnnunciNumber); - await setPage(0); - } - }, - 500, - [debouncedAnnunciNumber], - ); -*/ if (ricercaLoading) return ; if (status === "error") return ; @@ -429,7 +349,7 @@ const AnnunciList = () => { ) : (
- {data.annunci.map((annuncio, idx) => ( + {data.map((annuncio, idx) => ( { ))}
)} - - {/*
-
- - annunci per pagina: - - { - if (val !== undefined) { - setDebouncedAnnunciNumber(val); - } - }} - value={debouncedAnnunciNumber} - /> -
-
- {page >= 2 && ( - - )} - - -
-
- {page + 1} -
-
- -
-
*/}
); }; diff --git a/apps/infoalloggi/src/providers/RicercaProvider.tsx b/apps/infoalloggi/src/providers/RicercaProvider.tsx index 655dbaf..58bd038 100644 --- a/apps/infoalloggi/src/providers/RicercaProvider.tsx +++ b/apps/infoalloggi/src/providers/RicercaProvider.tsx @@ -41,15 +41,8 @@ type RicercaContextType = { tipologieOptions: string[]; consegnaOptions: number[]; sortOptions: typeof sortOptions; - page: number; - setPage: ( - value: number | ((old: number | null) => number | null) | null, - ) => Promise; ResetParams: () => Promise; - annunciNumber: number; - setAnnunciNumber: ( - value: number | ((old: number) => number | null) | null, - ) => Promise; + nFiltri: number; isLoading: boolean; }; @@ -104,15 +97,6 @@ export const RicercaProvider: FC<{ .withOptions({ startTransition }), ); - const [page, setPage] = useQueryState( - "p", - parseAsInteger.withDefault(0).withOptions({ clearOnDefault: true }), - ); - const [annunciNumber, setAnnunciNumber] = useQueryState( - "n", - parseAsInteger.withDefault(1000).withOptions({ startTransition }), - ); - const tipologieOptions = useMemo(() => { const set = new Set(); options.forEach((item) => { @@ -161,7 +145,6 @@ export const RicercaProvider: FC<{ await setComune(""); await setConsegna(null); await setSort("Recenti"); - //await setPage(0); }; const nFiltri = @@ -182,22 +165,18 @@ export const RicercaProvider: FC<{ isLoading, map, nFiltri, - page, ResetParams, setComune, setConsegna, setMap, - setPage, + setSort, setTipo, sort, sortOptions: sortOptions, tipo, tipologieOptions, - - annunciNumber, - setAnnunciNumber, }} > {children} diff --git a/apps/infoalloggi/src/server/api/routers/annunci.ts b/apps/infoalloggi/src/server/api/routers/annunci.ts index 091dc8f..979471b 100644 --- a/apps/infoalloggi/src/server/api/routers/annunci.ts +++ b/apps/infoalloggi/src/server/api/routers/annunci.ts @@ -13,8 +13,8 @@ import { getAnnunciById_rawImgUrls, getAnnunciListHandler, getAnnunciMetaByCod, + getAnnunciRicerca, getCodici_AnnunciHandler, - getCursor_AnnunciHandler, getOptions_AnnunciHandler, getProprietarioDataHandler, } from "~/server/controllers/annunci.controller"; @@ -89,19 +89,18 @@ export const annunciRouter = createTRPCRouter({ servizioId: input.servizioId, }); }), - getWithCursor: publicProcedure + + annunciRicerca: publicProcedure .input( z.object({ comune: z.string().optional(), consegna: z.number().optional(), - page: z.number().optional(), - pageSize: z.number(), sort: z.enum(["Recenti", "Prezzo", "Consegna", "Mq"]).optional(), tipologia: z.string().optional(), }), ) .query(async ({ input }) => { - return await getCursor_AnnunciHandler({ + return await getAnnunciRicerca({ ...input, }); }), diff --git a/apps/infoalloggi/src/server/controllers/annunci.controller.ts b/apps/infoalloggi/src/server/controllers/annunci.controller.ts index 7df8d26..2b15193 100644 --- a/apps/infoalloggi/src/server/controllers/annunci.controller.ts +++ b/apps/infoalloggi/src/server/controllers/annunci.controller.ts @@ -257,24 +257,17 @@ export type AnnuncioRicerca = Pick< videos: Pick[]; }; -export const getCursor_AnnunciHandler = async ({ - page = 0, - pageSize, +export const getAnnunciRicerca = async ({ tipologia, comune, consegna, sort, }: { - page?: number; - pageSize: number; tipologia?: string; comune?: string; consegna?: number; sort?: "Recenti" | "Prezzo" | "Consegna" | "Mq"; -}): Promise<{ - annunci: AnnuncioRicerca[]; - hasMore: boolean; -}> => { +}): Promise => { try { let query = db .selectFrom("annunci") @@ -334,24 +327,11 @@ export const getCursor_AnnunciHandler = async ({ query = query.orderBy("id", "asc"); - const annunci = await query - .limit(pageSize + 1) - .offset(page * pageSize) - .execute(); - - const hasMore = annunci.length > pageSize; - if (annunci.length > pageSize) { - annunci.pop(); // Remove the last item if it exceeds the page size - } - - return { - annunci, - hasMore, - }; + return await query.execute(); } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", - message: `Errore query getWithCursor: ${(e as Error).message}`, + message: `Errore query ricercaAnnunci: ${(e as Error).message}`, }); } };