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 (
@@ -53,35 +90,24 @@ export default function ProductsPage() {
- - - {/* handleKeywordChange(e.target.value)} + value={keywordFilter} errors={errors} validation={{ maxLength: { - value: 30, - message: "30 character max", + value: 15, + message: "15 character max", }, }} - /> */} + />
- { - items && - items.map((item) => ( - - )) - // .filter((item) => { - // return ( - // (!categoryFilter || - // item.category === categoryFilter) && - // (!keywordFilter || - // item.name - // .toLowerCase() - // .includes( - // keywordFilter.toLowerCase(), - // )) && - // (!locationFilter || - // item.location - // .toLowerCase() - // .includes(locationFilter.toLowerCase())) - // ); - // }) - //.map((item) => )} - } + {filteredItems.map((item) => ( + + ))}
);