Generateurv2/frontend/hooks/useFilter.hook.jsx

96 lines
2.7 KiB
React
Raw Permalink Normal View History

2022-05-18 10:15:54 +02:00
import { useEffect } from "react";
export default function useFilter(data, selectedOption, search, setExData, sortMode) {
useEffect(() => {
if (
data &&
(selectedOption == null ||
(selectedOption != null ? selectedOption.length == 0 : false))
) {
setExData && setExData(
data.filter((s) => {
return search
.toLowerCase()
.sansAccent()
.split(" ")
.every((n) => s.name.toLowerCase().sansAccent().includes(n));
})
);
} else if (
data &&
(selectedOption != null ||
(selectedOption != null ? selectedOption.length != 0 : false)) &&
sortMode == false &&
search == ""
) {
setExData && setExData(
data.filter((s) => {
return s.tags
.map((t) => t.id_code)
.some((r) => selectedOption.map((tt) => tt.value).includes(r));
})
);
} else if (
data &&
(selectedOption != null ||
(selectedOption != null ? selectedOption.length != 0 : false)) &&
sortMode == true &&
search == ""
) {
setExData && setExData(
data.filter((s) => {
return selectedOption
.map((opt) => opt.value)
.every((r) => s.tags.map((tt) => tt.id_code).includes(r));
})
);
} else if (
data &&
(selectedOption != null ||
(selectedOption != null ? selectedOption.length != 0 : false)) &&
sortMode == false &&
search != ""
) {
setExData && setExData(
data
.filter((s) => {
return search
.toLowerCase()
.sansAccent()
.split(" ")
.every((n) => s.name.toLowerCase().sansAccent().includes(n));
})
.filter((s) => {
return s.tags
.map((t) => t.id_code)
.some((r) => selectedOption.map((tt) => tt.value).includes(r));
})
);
} else if (
data &&
(selectedOption != null ||
(selectedOption != null ? selectedOption.length != 0 : false)) &&
sortMode == true &&
search != ""
) {
setExData && setExData(
data
.filter((s) => {
return search
.toLowerCase()
.sansAccent()
.split(" ")
.every((n) => s.name.toLowerCase().sansAccent().includes(n));
})
.filter((s) => {
return selectedOption
.map((opt) => opt.value)
.every((r) => s.tags.map((tt) => tt.id_code).includes(r));
})
);
}
return () => {
setExData && setExData([]);
};
}, [data, selectedOption, search, sortMode]);
}