diff --git a/src/components/sections/KeysDataSection.tsx b/src/components/sections/KeysDataSection.tsx index 4e4df8d..21044db 100644 --- a/src/components/sections/KeysDataSection.tsx +++ b/src/components/sections/KeysDataSection.tsx @@ -4,6 +4,7 @@ import React, { useEffect, useState } from 'react' import { useNavigate, useParams } from 'react-router-dom' import classes from '../../App.module.css' import useCustomAlert from '../../hooks/useCustomAlert' +import useSearchFilter from '../../hooks/useSearchFilter' import i18n from '../../locales' import ErrorNotice from '../error/ErrorNotice' import PanelHeader from '../header/PanelHeader' @@ -39,6 +40,10 @@ const KeysDataSection = ({ query }) => { } ) + const { searchTerm, setSearchTerm, filteredData } = useSearchFilter( + data?.results + ) + const numberOfKeysInNamespace = data?.results?.length const handleCreate = async ({ key }) => { @@ -144,12 +149,16 @@ const KeysDataSection = ({ query }) => { />
- +
- {data && ( + {filteredData && ( { const { error, loading, data, refetch } = useDataQuery(query) + const { searchTerm, setSearchTerm, filteredData } = useSearchFilter( + data?.results + ) + const handleCreate = async (values) => { await engine.mutate( { @@ -114,7 +119,11 @@ const NamespaceDataSection = ({ query }) => { return ( <>
- + setOpenCreateModal(true)} @@ -122,9 +131,9 @@ const NamespaceDataSection = ({ query }) => { />
- {data && ( + {filteredData && ( { +interface SearchFieldProps { + placeholder?: string + searchTerm: string + setSearchTerm: React.Dispatch> +} + +const SearchField = ({ + placeholder, + searchTerm, + setSearchTerm, +}: SearchFieldProps) => { return (
setSearchTerm(e.value)} />
) diff --git a/src/components/table/ItemsTable.tsx b/src/components/table/ItemsTable.tsx index a1919d2..de0e12a 100644 --- a/src/components/table/ItemsTable.tsx +++ b/src/components/table/ItemsTable.tsx @@ -13,21 +13,19 @@ import i18n from '../../locales' import DeleteAction from './DeleteAction' import SharingAction from './SharingAction' -interface TableProps { - data: { - results: string[] - } +interface ItemsTableProps { + tableData: string[] label: string setOpenDeleteModal: React.Dispatch> setSelectedItem: React.Dispatch> } const ItemsTable = ({ - data, + tableData, label, setOpenDeleteModal, setSelectedItem, -}: TableProps) => { +}: ItemsTableProps) => { const navigate = useNavigate() const { namespace: currentNamespace, key } = useParams() @@ -45,7 +43,7 @@ const ItemsTable = ({ return (
- {data && ( + {tableData && ( @@ -66,9 +64,9 @@ const ItemsTable = ({ - {data?.results?.length && ( + {tableData?.length ? ( <> - {data.results.map((item, index) => { + {tableData.map((item, index) => { return ( + ) : ( + + + {i18n.t('No items found')} + + )} diff --git a/src/hooks/useSearchFilter.tsx b/src/hooks/useSearchFilter.tsx new file mode 100644 index 0000000..7479472 --- /dev/null +++ b/src/hooks/useSearchFilter.tsx @@ -0,0 +1,28 @@ +import { useEffect, useState } from 'react' + +const useSearchFilter = (itemsArray = []) => { + const [searchTerm, setSearchTerm] = useState(null) + const [filteredData, setFilteredData] = useState(null) + + useEffect(() => { + if (!itemsArray.length) { + return + } + if (searchTerm) { + const filteredItemsArray = itemsArray.filter((item) => + item.toLowerCase().includes(searchTerm.toLowerCase()) + ) + setFilteredData(filteredItemsArray) + } else { + setFilteredData(itemsArray) + } + }, [searchTerm, itemsArray]) + + return { + searchTerm, + setSearchTerm, + filteredData, + } +} + +export default useSearchFilter