From 34bb54f8b1b962ac6c113c0d358fd97be3ea3004 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Sat, 16 Dec 2023 15:46:26 +0800 Subject: [PATCH] Added a search bar for filtering results in equipment instance list page --- .../EquipmentInstancesListPage.tsx | 127 +++++++++++++----- 1 file changed, 90 insertions(+), 37 deletions(-) diff --git a/src/Pages/EquipmentInstancesListPage/EquipmentInstancesListPage.tsx b/src/Pages/EquipmentInstancesListPage/EquipmentInstancesListPage.tsx index 2de21a2..e3b0ee8 100644 --- a/src/Pages/EquipmentInstancesListPage/EquipmentInstancesListPage.tsx +++ b/src/Pages/EquipmentInstancesListPage/EquipmentInstancesListPage.tsx @@ -14,6 +14,7 @@ import { colors } from "../../styles"; import EditItemModal from "../../Components/EditItemInstanceModal/EditItemInstanceModal"; import { useState } from "react"; import Popup from "reactjs-popup"; +import Autocomplete from "@mui/material/Autocomplete"; export default function EquipmentInstancesListPage() { const [editmodalOpen, SetEditModalOpen] = useState(false); @@ -22,6 +23,7 @@ export default function EquipmentInstancesListPage() { queryKey: ["equipment_instances"], queryFn: EquipmentInstancesAPI, }); + const [filter, setFilter] = useState(null); if (equipment_instances.isLoading) { return (
@@ -65,6 +67,37 @@ export default function EquipmentInstancesListPage() { flexWrap: "wrap", }} > +
+

Filter:

+ + theme.palette.getContrastText(theme.palette.background.paper), + }, + }} + value={filter} + onChange={(_event, newValue) => { + setFilter(newValue); + }} + freeSolo + id="custom-input-demo" + options={["Available", "Broken", "Glassware", "Miscellaneous"]} + renderInput={(params) => ( +
+ +
+ )} + /> +
{equipment_instances.data ? ( - equipment_instances.data.map((equipment) => ( - { - SetSelectedItem(equipment.id); - SetEditModalOpen(true); - }} - > - - {equipment.id} - - - {equipment.equipment_name} - + equipment_instances.data + .filter((equipment) => + filter !== null + ? // If filter is not null, we filter if it matches any criteria + equipment.equipment_name + .toLowerCase() + .includes(filter.toLowerCase()) || + equipment.category + .toLowerCase() + .includes(filter.toLowerCase()) || + equipment.remarks + .toLowerCase() + .includes(filter.toLowerCase()) || + equipment.last_updated + .toLowerCase() + .includes(filter.toLowerCase()) || + equipment.status.toLowerCase() == filter.toLowerCase() + : // If filter keyword is null then we just pass through everything as if we did not filter at all + true + ) + .map((equipment) => ( + { + SetSelectedItem(equipment.id); + SetEditModalOpen(true); + }} + > + + {equipment.id} + + + {equipment.equipment_name} + - - {equipment.status} - - - {equipment.category} - - -
-
{equipment.last_updated}
-
- {equipment.last_updated_by - ? "by " + equipment.last_updated_by - : ""} + + {equipment.status} + + + {equipment.category} + + +
+
{equipment.last_updated}
+
+ {equipment.last_updated_by + ? "by " + equipment.last_updated_by + : ""} +
-
- - - )) + + + )) ) : ( <> )}