diff --git a/nextjs-interface/src/app/ui/dashboard/EspLinksElement.tsx b/nextjs-interface/src/app/ui/dashboard/EspLinksElement.tsx index 4deacc7..2711247 100644 --- a/nextjs-interface/src/app/ui/dashboard/EspLinksElement.tsx +++ b/nextjs-interface/src/app/ui/dashboard/EspLinksElement.tsx @@ -1,7 +1,7 @@ "use client"; // import nextjs and react -import { useState } from "react"; +import { useEffect, useState } from "react"; import Link from "next/link"; import { usePathname } from "next/navigation"; import clsx from "clsx"; @@ -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 @@ -29,13 +29,18 @@ export default function EspLinksElement() { const pathname = usePathname(); const links = useAllEsp(); + const [allLinks, setAllLinks] = useState(links); const [newLink, setNewLink] = useState({ name: "", ip: "" }); - const handleInputChange = (e: any) => { + useEffect(() => { + setAllLinks(links); + }, [links]); + + const handleInputChange = (e: React.ChangeEvent) => { setNewLink({ ...newLink, [e.target.id]: e.target.value }); }; - const handleSubmit = async (e: { preventDefault: () => void }) => { + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const url = `/postgrest/esp`; @@ -44,6 +49,7 @@ export default function EspLinksElement() { method: "POST", headers: { "Content-Type": "application/json", + Prefer: "return=representation", Authorization: `Bearer ${getToken()}`, }, body: JSON.stringify(newLink), @@ -53,16 +59,23 @@ export default function EspLinksElement() { Error("Erreur lors de l'envoi des données à l'API"); } + const responseData = await response.json(); + const newLinks = Array.isArray(responseData) + ? responseData.flat() + : [responseData]; + + setAllLinks([...allLinks, ...newLinks]); setNewLink({ name: "", ip: "" }); - window.location.reload(); } catch (e) { console.error("Une erreur s'est produite :", e); } }; + console.log(allLinks); + return ( <> - {links.map((link) => { + {allLinks.map((link) => { const href = `/dashboard/esp/${link.id}`; return ( <> @@ -90,9 +103,9 @@ export default function EspLinksElement() { - +
-
+
- +