refactor: simplify filter logic in RicercaProvider; remove unused state and optimize options generation
This commit is contained in:
parent
edaee780d6
commit
746b0dd1bf
1 changed files with 43 additions and 68 deletions
|
|
@ -13,9 +13,7 @@ import {
|
||||||
type ReactNode,
|
type ReactNode,
|
||||||
useCallback,
|
useCallback,
|
||||||
useContext,
|
useContext,
|
||||||
useEffect,
|
|
||||||
useMemo,
|
useMemo,
|
||||||
useState,
|
|
||||||
useTransition,
|
useTransition,
|
||||||
} from "react";
|
} from "react";
|
||||||
|
|
||||||
|
|
@ -105,7 +103,6 @@ export const RicercaProvider: FC<{
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
options: AnnunciOptions;
|
options: AnnunciOptions;
|
||||||
}> = ({ children, options }) => {
|
}> = ({ children, options }) => {
|
||||||
const [nFiltri, setNFiltri] = useState(0);
|
|
||||||
const [isLoading, startTransition] = useTransition();
|
const [isLoading, startTransition] = useTransition();
|
||||||
|
|
||||||
const [
|
const [
|
||||||
|
|
@ -135,77 +132,55 @@ export const RicercaProvider: FC<{
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const tipologieOptions = useMemo(() => {
|
const { tipologieOptions, comuniOptions, consegnaOptions } = useMemo(() => {
|
||||||
const set = new Set<string>();
|
const typeSet = new Set<string>();
|
||||||
options.forEach((item) => {
|
const comuneSet = new Set<string>();
|
||||||
if (!comune || item.comune === comune) {
|
const consegnaSet = new Set<number>();
|
||||||
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 comuniOptions = useMemo(() => {
|
for (const item of options) {
|
||||||
const set = new Set<string>();
|
const matchesTipo = !tipo || item.tipo === tipo;
|
||||||
options.forEach((item) => {
|
const matchesComune = !comune || item.comune === comune;
|
||||||
if (!tipo || item.tipo === tipo) {
|
const matchesConsegna = !consegna || consegna.includes(item.consegna);
|
||||||
if (!consegna || consegna.includes(item.consegna)) {
|
const matchesBudget =
|
||||||
if (!minBudget || item.prezzo >= minBudget) {
|
(!minBudget || item.prezzo >= minBudget) &&
|
||||||
if (!maxBudget || item.prezzo <= maxBudget) {
|
(!maxBudget || item.prezzo <= maxBudget);
|
||||||
set.add(item.comune);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return Array.from(set).sort((a, b) => a.localeCompare(b));
|
|
||||||
}, [options, tipo, consegna, minBudget, maxBudget]);
|
|
||||||
|
|
||||||
const consegnaOptions = useMemo(() => {
|
// Add to Tipologie if it matches everything ELSE (Comune, Consegna, Budget)
|
||||||
const set = new Set<number>();
|
if (matchesComune && matchesConsegna && matchesBudget) {
|
||||||
options.forEach((item) => {
|
typeSet.add(item.tipo);
|
||||||
if (!tipo || item.tipo === tipo) {
|
}
|
||||||
if (!comune || item.comune === comune) {
|
|
||||||
if (!minBudget || item.prezzo >= minBudget) {
|
// Add to Comuni if it matches everything ELSE (Tipo, Consegna, Budget)
|
||||||
if (!maxBudget || item.prezzo <= maxBudget) {
|
if (matchesTipo && matchesConsegna && matchesBudget) {
|
||||||
set.add(item.consegna);
|
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
|
const currentMonth = new Date().getMonth() + 1; // 1-12
|
||||||
|
|
||||||
return Array.from(set).sort((a, b) => {
|
return {
|
||||||
// Calculate distance from current month (wrapping around)
|
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 distA = (a - currentMonth + 12) % 12;
|
||||||
const distB = (b - currentMonth + 12) % 12;
|
const distB = (b - currentMonth + 12) % 12;
|
||||||
return distA - distB;
|
return distA - distB;
|
||||||
});
|
}),
|
||||||
}, [options, tipo, comune, minBudget, maxBudget]);
|
};
|
||||||
|
}, [options, tipo, comune, consegna, minBudget, maxBudget]);
|
||||||
|
|
||||||
const updateNFiltri = () => {
|
const nFiltri =
|
||||||
setNFiltri(
|
(tipo ? 1 : 0) +
|
||||||
[tipo, comune].reduce(
|
(comune ? 1 : 0) +
|
||||||
(count, item) => (item !== "" ? count + 1 : count),
|
(consegna !== null && consegna.length > 0 ? 1 : 0) +
|
||||||
0,
|
|
||||||
) +
|
|
||||||
(consegna !== null ? 1 : 0) +
|
|
||||||
(sort !== "Recenti" ? 1 : 0) +
|
(sort !== "Recenti" ? 1 : 0) +
|
||||||
(minBudget !== null ? 1 : 0) +
|
(minBudget !== null ? 1 : 0) +
|
||||||
(maxBudget !== null ? 1 : 0),
|
(maxBudget !== null ? 1 : 0);
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
updateNFiltri();
|
|
||||||
}, [tipo, comune, consegna, minBudget, maxBudget, sort]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<RicercaContext.Provider
|
<RicercaContext.Provider
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue