Add admin Articles (CRUD) + Bans resources
- Articles: list / new / edit / delete (websiteArticles), unique-slug generation via slugify() (pure, unit-tested); staff-gated server actions. - Bans: list active bans (ban_expire > now), create ban (writes the bans row + RCON disconnect) with type/duration/reason, lift ban; staff-gated actions. - Admin nav extended (Articles, Bans). Verified: tsc exit 0, vitest 48/48, next build exit 0 (7 /admin routes).
This commit is contained in:
1 parent
9f81096f05
commit
5861d9f1f5
9 files changed
+310
-1
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { deleteArticle, updateArticle } from "@/actions/admin-articles";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export default async function EditArticle({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ id: string }>;
|
||||
}) {
|
||||
const { id } = await params;
|
||||
let article: Awaited<ReturnType<typeof prisma.websiteArticles.findUnique>> = null;
|
||||
try {
|
||||
article = await prisma.websiteArticles.findUnique({ where: { id: BigInt(id) } });
|
||||
} catch {
|
||||
notFound();
|
||||
}
|
||||
if (!article) notFound();
|
||||
|
||||
return (
|
||||
<main>
|
||||
<p className="muted">
|
||||
<Link href="/admin/articles">← Articles</Link>
|
||||
</p>
|
||||
<h1>Edit article</h1>
|
||||
<form action={updateArticle} className="card" style={{ display: "grid", gap: "0.6rem" }}>
|
||||
<input type="hidden" name="id" value={String(article.id)} />
|
||||
<input name="title" defaultValue={article.title} required />
|
||||
<input name="image" defaultValue={article.image} placeholder="Image URL" />
|
||||
<textarea name="shortStory" defaultValue={article.shortStory} rows={2} />
|
||||
<textarea name="fullStory" defaultValue={article.fullStory} rows={10} />
|
||||
<button type="submit">Save</button>
|
||||
</form>
|
||||
<form action={deleteArticle} style={{ marginTop: "1rem" }}>
|
||||
<input type="hidden" name="id" value={String(article.id)} />
|
||||
<button type="submit">Delete article</button>
|
||||
</form>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import Link from "next/link";
|
||||
import { createArticle } from "@/actions/admin-articles";
|
||||
|
||||
export default function NewArticle() {
|
||||
return (
|
||||
<main>
|
||||
<p className="muted">
|
||||
<Link href="/admin/articles">← Articles</Link>
|
||||
</p>
|
||||
<h1>New article</h1>
|
||||
<form action={createArticle} className="card" style={{ display: "grid", gap: "0.6rem" }}>
|
||||
<input name="title" placeholder="Title" required />
|
||||
<input name="image" placeholder="Image URL" />
|
||||
<textarea name="shortStory" placeholder="Short story (summary)" rows={2} />
|
||||
<textarea name="fullStory" placeholder="Full story" rows={10} />
|
||||
<button type="submit">Create</button>
|
||||
</form>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import Link from "next/link";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export default async function AdminArticles() {
|
||||
const articles = await prisma.websiteArticles.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, title: true, slug: true, createdAt: true },
|
||||
take: 50,
|
||||
});
|
||||
|
||||
return (
|
||||
<main>
|
||||
<div style={{ display: "flex", alignItems: "center", gap: "1rem" }}>
|
||||
<h1 style={{ flex: 1 }}>Articles</h1>
|
||||
<Link href="/admin/articles/new">+ New article</Link>
|
||||
</div>
|
||||
<table style={{ width: "100%", borderCollapse: "collapse" }}>
|
||||
<thead>
|
||||
<tr style={{ textAlign: "left", color: "var(--muted)" }}>
|
||||
<th>Title</th>
|
||||
<th>Slug</th>
|
||||
<th>Created</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{articles.map((a) => (
|
||||
<tr key={String(a.id)} style={{ borderTop: "1px solid var(--border)" }}>
|
||||
<td>
|
||||
<Link href={`/admin/articles/${a.id}`}>{a.title}</Link>
|
||||
</td>
|
||||
<td className="muted">{a.slug}</td>
|
||||
<td className="muted">{a.createdAt ? a.createdAt.toISOString().slice(0, 10) : ""}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { createBan, liftBan } from "@/actions/admin-bans";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export default async function AdminBans() {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const bans = await prisma.ban.findMany({
|
||||
where: { banExpire: { gt: now } },
|
||||
orderBy: { timestamp: "desc" },
|
||||
take: 100,
|
||||
});
|
||||
|
||||
return (
|
||||
<main>
|
||||
<h1>Bans</h1>
|
||||
|
||||
<form action={createBan} className="card" style={{ marginBottom: "1.5rem" }}>
|
||||
<h3 style={{ marginTop: 0 }}>New ban</h3>
|
||||
<div style={{ display: "flex", gap: "0.5rem", flexWrap: "wrap" }}>
|
||||
<input name="userId" type="number" min={1} placeholder="User ID" required />
|
||||
<select name="type">
|
||||
<option value="account">account</option>
|
||||
<option value="ip">ip</option>
|
||||
<option value="machine">machine</option>
|
||||
<option value="super">super</option>
|
||||
</select>
|
||||
<input name="hours" type="number" min={0} placeholder="Hours (0 = perm)" />
|
||||
<input name="reason" placeholder="Reason" style={{ flex: 1, minWidth: 160 }} />
|
||||
<button type="submit">Ban</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<table style={{ width: "100%", borderCollapse: "collapse" }}>
|
||||
<thead>
|
||||
<tr style={{ textAlign: "left", color: "var(--muted)" }}>
|
||||
<th>User ID</th>
|
||||
<th>Type</th>
|
||||
<th>Reason</th>
|
||||
<th>Expires</th>
|
||||
<th />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{bans.map((b) => (
|
||||
<tr key={b.id} style={{ borderTop: "1px solid var(--border)" }}>
|
||||
<td>{b.userId}</td>
|
||||
<td>{b.type}</td>
|
||||
<td>{b.banReason}</td>
|
||||
<td className="muted">{new Date(b.banExpire * 1000).toISOString().slice(0, 16).replace("T", " ")}</td>
|
||||
<td>
|
||||
<form action={liftBan}>
|
||||
<input type="hidden" name="id" value={b.id} />
|
||||
<button type="submit">Lift</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{bans.length === 0 ? <p className="muted">No active bans.</p> : null}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -16,6 +16,8 @@ export default async function AdminLayout({ children }: { children: ReactNode })
|
||||
<nav style={{ display: "grid", gap: "0.45rem" }}>
|
||||
<Link href="/admin">Dashboard</Link>
|
||||
<Link href="/admin/users">Users</Link>
|
||||
<Link href="/admin/articles">Articles</Link>
|
||||
<Link href="/admin/bans">Bans</Link>
|
||||
</nav>
|
||||
</aside>
|
||||
<section>{children}</section>
|
||||
|
||||
Reference in new issue
Block a user