diff --git a/src/pages/products/index.jsx b/src/pages/products/index.jsx index 81886b0..f1cd385 100644 --- a/src/pages/products/index.jsx +++ b/src/pages/products/index.jsx @@ -15,6 +15,7 @@ export default function ProductsPage() { const { register, handleSubmit, + setValue, formState: { errors, isSubmitting }, } = useForm(); @@ -22,11 +23,13 @@ export default function ProductsPage() { const [keywordFilter, setKeywordFilter] = useState(""); const [locationFilter, setLocationFilter] = useState(""); const [items, setItems] = useState([]); + const [filteredItems, setFilteredItems] = useState([]); const onSubmit = (data) => { setKeywordFilter(data.keyword || ""); setLocationFilter(data.location || ""); }; + useEffect(() => { async function getItems() { let items = await getAllItems("items"); @@ -34,6 +37,40 @@ export default function ProductsPage() { } getItems(); }, []); + + useEffect(() => { + // Function to filter items based on category, keyword, and location + const filterItems = () => { + const filtered = items.filter((item) => { + return ( + (!categoryFilter || item.category === categoryFilter) && + (!keywordFilter || + (item.name && + item.name + .toLowerCase() + .includes(keywordFilter.toLowerCase())) || + (item.description && + item.description + .toLowerCase() + .includes(keywordFilter.toLowerCase()))) && + (!locationFilter || + (item.location && + item.location + .toLowerCase() + .includes(locationFilter.toLowerCase()))) + ); + }); + setFilteredItems(filtered); + }; + + filterItems(); + }, [items, categoryFilter, keywordFilter, locationFilter]); + + const handleKeywordChange = (value) => { + setKeywordFilter(value); + setValue("keyword", value); + }; + return (