From 8cb999a4577c191400325443f347f218eb465f1e Mon Sep 17 00:00:00 2001 From: Alex-zReeZ Date: Tue, 9 Jul 2024 10:23:40 +0200 Subject: [PATCH] refactor: removed window.location.reload and added dynamically new esp in sidebar --- .../src/app/ui/dashboard/EspLinksElement.tsx | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/nextjs-interface/src/app/ui/dashboard/EspLinksElement.tsx b/nextjs-interface/src/app/ui/dashboard/EspLinksElement.tsx index 4deacc7..7af9dcb 100644 --- a/nextjs-interface/src/app/ui/dashboard/EspLinksElement.tsx +++ b/nextjs-interface/src/app/ui/dashboard/EspLinksElement.tsx @@ -10,7 +10,7 @@ import clsx from "clsx"; import { Label } from "recharts"; // import context and scripts -import { getToken } from "@/lib/context"; +import { esp, getToken } from "@/lib/context"; import { useAllEsp } from "@/lib/data"; // import lucide icons @@ -30,6 +30,7 @@ export default function EspLinksElement() { const links = useAllEsp(); const [newLink, setNewLink] = useState({ name: "", ip: "" }); + const [newLinkName, setNewLinkName] = useState([]); const handleInputChange = (e: any) => { setNewLink({ ...newLink, [e.target.id]: e.target.value }); @@ -44,6 +45,7 @@ export default function EspLinksElement() { method: "POST", headers: { "Content-Type": "application/json", + Prefer: "return=representation", Authorization: `Bearer ${getToken()}`, }, body: JSON.stringify(newLink), @@ -53,8 +55,10 @@ export default function EspLinksElement() { Error("Erreur lors de l'envoi des données à l'API"); } + const responseData = await response.json(); + + setNewLinkName(responseData); setNewLink({ name: "", ip: "" }); - window.location.reload(); } catch (e) { console.error("Une erreur s'est produite :", e); } @@ -82,6 +86,26 @@ export default function EspLinksElement() { ); })} + {newLinkName.map((link) => { + const href = `/dashboard/esp/${link.id}`; + return ( + <> + {" "} + +

{link.name}

+ + + ); + })}
@@ -90,9 +114,9 @@ export default function EspLinksElement() { - +
-
+
- +