api-server gets /create-acc-data and /create-user-data POST routes that INSERT into the respective tables with required-field validation. Frontend adds an 'Add' button next to Refresh in each table head; opens a native <dialog> form with all fields. Inputs use 16px font on phone (sm:text-[13px] desktop) so iOS doesn't auto-zoom. A small form-dialog-shell helper centralizes the modal chrome, field label, and input class so create-account-dialog and create-user-dialog stay focused on their fields and validation.
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { fetchApi } from "@/lib/api";
|
|
import type { AccUpdate, UserUpdate } from "@/lib/types";
|
|
|
|
export type ActionResult = { ok: true } | { ok: false; error: string };
|
|
|
|
export async function updateAccount(data: AccUpdate): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/update-acc-data", { method: "POST", body: data });
|
|
revalidatePath("/");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
export async function updateUser(data: UserUpdate): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/update-user-data", { method: "POST", body: data });
|
|
revalidatePath("/users");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
export async function createAccount(data: AccUpdate): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/create-acc-data", { method: "POST", body: data });
|
|
revalidatePath("/");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
export async function createUser(data: UserUpdate): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/create-user-data", { method: "POST", body: data });
|
|
revalidatePath("/users");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
export async function deleteAccount(username: string): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/delete-acc-data", { method: "POST", body: { username } });
|
|
revalidatePath("/");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|
|
|
|
export async function deleteUser(f_username: string): Promise<ActionResult> {
|
|
try {
|
|
await fetchApi("/delete-user-data", { method: "POST", body: { f_username } });
|
|
revalidatePath("/users");
|
|
return { ok: true };
|
|
} catch (err) {
|
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
}
|
|
}
|