From 0338e0fbb45ba01e906272682705708b7bc7c796 Mon Sep 17 00:00:00 2001 From: David Linke Date: Fri, 10 Jan 2025 15:00:48 -0500 Subject: [PATCH] feat(pattern): fix search on advanced and additive filters --- .../FiltersAdvPage/FiltersAdvancedPage.tsx | 17 +++++--------- .../FiltersAdvancedAdditivePage.tsx | 23 ++++++++++--------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/packages/examples-site/src/pages/FiltersAdvPage/FiltersAdvancedPage.tsx b/packages/examples-site/src/pages/FiltersAdvPage/FiltersAdvancedPage.tsx index 007297e..f03f251 100644 --- a/packages/examples-site/src/pages/FiltersAdvPage/FiltersAdvancedPage.tsx +++ b/packages/examples-site/src/pages/FiltersAdvPage/FiltersAdvancedPage.tsx @@ -49,6 +49,7 @@ import { formatPrice } from "../../helpers/price"; interface Item extends DummyItem, TableItem {} interface Filter { + name?: string; category: number[]; priceMin: number | undefined; priceMax: number | undefined; @@ -233,27 +234,19 @@ const PageFiltersAdvanced: FunctionComponent = () => { setSearchValue(event.target.value); // let's reset the items to the original data if the search value is empty if (!event.target.value) { - setItems(allItems); - setTableItems(allItems); + handleFiltering(); } }; // search submission handler const onSearchSubmit = (e) => { e.preventDefault(); - if (searchValue) { - // let's find the items - const foundItems = items.filter((item) => - item.name.toLowerCase().includes(searchValue.toLowerCase()) - ); - // set the items - setItems(foundItems); - setTableItems(foundItems); - } + handleFiltering(); }; const clearAllFilters = (e) => { e && e.preventDefault(); setFilters({ + name: filters.name, category: [], priceMin: undefined, priceMax: undefined, @@ -329,6 +322,7 @@ const PageFiltersAdvanced: FunctionComponent = () => { */ const [isFilterModalOpen, setIsFilterModalOpen] = useState(false); const [filters, setFilters] = useState({ + name: undefined, category: [], priceMin: undefined, priceMax: undefined, @@ -352,6 +346,7 @@ const PageFiltersAdvanced: FunctionComponent = () => { const applyModalFilters = () => { setFilters({ + name: filters.name, category: modalFilters.category, priceMin: modalFilters.priceMin, priceMax: modalFilters.priceMax, diff --git a/packages/examples-site/src/pages/FiltersAdvancedAdditivePage/FiltersAdvancedAdditivePage.tsx b/packages/examples-site/src/pages/FiltersAdvancedAdditivePage/FiltersAdvancedAdditivePage.tsx index 69f069a..ff92081 100644 --- a/packages/examples-site/src/pages/FiltersAdvancedAdditivePage/FiltersAdvancedAdditivePage.tsx +++ b/packages/examples-site/src/pages/FiltersAdvancedAdditivePage/FiltersAdvancedAdditivePage.tsx @@ -220,8 +220,7 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { setSearchValue(event.target.value); // let's reset the items to the original data if the search value is empty if (!event.target.value) { - setItems(allItems); - setTableItems(allItems); + setFilterArray(filterArray); } }; // search submission handler @@ -229,12 +228,7 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { e.preventDefault(); if (searchValue) { // let's find the items - const foundItems = items.filter((item) => - item.name.toLowerCase().includes(searchValue.toLowerCase()) - ); - // set the items - setItems(foundItems); - setTableItems(foundItems); + setFilterArray(filterArray); } }; @@ -266,6 +260,13 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { useEffect(() => { let filteredItems = [...allItems]; + // lets include search + if (searchValue) { + filteredItems = filteredItems.filter((item) => + item.name.toLowerCase().includes(searchValue.toLowerCase()) + ); + } + const filterfunction = (baseArray, filter) => { return baseArray.filter((item) => { // let's switch through the comparison operators @@ -454,8 +455,8 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { navigate("/"), @@ -510,7 +511,7 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { filter.field.slice(1); const filterLogicalOperatorUppercase = filter.logicalOperator.toUpperCase(); - if (filter.field === "category") { + if (filter.field === "categories") { const cat = filter.value !== undefined ? findCategoryById(productCats, Number(filter.value))