Files
wireguard-ui/src/pages/admin/hosts/add/(functions)/submit-add-host-form.ts
T
2026-09-20 11:59:50 +01:00

44 lines
1.3 KiB
TypeScript

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);
}
}