CoreControl/components/dialogues/EditNetwork.tsx

154 lines
7.1 KiB
TypeScript
Raw Normal View History

2025-05-18 16:59:16 +02:00
"use client";
import { useState } from "react";
import ErrorToast from "@/components/Error";
import SuccessToast from "@/components/Success";
import useNetworks from "@/hooks/useNetworks";
import { Network } from "@/app/types";
2025-05-18 20:15:23 +02:00
import { PencilLine, Network as NetworkIcon, Globe, Wifi } from "lucide-react";
2025-05-18 16:59:16 +02:00
interface EditNetworkProps {
2025-05-19 00:23:43 +02:00
onNetworkEdited?: () => void;
2025-05-18 16:59:16 +02:00
siteId: string;
network: Network;
}
2025-05-19 00:23:43 +02:00
export default function EditNetwork({ onNetworkEdited, siteId, network }: EditNetworkProps) {
2025-05-18 16:59:16 +02:00
const [editName, setEditName] = useState(network.name);
const [editIpv4Subnet, setEditIpv4Subnet] = useState(network.ipv4Subnet);
const [editIpv6Subnet, setEditIpv6Subnet] = useState(network.ipv6Subnet);
const [editGateway, setEditGateway] = useState(network.gateway);
const [error, setError] = useState("");
const [success, setSuccess] = useState("");
const { editNetwork } = useNetworks();
2025-05-20 00:06:32 +02:00
const handleEdit = async () => {
2025-05-20 20:06:32 +02:00
const response = editNetwork({
id: network.id,
siteId,
name: editName,
ipv4Subnet: editIpv4Subnet,
ipv6Subnet: editIpv6Subnet,
gateway: editGateway
});
if (typeof response === "string") {
setError(response)
return
}
2025-05-20 00:06:32 +02:00
2025-05-20 20:06:32 +02:00
try{
const successMessage = await response
if (onNetworkEdited && successMessage) {
onNetworkEdited()
setSuccess(successMessage)
2025-05-20 00:06:32 +02:00
}
2025-05-20 20:06:32 +02:00
} catch (apiError: any) {
setError(apiError)
} finally {
setEditName("")
setEditIpv4Subnet("")
setEditIpv6Subnet("")
setEditGateway("")
2025-05-19 12:56:46 +02:00
}
2025-05-18 20:15:23 +02:00
};
2025-05-18 16:59:16 +02:00
return (
<div>
<dialog id="edit_network" className="modal">
2025-05-18 20:15:23 +02:00
<div className="modal-box w-11/12 max-w-3xl border-l-4 border-primary">
<div className="flex items-center gap-3 mb-3">
<div className="bg-primary text-primary-content rounded-full p-2 flex items-center justify-center">
<PencilLine className="h-6 w-6" />
</div>
<h3 className="font-bold text-xl">Edit Network</h3>
</div>
2025-05-18 16:59:16 +02:00
2025-05-18 20:15:23 +02:00
<div className="bg-base-200 p-4 rounded-lg mb-4">
2025-05-18 16:59:16 +02:00
<div className="space-y-4">
<div className="form-control">
<label className="label">
2025-05-18 20:15:23 +02:00
<div className="flex items-center gap-2">
<NetworkIcon className="h-4 w-4 opacity-70" />
<span className="label-text font-medium">Network Name</span>
</div>
2025-05-18 16:59:16 +02:00
</label>
<input
type="text"
2025-05-18 20:15:23 +02:00
className="input input-bordered w-full"
2025-05-18 16:59:16 +02:00
placeholder="e.g. VLAN 10"
value={editName}
onChange={(e) => setEditName(e.target.value)}
/>
</div>
<div className="form-control">
<label className="label">
2025-05-18 20:15:23 +02:00
<div className="flex items-center gap-2">
<Globe className="h-4 w-4 opacity-70" />
<span className="label-text font-medium">IPv4 Subnet</span>
<span className="ml-1 text-xs opacity-60">(optional)</span>
</div>
2025-05-18 16:59:16 +02:00
</label>
<input
type="text"
2025-05-18 20:15:23 +02:00
className="input input-bordered w-full"
2025-05-18 16:59:16 +02:00
placeholder="Enter the IPv4 subnet"
value={editIpv4Subnet}
onChange={(e) => setEditIpv4Subnet(e.target.value)}
/>
</div>
2025-05-18 20:15:23 +02:00
2025-05-18 16:59:16 +02:00
<div className="form-control">
<label className="label">
2025-05-18 20:15:23 +02:00
<div className="flex items-center gap-2">
<Globe className="h-4 w-4 opacity-70" />
<span className="label-text font-medium">IPv6 Subnet</span>
<span className="ml-1 text-xs opacity-60">(optional)</span>
</div>
2025-05-18 16:59:16 +02:00
</label>
<input
type="text"
2025-05-18 20:15:23 +02:00
className="input input-bordered w-full"
2025-05-18 16:59:16 +02:00
placeholder="Enter the IPv6 subnet"
value={editIpv6Subnet}
onChange={(e) => setEditIpv6Subnet(e.target.value)}
/>
</div>
2025-05-18 20:15:23 +02:00
2025-05-18 16:59:16 +02:00
<div className="form-control">
<label className="label">
2025-05-18 20:15:23 +02:00
<div className="flex items-center gap-2">
<Wifi className="h-4 w-4 opacity-70" />
<span className="label-text font-medium">Gateway</span>
<span className="ml-1 text-xs opacity-60">(optional)</span>
</div>
2025-05-18 16:59:16 +02:00
</label>
<input
type="text"
2025-05-18 20:15:23 +02:00
className="input input-bordered w-full"
2025-05-18 16:59:16 +02:00
placeholder="Enter the gateway"
2025-05-18 20:15:23 +02:00
value={editGateway}
2025-05-18 16:59:16 +02:00
onChange={(e) => setEditGateway(e.target.value)}
/>
</div>
</div>
2025-05-18 20:15:23 +02:00
</div>
2025-05-18 16:59:16 +02:00
2025-05-18 20:15:23 +02:00
<div className="modal-action">
<form method="dialog" className="flex gap-3 w-full justify-end">
<button className="btn btn-outline">Cancel</button>
<button
className="btn btn-primary"
onClick={handleEdit}
>
Save Changes
</button>
</form>
2025-05-18 16:59:16 +02:00
</div>
</div>
</dialog>
<ErrorToast message={error} show={error !== ''} onClose={() => setError('')} />
<SuccessToast message={success} show={success !== ''} onClose={() => setSuccess('')} />
</div>
2025-05-18 20:15:23 +02:00
);
2025-05-18 16:59:16 +02:00
}