"use server"; import { revalidatePath } from "next/cache"; import { api, unwrap } from "@/lib/api"; import { attempt, field, type ActionState } from "@/lib/actions"; const roles = ["user", "moderator", "admin"] as const; /** Runs a change to a person from the console, then shows their page again. */ async function onPerson(form: FormData, change: (id: string) => Promise): Promise { const id = field(form, "id"); return attempt(async () => { const notice = await change(id); revalidatePath(`/console/people/${id}`); return notice; }); } export async function setLinkCap(_: ActionState, form: FormData): Promise { return onPerson(form, async (id) => { const linkCap = Number(field(form, "linkCap")); const { person } = unwrap(await api.console.people({ id })["link-cap"].post({ linkCap })); return `Link cap set to ${person.linkCap}.`; }); } export async function ban(_: ActionState, form: FormData): Promise { return onPerson(form, async (id) => { unwrap(await api.console.people({ id }).ban.post({ reason: field(form, "reason") })); }); } export async function unban(_: ActionState, form: FormData): Promise { return onPerson(form, async (id) => { unwrap(await api.console.people({ id }).unban.post()); }); } export async function setRole(_: ActionState, form: FormData): Promise { return onPerson(form, async (id) => { const role = roles.find((r) => r === field(form, "role")) ?? "user"; const { person } = unwrap(await api.console.people({ id }).role.post({ role })); return `Role set to ${person.role}.`; }); } export async function decideCatalog(_: ActionState, form: FormData): Promise { const id = field(form, "id"); const decision = field(form, "decision") === "approve" ? "approve" : "reject"; return attempt(async () => { unwrap(await api.console.catalogs({ id })({ decision }).post()); revalidatePath("/console/catalogs"); revalidatePath("/catalogs"); }); }