Refactor add new host form

This commit is contained in:
2026-09-20 11:59:50 +01:00
parent 25b64281f8
commit 957f2de13c
8 changed files with 212 additions and 135 deletions
@@ -0,0 +1,44 @@
import type { BUN_SQLITE_WGUI_HOSTS } from "@/db/types/db";
import fetchApi from "@/src/components/twui/utils/fetch/fetchApi";
import type useFormInit from "@/src/hooks/use-form-init";
import type { ApiReqParams } from "@/src/types";
import type { APIResponseObject } from "@moduletrace/bunext/types";
export default async function submitAddHostForm({
setLoading,
setStatus,
form,
}: ReturnType<typeof useFormInit<BUN_SQLITE_WGUI_HOSTS>>) {
try {
if (!window.confirm("Create this new host?")) {
return;
}
setLoading(true);
const res = await fetchApi<ApiReqParams, APIResponseObject>(
`/api/admin/add-host`,
{
method: "POST",
body: {
wg_ip_address: form.wg_ip_address || "",
},
},
);
if (res.success && res.numberRes) {
window.location.pathname = `/admin/hosts/${res.numberRes}`;
return;
}
throw new Error(res.msg || "Could not create the host");
} catch (error: any) {
setStatus({
error: true,
success: false,
msg: error.message || "Could not create the host",
});
setLoading(false);
}
}