Refactor add new host form
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user