Skip to content

Commit

Permalink
add simple search component
Browse files Browse the repository at this point in the history
  • Loading branch information
mgtennant committed Mar 28, 2024
1 parent 60aa565 commit ca33ba3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
30 changes: 30 additions & 0 deletions frontend/src/app/components/common/SimpleSearch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { useState } from 'react';

interface SimpleSearchProps<T extends Record<string, any>> {
searchKeys: string[];
data: T[];
onSearch: (filteredData: T[]) => void;
}

const SimpleSearch = <T extends Record<string, any>>({ searchKeys, data, onSearch }: SimpleSearchProps<T>) => {
const [filterText, setFilterText] = useState('');

const handleFilterChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const text = event.target.value;
setFilterText(text);

const filteredData = data.filter((item) =>
searchKeys.some((key) => item[key]?.toString().toLowerCase().includes(text.toLowerCase()))
);

onSearch(filteredData);
};

return (
<div>
<input type="text" placeholder="Search..." value={filterText} onChange={handleFilterChange} />
</div>
);
};

export default SimpleSearch;
19 changes: 18 additions & 1 deletion frontend/src/app/content/pages/ManageProvisionsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
removeProvision,
updateProvision,
} from '../../common/manage-provisions';
import SimpleSearch from '../../components/common/SimpleSearch';

interface ManageProvisionsPageProps {}

Expand All @@ -25,6 +26,7 @@ const ManageProvisionsPage: FC<ManageProvisionsPageProps> = () => {
const [showAddProvisionModal, setShowAddProvisionModal] = useState<boolean>(false);
const [showRemoveProvisionModal, setShowRemoveProvisionModal] = useState<boolean>(false);
const [refreshTrigger, setRefreshTrigger] = useState(0);
const [filteredProvisions, setFilteredProvisions] = useState<Provision[]>([]);

useEffect(() => {
const getData = async () => {
Expand All @@ -42,6 +44,12 @@ const ManageProvisionsPage: FC<ManageProvisionsPageProps> = () => {
}
}, [currentProvision, data.allProvisions]);

useEffect(() => {
if (data.allProvisions) {
setFilteredProvisions(data.allProvisions);
}
}, [data.allProvisions]);

const refreshTables = () => {
setRefreshTrigger((prev) => prev + 1);
};
Expand All @@ -68,14 +76,23 @@ const ManageProvisionsPage: FC<ManageProvisionsPageProps> = () => {
refreshTables();
};

const handleSearch = (filteredProvisions: Provision[]) => {
setFilteredProvisions(filteredProvisions);
};

const currentVariables = data.allVariables?.filter((v) => v.provision_id === currentProvision?.id) || [];

return (
<>
<h1>Manage Provisions</h1>
<hr />
<SimpleSearch
searchKeys={['provision_name', 'category']}
data={data.allProvisions ? data.allProvisions : []}
onSearch={handleSearch}
/>
<ManageProvisionsTable
provisions={data.allProvisions}
provisions={filteredProvisions}
variables={data.allVariables}
editProvisionHandler={openEditProvisionModal}
removeProvisionHandler={openRemoveProvisionModal}
Expand Down

0 comments on commit ca33ba3

Please sign in to comment.