From 8c303ffeef61c088a09bad9d441bb8fa1ece3d04 Mon Sep 17 00:00:00 2001 From: David Linke Date: Fri, 10 Jan 2025 14:27:16 -0500 Subject: [PATCH] feat(pattern): simplify additive filters logic on filters applied --- .../FiltersAdvancedAdditivePage.tsx | 89 +++++++++---------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/packages/examples-site/src/pages/FiltersAdvancedAdditivePage/FiltersAdvancedAdditivePage.tsx b/packages/examples-site/src/pages/FiltersAdvancedAdditivePage/FiltersAdvancedAdditivePage.tsx index d91a475..69f069a 100644 --- a/packages/examples-site/src/pages/FiltersAdvancedAdditivePage/FiltersAdvancedAdditivePage.tsx +++ b/packages/examples-site/src/pages/FiltersAdvancedAdditivePage/FiltersAdvancedAdditivePage.tsx @@ -235,7 +235,6 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { // set the items setItems(foundItems); setTableItems(foundItems); - setFiltersApplied(true); } }; @@ -262,7 +261,6 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { // FILTERING MODAL const [isFilterModalOpen, setIsFilterModalOpen] = useState(false); - const [filtersApplied, setFiltersApplied] = useState(false); const [filterArray, setFilterArray] = useState([]); useEffect(() => { @@ -305,7 +303,6 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { } }); - setFiltersApplied(true); setItems(filteredItems as Item[]); setTableItems(filteredItems); }, [filterArray]); @@ -321,18 +318,22 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { const clearAllFilters = (e) => { e && e.preventDefault(); setFilterArray([]); - setFiltersApplied(false); }; const openFilterModal = (e) => { e.preventDefault(); // set the modal filter array to the current filter array - const modalFilters = filterArray.length > 0 ? filterArray : [{ - logicalOperator: "where", - field: "categories", - comparisonOperator: "contains", - value: undefined, - }]; + const modalFilters = + filterArray.length > 0 + ? filterArray + : [ + { + logicalOperator: "where", + field: "categories", + comparisonOperator: "contains", + value: undefined, + }, + ]; setModalFilterArray(modalFilters); setIsFilterModalOpen(true); }; @@ -438,7 +439,7 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { { // differentiate from empty search or empty products and show a loader element if the data is being fetched - filtersApplied + filterArray.length > 0 ? `No products were found for the criteria` : "You have no products yet." } @@ -494,7 +495,7 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { - {filtersApplied && ( + {filterArray.length > 0 && ( { alignItems={"center"} > {/* let's showcase the filters applied with chips here*/} - {filterArray.length > 0 && - filterArray.map((filter: Filter, index) => { - const filterFieldCapitalized = - filter.field.charAt(0).toUpperCase() + - filter.field.slice(1); - const filterLogicalOperatorUppercase = - filter.logicalOperator.toUpperCase(); - if (filter.field === "category") { - const cat = - filter.value !== undefined - ? findCategoryById( - productCats, - Number(filter.value) - ) - : undefined; - return ( - { - deleteChip(index); - }} - label={`${ - filter.logicalOperator !== "where" - ? filterLogicalOperatorUppercase - : "" - } ${filterFieldCapitalized} ${ - filter.comparisonOperator - } ${cat?.label}`} - /> - ); - } + {filterArray.map((filter: Filter, index) => { + const filterFieldCapitalized = + filter.field.charAt(0).toUpperCase() + + filter.field.slice(1); + const filterLogicalOperatorUppercase = + filter.logicalOperator.toUpperCase(); + if (filter.field === "category") { + const cat = + filter.value !== undefined + ? findCategoryById(productCats, Number(filter.value)) + : undefined; return ( { deleteChip(index); }} @@ -546,10 +527,26 @@ const PageFiltersAdvancedAdditive: FunctionComponent = () => { : "" } ${filterFieldCapitalized} ${ filter.comparisonOperator - } ${filter.value}`} + } ${cat?.label}`} /> ); - })} + } + return ( + { + deleteChip(index); + }} + label={`${ + filter.logicalOperator !== "where" + ? filterLogicalOperatorUppercase + : "" + } ${filterFieldCapitalized} ${ + filter.comparisonOperator + } ${filter.value}`} + /> + ); + })} {filterArray.length > 0 && (