156 lines
4.6 KiB
TypeScript
156 lines
4.6 KiB
TypeScript
import Link from "next/link";
|
|
import { notFound } from "next/navigation";
|
|
import { updateUser } from "@/actions/admin-user-edit";
|
|
import { requireStaff } from "@/lib/admin/guard";
|
|
import { User } from "lucide-react";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { getTranslations } from "next-intl/server";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
// users_currency.type values for the non-credits currencies (see send-currency.ts).
|
|
const DUCKETS_TYPE = 0;
|
|
const DIAMONDS_TYPE = 5;
|
|
|
|
export default async function AdminUserEdit({
|
|
params,
|
|
}: {
|
|
params: Promise<{ id: string }>;
|
|
}) {
|
|
// Gate the page with the SAME helper the action re-checks.
|
|
await requireStaff();
|
|
|
|
const { id } = await params;
|
|
const t = await getTranslations("pages.admin.users.editForm");
|
|
const userId = Number(id);
|
|
if (!Number.isInteger(userId) || userId <= 0) notFound();
|
|
|
|
let user: {
|
|
id: number;
|
|
username: string;
|
|
mail: string | null;
|
|
motto: string;
|
|
look: string;
|
|
rank: number;
|
|
credits: number;
|
|
pixels: number;
|
|
points: number;
|
|
} | null = null;
|
|
let duckets = 0;
|
|
let diamonds = 0;
|
|
|
|
try {
|
|
user = await prisma.user.findUnique({
|
|
where: { id: userId },
|
|
select: {
|
|
id: true,
|
|
username: true,
|
|
mail: true,
|
|
motto: true,
|
|
look: true,
|
|
rank: true,
|
|
credits: true,
|
|
pixels: true,
|
|
points: true,
|
|
},
|
|
});
|
|
if (user) {
|
|
const currencies = await prisma.usersCurrency.findMany({
|
|
where: { userId, type: { in: [DUCKETS_TYPE, DIAMONDS_TYPE] } },
|
|
select: { type: true, amount: true },
|
|
});
|
|
for (const c of currencies) {
|
|
if (c.type === DUCKETS_TYPE) duckets = c.amount;
|
|
else if (c.type === DIAMONDS_TYPE) diamonds = c.amount;
|
|
}
|
|
}
|
|
} catch {
|
|
return (
|
|
<main>
|
|
<p className="text-sm text-gray-500 dark:text-gray-400">
|
|
<Link href="/admin/users">← {t("title")}</Link>
|
|
</p>
|
|
<h1>{t("title")}</h1>
|
|
<p className="text-xs text-gray-500 dark:text-gray-400">{t("loadError")}</p>
|
|
</main>
|
|
);
|
|
}
|
|
|
|
if (!user) notFound();
|
|
|
|
return (
|
|
<main>
|
|
<p className="text-sm text-gray-500 dark:text-gray-400 mb-2">
|
|
<Link href={`/admin/users/${user.id}`}>← {user.username}</Link>
|
|
</p>
|
|
<div className="flex items-center gap-3 mb-6">
|
|
<div className="w-10 h-10 rounded-xl bg-gradient-to-br from-[var(--color-primary)]/20 to-[var(--color-primary)]/5 grid place-items-center">
|
|
<User size={20} className="text-[var(--color-primary)]" />
|
|
</div>
|
|
<div>
|
|
<h1 className="m-0 text-xl font-extrabold text-[var(--color-text)]">{t("title", { username: user.username })}</h1>
|
|
<p className="m-0 text-xs text-[var(--color-text-muted)] mt-0.5">{t("idLabel", { id: user.id })}</p>
|
|
</div>
|
|
</div>
|
|
|
|
<form action={updateUser} className="admin-card">
|
|
<input type="hidden" name="id" value={user.id} />
|
|
|
|
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
|
|
<label>
|
|
{t("email")}
|
|
<input name="mail" type="email" defaultValue={user.mail ?? ""} maxLength={500} />
|
|
</label>
|
|
|
|
<label>
|
|
{t("rank")}
|
|
<input name="rank" type="number" min={1} defaultValue={user.rank} />
|
|
</label>
|
|
|
|
<label>
|
|
{t("motto")}
|
|
<input name="motto" defaultValue={user.motto} maxLength={127} />
|
|
</label>
|
|
|
|
<label>
|
|
{t("look")}
|
|
<input name="look" defaultValue={user.look} maxLength={256} />
|
|
</label>
|
|
|
|
<label>
|
|
{t("credits")}
|
|
<input name="credits" type="number" min={0} defaultValue={user.credits} />
|
|
</label>
|
|
|
|
<label>
|
|
{t("pixels")}
|
|
<input name="pixels" type="number" min={0} defaultValue={user.pixels} />
|
|
</label>
|
|
|
|
<label>
|
|
{t("points")}
|
|
<input name="points" type="number" min={0} defaultValue={user.points} />
|
|
</label>
|
|
|
|
<label>
|
|
{t("duckets")}
|
|
<input name="duckets" type="number" min={0} defaultValue={duckets} />
|
|
</label>
|
|
|
|
<label>
|
|
{t("diamonds")}
|
|
<input name="diamonds" type="number" min={0} defaultValue={diamonds} />
|
|
</label>
|
|
</div>
|
|
|
|
<p className="text-xs text-gray-500 dark:text-gray-400">{t("hint")}</p>
|
|
|
|
<div className="flex gap-2 mt-2">
|
|
<button type="submit" className="btn btn-primary">{t("save")}</button>
|
|
<Link href={`/admin/users/${user.id}`} className="btn">{t("cancel")}</Link>
|
|
</div>
|
|
</form>
|
|
</main>
|
|
);
|
|
}
|