import { useEffect, useState } from "react"; import { Server } from "lucide-react"; import Modal from "../ui/Modal"; import Field from "../ui/Field"; import Button from "../ui/Button"; import { emptyServer } from "../../lib/constants"; export default function ServerFormModal({ open, onClose, initial, onSave }) { const [form, setForm] = useState(emptyServer); const [saving, setSaving] = useState(false); const [error, setError] = useState(""); const isEditing = Boolean(initial?.id); useEffect(() => { if (open) { setForm( initial ? { name: initial.name || "", ipAddress: initial.ipAddress || "", description: initial.description || "", proxyPort: initial.proxyPort ?? "", proxyPath: initial.proxyPath || "", } : emptyServer, ); setError(""); } }, [open, initial]); async function submit(event) { event.preventDefault(); setSaving(true); setError(""); try { await onSave({ ...form, proxyPort: form.proxyPort === "" ? null : Number(form.proxyPort), proxyPath: form.proxyPath.trim() === "" ? null : form.proxyPath.trim(), }); onClose(); } catch (err) { setError(err.message); } finally { setSaving(false); } } return ( } >
setForm({ ...form, name: event.target.value })} placeholder="e.g. Billing Server" /> setForm({ ...form, ipAddress: event.target.value })} placeholder="192.168.1.50" />