From 0784298a64e3abac15839c56910ec95620586b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m?= Date: Thu, 28 Mar 2024 11:45:27 +0100 Subject: [PATCH] Filter bar refactor --- web/src/App.js | 29 +++-------------------------- web/src/FilterBar.js | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/web/src/App.js b/web/src/App.js index 78e4f27..85fa9a5 100644 --- a/web/src/App.js +++ b/web/src/App.js @@ -38,27 +38,6 @@ function App() { localStorage.setItem("filters", JSON.stringify(filters)); }, [filters]); - const addFilter = (filter) => { - setFilters([...filters, filter]); - } - - const filterValueByProperty = (property) => { - const filter = filters.find(f => f.property === property) - if (!filter) { - return "" - } - - return filter.value - } - - const deleteFilter = (filter) => { - setFilters(filters.filter(f => f.property !== filter.property)) - } - - const resetFilters = () => { - setFilters([]) - } - return ( <> @@ -66,12 +45,10 @@ function App() {
-
diff --git a/web/src/FilterBar.js b/web/src/FilterBar.js index bbc7358..6ff5ea6 100644 --- a/web/src/FilterBar.js +++ b/web/src/FilterBar.js @@ -1,7 +1,28 @@ import React, { useState, useRef, useEffect } from 'react'; import { FunnelIcon, XMarkIcon } from '@heroicons/react/24/outline' -function FilterBar({ filters, addFilter, deleteFilter, resetFilters, filterValueByProperty }) { +function FilterBar({ properties = [], filters, change }) { + const addFilter = (filter) => { + change([...filters, filter]); + } + + const filterValueByProperty = (property) => { + const filter = filters.find(f => f.property === property) + if (!filter) { + return "" + } + + return filter.value + } + + const deleteFilter = (filter) => { + change(filters.filter(f => f.property !== filter.property)) + } + + const resetFilters = () => { + change([]) + } + return (
@@ -10,7 +31,7 @@ function FilterBar({ filters, addFilter, deleteFilter, resetFilters, filterValue {filters.map(filter => ( ))} - +
  @@ -41,8 +62,7 @@ function FilterInput(props) { const [active, setActive] = useState(false) const [property, setProperty] = useState("") const [value, setValue] = useState("") - const properties=["Service", "Namespace", "Domain"] - const { addFilter, filterValueByProperty } = props; + const { properties, addFilter, filterValueByProperty } = props; const inputRef = useRef(null); const reset = () => { @@ -76,7 +96,7 @@ function FilterInput(props) { setActive(false); if (value !== "") { if (property === "") { - addFilter({property: "Service", value: value}) + addFilter({property: properties[0], value: value}) } else { addFilter({property, value}) } @@ -92,7 +112,7 @@ function FilterInput(props) { if (e.keyCode === 13){ setActive(false) if (property === "") { - addFilter({property: "Service", value: value}) + addFilter({property: properties[0], value: value}) } else { addFilter({property, value}) } @@ -117,7 +137,10 @@ function FilterInput(props) { return (
  • { setProperty(p); setActive(false); }}> + onClick={() => { + setProperty(p); + setActive(false); + }}> {p}
  • ) })}