Refactor main host setup
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { useState } from "react";
|
||||
import { Trash2 } from "lucide-react";
|
||||
import Button from "@/src/components/twui/layout/Button";
|
||||
import fetchApi from "@/src/components/twui/utils/fetch/fetchApi";
|
||||
import type { ApiReqParams } from "@/src/types";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
|
||||
export default function DeleteHostButton({ host_id }: { host_id: number }) {
|
||||
const [deleting, setDeleting] = useState(false);
|
||||
|
||||
async function handleDelete() {
|
||||
if (
|
||||
!window.confirm(
|
||||
`Delete host #${host_id}? All clients and their configurations will be removed.`,
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
setDeleting(true);
|
||||
|
||||
const res = await fetchApi<ApiReqParams, APIResponseObject>(
|
||||
`/api/admin/delete-host`,
|
||||
{
|
||||
method: "DELETE",
|
||||
body: { host_id },
|
||||
},
|
||||
);
|
||||
|
||||
if (!res.success) {
|
||||
setDeleting(false);
|
||||
window.alert(res.msg || "Could not delete the host");
|
||||
return;
|
||||
}
|
||||
|
||||
window.location.pathname = "/admin/hosts";
|
||||
}
|
||||
|
||||
return (
|
||||
<Button
|
||||
title="Delete Host"
|
||||
size="small"
|
||||
variant="ghost"
|
||||
color="error"
|
||||
beforeIcon={<Trash2 size={14} />}
|
||||
disabled={deleting}
|
||||
onClick={handleDelete}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user