From f7063d03f1588085411c4b0b6bcb6dddda009d12 Mon Sep 17 00:00:00 2001 From: AkashJana18 Date: Thu, 17 Oct 2024 00:40:44 +0530 Subject: [PATCH] added search funtionality --- frontend/src/settings/APIKeys.tsx | 45 ++++++++++++++++++++++------- frontend/src/settings/Documents.tsx | 26 +++++++++++++---- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/frontend/src/settings/APIKeys.tsx b/frontend/src/settings/APIKeys.tsx index aa4b73d4..28d414bb 100644 --- a/frontend/src/settings/APIKeys.tsx +++ b/frontend/src/settings/APIKeys.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import userService from '../api/services/userService'; @@ -10,10 +10,12 @@ import Input from '../components/Input'; export default function APIKeys() { const { t } = useTranslation(); - const [isCreateModalOpen, setCreateModal] = React.useState(false); - const [isSaveKeyModalOpen, setSaveKeyModal] = React.useState(false); - const [newKey, setNewKey] = React.useState(''); - const [apiKeys, setApiKeys] = React.useState([]); + const [isCreateModalOpen, setCreateModal] = useState(false); + const [isSaveKeyModalOpen, setSaveKeyModal] = useState(false); + const [newKey, setNewKey] = useState(''); + const [apiKeys, setApiKeys] = useState([]); + const [searchTerm, setSearchTerm] = useState(''); // Added state for search term + const [filteredKeys, setFilteredKeys] = useState([]); // State for filtered API keys const handleFetchKeys = async () => { try { @@ -23,6 +25,7 @@ export default function APIKeys() { } const apiKeys = await response.json(); setApiKeys(apiKeys); + setFilteredKeys(apiKeys); // Initialize the filtered keys as the fetched ones } catch (error) { console.log(error); } @@ -38,8 +41,12 @@ export default function APIKeys() { return response.json(); }) .then((data) => { - data.status === 'ok' && + if (data.status === 'ok') { setApiKeys((previous) => previous.filter((elem) => elem.id !== id)); + setFilteredKeys((previous) => + previous.filter((elem) => elem.id !== id), + ); + } }) .catch((error) => { console.error(error); @@ -63,6 +70,7 @@ export default function APIKeys() { }) .then((data) => { setApiKeys([...apiKeys, data]); + setFilteredKeys([...apiKeys, data]); // Update filtered keys too setCreateModal(false); setNewKey(data.key); setSaveKeyModal(true); @@ -73,9 +81,22 @@ export default function APIKeys() { }); }; - React.useEffect(() => { + useEffect(() => { handleFetchKeys(); }, []); + + // Filter API keys when the search term changes + useEffect(() => { + setFilteredKeys( + apiKeys.filter( + (key) => + key.name.toLowerCase().includes(searchTerm.toLowerCase()) || + key.source?.toLowerCase().includes(searchTerm.toLowerCase()) || + key.key.toLowerCase().includes(searchTerm.toLowerCase()), + ), + ); + }, [searchTerm, apiKeys]); + return (
@@ -84,9 +105,11 @@ export default function APIKeys() { setSearchTerm(e.target.value)} // Update search term />
@@ -81,15 +94,15 @@ const Documents: React.FC = ({ - {!documents?.length && ( + {!filteredDocuments?.length && ( {t('settings.documents.noData')} )} - {documents && - documents.map((document, index) => ( + {filteredDocuments && + filteredDocuments.map((document, index) => ( {document.name} {document.date} @@ -105,8 +118,7 @@ const Documents: React.FC = ({ Delete { event.stopPropagation(); @@ -138,8 +150,10 @@ const Documents: React.FC = ({ ); }; + Documents.propTypes = { documents: PropTypes.array.isRequired, handleDeleteDocument: PropTypes.func.isRequired, }; + export default Documents;