import React, { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { apiFetch } from "../api"; import { useAuth } from "../state"; const emptyForm = { name: "", host: "", port: 5432, dbname: "", username: "", password: "", sslmode: "prefer", tags: {}, }; export function TargetsPage() { const { tokens, refresh, me } = useAuth(); const [targets, setTargets] = useState([]); const [form, setForm] = useState(emptyForm); const [error, setError] = useState(""); const [loading, setLoading] = useState(true); const canManage = me?.role === "admin" || me?.role === "operator"; const load = async () => { setLoading(true); try { setTargets(await apiFetch("/targets", {}, tokens, refresh)); setError(""); } catch (e) { setError(String(e.message || e)); } finally { setLoading(false); } }; useEffect(() => { load(); }, []); const createTarget = async (e) => { e.preventDefault(); try { await apiFetch("/targets", { method: "POST", body: JSON.stringify(form) }, tokens, refresh); setForm(emptyForm); await load(); } catch (e) { setError(String(e.message || e)); } }; const deleteTarget = async (id) => { if (!confirm("Target löschen?")) return; try { await apiFetch(`/targets/${id}`, { method: "DELETE" }, tokens, refresh); await load(); } catch (e) { setError(String(e.message || e)); } }; return (
Lade Targets...
) : (| Name | Host | DB | Aktionen |
|---|---|---|---|
| {t.name} | {t.host}:{t.port} | {t.dbname} | Details{" "} {canManage && } |