CoreControl/components/dialogues/DeleteNetwork.tsx

77 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-05-18 17:07:35 +02:00
"use client";
import useNetworks from "@/hooks/useNetworks";
2025-05-18 20:15:23 +02:00
import { Trash2, AlertTriangle } from "lucide-react";
2025-05-19 12:56:46 +02:00
import { useState } from "react";
import ErrorToast from "@/components/Error";
import SuccessToast from "@/components/Success";
2025-05-18 17:07:35 +02:00
interface DeleteNetworkProps {
networkId: string;
2025-05-19 00:23:43 +02:00
onNetworkDeleted?: () => void;
2025-05-18 17:07:35 +02:00
}
2025-05-19 00:23:43 +02:00
export default function DeleteNetwork({ networkId, onNetworkDeleted }: DeleteNetworkProps) {
2025-05-19 12:56:46 +02:00
const [error, setError] = useState("");
const [success, setSuccess] = useState("");
2025-05-18 17:07:35 +02:00
const { deleteNetwork } = useNetworks();
2025-05-18 20:15:23 +02:00
2025-05-20 00:06:32 +02:00
const handleDelete = async () => {
2025-05-20 20:06:32 +02:00
const response = deleteNetwork(networkId);
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 (onNetworkDeleted && successMessage) {
onNetworkDeleted()
setSuccess(successMessage)
2025-05-20 00:06:32 +02:00
}
2025-05-20 20:06:32 +02:00
} catch (apiError: any) {
setError(apiError)
2025-05-19 12:56:46 +02:00
}
2025-05-18 20:15:23 +02:00
};
2025-05-18 17:07:35 +02:00
return (
<div>
<dialog id="delete_network" className="modal">
2025-05-18 20:15:23 +02:00
<div className="modal-box w-11/12 max-w-md border-l-4 border-error">
<div className="flex items-center gap-3 mb-3">
<div className="bg-error text-error-content rounded-full p-2 flex items-center justify-center">
<Trash2 className="h-6 w-6" />
</div>
<h3 className="font-bold text-xl">Delete Network</h3>
</div>
<div className="bg-base-200 p-4 rounded-lg mb-4">
<p className="text-sm">
<span className="font-medium">Are you sure you want to delete this network?</span>
</p>
<p className="text-sm mt-2">
This will also delete all servers & applications associated with it.
</p>
<div className="mt-2 flex items-center gap-2 text-error">
<AlertTriangle className="h-5 w-5" />
<span className="font-bold">This action cannot be undone.</span>
</div>
</div>
2025-05-18 17:07:35 +02:00
<div className="modal-action">
2025-05-18 20:15:23 +02:00
<form method="dialog" className="flex gap-2">
<button className="btn btn-outline">Cancel</button>
<button
className="btn btn-error text-error-content"
onClick={handleDelete}
>
Delete Network
</button>
2025-05-18 17:07:35 +02:00
</form>
</div>
</div>
</dialog>
2025-05-19 12:56:46 +02:00
<ErrorToast message={error} show={error !== ''} onClose={() => setError('')} />
<SuccessToast message={success} show={success !== ''} onClose={() => setSuccess('')} />
2025-05-18 17:07:35 +02:00
</div>
);
}