update user backend + frontend server function

This commit is contained in:
Oliver Bryan
2025-12-31 19:09:24 +00:00
parent 435540f158
commit c274ea9036
7 changed files with 99 additions and 11 deletions

View File

@@ -0,0 +1,39 @@
import { getAuthHeaders, getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
export async function update({
id,
name,
password,
serverURL,
onSuccess,
onError,
}: {
id: number;
name: string;
password: string;
serverURL: string;
} & ServerQueryInput) {
const url = new URL(`${getServerURL()}/user/update`);
url.searchParams.set("id", `${id}`);
url.searchParams.set("name", name.trim());
url.searchParams.set("password", password.trim());
url.searchParams.set("serverURL", serverURL.trim());
const res = await fetch(url.toString(), {
headers: getAuthHeaders(),
});
if (!res.ok) {
const error = await res.text();
onError?.(error || `failed to update user (${res.status})`);
} else {
const data = await res.json();
if (!data.id) {
onError?.(`failed to update user (${res.status})`);
return;
}
onSuccess?.(data, res);
}
}