diff --git a/apps/infoalloggi/src/providers/RicercaProvider.tsx b/apps/infoalloggi/src/providers/RicercaProvider.tsx index 83c9206..bba3b37 100644 --- a/apps/infoalloggi/src/providers/RicercaProvider.tsx +++ b/apps/infoalloggi/src/providers/RicercaProvider.tsx @@ -13,9 +13,7 @@ import { type ReactNode, useCallback, useContext, - useEffect, useMemo, - useState, useTransition, } from "react"; @@ -105,7 +103,6 @@ export const RicercaProvider: FC<{ children: ReactNode; options: AnnunciOptions; }> = ({ children, options }) => { - const [nFiltri, setNFiltri] = useState(0); const [isLoading, startTransition] = useTransition(); const [ @@ -135,77 +132,55 @@ export const RicercaProvider: FC<{ }); }, []); - const tipologieOptions = useMemo(() => { - const set = new Set(); - options.forEach((item) => { - if (!comune || item.comune === comune) { - if (!consegna || consegna.includes(item.consegna)) { - if (!minBudget || item.prezzo >= minBudget) { - if (!maxBudget || item.prezzo <= maxBudget) { - set.add(item.tipo); - } - } - } - } - }); - return Array.from(set); - }, [options, comune, consegna, minBudget, maxBudget]); + const { tipologieOptions, comuniOptions, consegnaOptions } = useMemo(() => { + const typeSet = new Set(); + const comuneSet = new Set(); + const consegnaSet = new Set(); - const comuniOptions = useMemo(() => { - const set = new Set(); - options.forEach((item) => { - if (!tipo || item.tipo === tipo) { - if (!consegna || consegna.includes(item.consegna)) { - if (!minBudget || item.prezzo >= minBudget) { - if (!maxBudget || item.prezzo <= maxBudget) { - set.add(item.comune); - } - } - } - } - }); - return Array.from(set).sort((a, b) => a.localeCompare(b)); - }, [options, tipo, consegna, minBudget, maxBudget]); + for (const item of options) { + const matchesTipo = !tipo || item.tipo === tipo; + const matchesComune = !comune || item.comune === comune; + const matchesConsegna = !consegna || consegna.includes(item.consegna); + const matchesBudget = + (!minBudget || item.prezzo >= minBudget) && + (!maxBudget || item.prezzo <= maxBudget); - const consegnaOptions = useMemo(() => { - const set = new Set(); - options.forEach((item) => { - if (!tipo || item.tipo === tipo) { - if (!comune || item.comune === comune) { - if (!minBudget || item.prezzo >= minBudget) { - if (!maxBudget || item.prezzo <= maxBudget) { - set.add(item.consegna); - } - } - } + // Add to Tipologie if it matches everything ELSE (Comune, Consegna, Budget) + if (matchesComune && matchesConsegna && matchesBudget) { + typeSet.add(item.tipo); } - }); + + // Add to Comuni if it matches everything ELSE (Tipo, Consegna, Budget) + if (matchesTipo && matchesConsegna && matchesBudget) { + comuneSet.add(item.comune); + } + + // Add to Consegna if it matches everything ELSE (Tipo, Comune, Budget) + if (matchesTipo && matchesComune && matchesBudget) { + consegnaSet.add(item.consegna); + } + } + const currentMonth = new Date().getMonth() + 1; // 1-12 - return Array.from(set).sort((a, b) => { - // Calculate distance from current month (wrapping around) - const distA = (a - currentMonth + 12) % 12; - const distB = (b - currentMonth + 12) % 12; - return distA - distB; - }); - }, [options, tipo, comune, minBudget, maxBudget]); + return { + tipologieOptions: Array.from(typeSet), + comuniOptions: Array.from(comuneSet).sort((a, b) => a.localeCompare(b)), + consegnaOptions: Array.from(consegnaSet).sort((a, b) => { + const distA = (a - currentMonth + 12) % 12; + const distB = (b - currentMonth + 12) % 12; + return distA - distB; + }), + }; + }, [options, tipo, comune, consegna, minBudget, maxBudget]); - const updateNFiltri = () => { - setNFiltri( - [tipo, comune].reduce( - (count, item) => (item !== "" ? count + 1 : count), - 0, - ) + - (consegna !== null ? 1 : 0) + - (sort !== "Recenti" ? 1 : 0) + - (minBudget !== null ? 1 : 0) + - (maxBudget !== null ? 1 : 0), - ); - }; - - useEffect(() => { - updateNFiltri(); - }, [tipo, comune, consegna, minBudget, maxBudget, sort]); + const nFiltri = + (tipo ? 1 : 0) + + (comune ? 1 : 0) + + (consegna !== null && consegna.length > 0 ? 1 : 0) + + (sort !== "Recenti" ? 1 : 0) + + (minBudget !== null ? 1 : 0) + + (maxBudget !== null ? 1 : 0); return (