From 06d513b14e5fe68ee5d441453d794fd72daf2a31 Mon Sep 17 00:00:00 2001 From: Ali Aziri Date: Tue, 11 Jul 2023 22:37:39 +0200 Subject: [PATCH] feat: adjusted podstable Signed-off-by: Ali Aziri --- Explorer/src/components/podsTable.tsx | 85 ++++++++++++++------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/Explorer/src/components/podsTable.tsx b/Explorer/src/components/podsTable.tsx index 4f0e1b5..f7ee605 100644 --- a/Explorer/src/components/podsTable.tsx +++ b/Explorer/src/components/podsTable.tsx @@ -6,39 +6,33 @@ import React, { useState } from "react"; import { HealthIndicatorBadge } from "./health_indicators"; export default function PodTable({ list }: { list: PodList }): JSX.Element { - const [searchTerm, setSearchTerm] = useState(""); - const [filteredPods, setFilteredPods] = useState(list); - - const handleSearch = () => { - const filtered = list.filter( - (pod) => - pod.name.toLowerCase().includes(searchTerm.toLowerCase()) || - pod.namespace.toLowerCase().includes(searchTerm.toLowerCase()) - ); - setFilteredPods(filtered); - }; - - const handleInputChange = (e: React.ChangeEvent) => { - setSearchTerm(e.target.value); - }; - - const handleKeyPress = (e: React.KeyboardEvent) => { - if (e.key === "Enter") { - handleSearch(); + const [searchTerm, setSearchTerm] = useState(""); + const [sortDirection, setSortDirection] = useState<"asc" | "desc" | "none">( + "none" + ); + const [searchActive, setSearchActive] = useState(false); + const sortFn = (a: Pod, b: Pod) => { + if (sortDirection === "none") { + // don't change anything, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description + return 0; + } + if (sortDirection === "asc") { + return a.status_phase.localeCompare(b.status_phase); } + // must be desc + return b.status_phase.localeCompare(a.status_phase); }; - - // const handleSortAsc = () => { - // const sorted = [...filteredPods]; - // sorted.sort((a, b) => a.status.localeCompare(b.status)); - // setFilteredPods(sorted); - // }; - - // const handleSortDsc = () => { - // const sorted = [...filteredPods]; - // sorted.sort((a, b) => b.status.localeCompare(a.status)); - // setFilteredPods(sorted); - // }; + const displayList = list + .filter((pod) => { + if (searchTerm === "" || !searchActive) { + return true; + } + return ( + pod.namespace.toLowerCase().includes(searchTerm.toLowerCase()) || + pod.name.toLowerCase().includes(searchTerm.toLowerCase()) + ); + }) + .sort(sortFn); return (
@@ -47,13 +41,20 @@ export default function PodTable({ list }: { list: PodList }): JSX.Element { type="text" placeholder="Search..." value={searchTerm} - onChange={handleInputChange} - onKeyPress={handleKeyPress} + onChange={(e) => { + setSearchTerm(e.target.value); + setSearchActive(false); + }} + onKeyDown={(e) => { + if (e.key === "Enter") { + setSearchActive(true); + } + }} className="border border-gray-300 px-4 py-2 rounded-md" /> + + setSortDirection("asc")}> + Ascending + + setSortDirection("desc")}> + Descending - - - */} - + {" "} - {filteredPods.map((pod: Pod, index: number) => ( + {displayList.map((pod: Pod, index: number) => (