From 8589ccb35fe7757404eb9fd7f12b251298bd4382 Mon Sep 17 00:00:00 2001 From: headlessdev Date: Mon, 14 Apr 2025 14:50:48 +0200 Subject: [PATCH] Server Search Function --- app/dashboard/applications/Applications.tsx | 3 +- app/dashboard/servers/Servers.tsx | 38 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/app/dashboard/applications/Applications.tsx b/app/dashboard/applications/Applications.tsx index 62c1fb9..cd67711 100644 --- a/app/dashboard/applications/Applications.tsx +++ b/app/dashboard/applications/Applications.tsx @@ -213,7 +213,7 @@ export default function Dashboard() { console.log(error.response.data); } }; - + const searchApplications = async () => { try { setIsSearching(true); @@ -391,7 +391,6 @@ export default function Dashboard() { onChange={(e) => setSearchTerm(e.target.value)} /> -
{!loading ? (
(""); const [editDisk, setEditDisk] = useState(""); + const [searchTerm, setSearchTerm] = useState(""); + const [isSearching, setIsSearching] = useState(false); + useEffect(() => { const savedLayout = Cookies.get('layoutPreference-servers'); setIsGridLayout(savedLayout === 'grid'); @@ -210,6 +213,33 @@ export default function Dashboard() { } } + const searchServers = async () => { + try { + setIsSearching(true); + const response = await axios.post<{ results: Server[] }>( + "/api/servers/search", + { searchterm: searchTerm } + ); + setServers(response.data.results); + setIsSearching(false); + } catch (error: any) { + console.error("Search error:", error.response?.data); + setIsSearching(false); + } + }; + + useEffect(() => { + const delayDebounce = setTimeout(() => { + if (searchTerm.trim() === "") { + getServers(); + } else { + searchServers(); + } + }, 300); + + return () => clearTimeout(delayDebounce); + }, [searchTerm]); + return ( @@ -345,6 +375,14 @@ export default function Dashboard() {
+
+ setSearchTerm(e.target.value)} + /> +

{!loading ?