Add admin CMS Settings editor (website_settings)
/admin/settings: list all website_settings, inline value edit, add/overwrite, delete; staff-gated server actions that bust the siteSettings cache after each write. Admin nav extended (Settings). Verified: tsc exit 0, vitest 48/48, next build exit 0.
This commit is contained in:
1 parent
5861d9f1f5
commit
5f8b465451
3 files changed
+96
No files matched your search
@@ -0,0 +1,40 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
import { requireStaff } from "@/lib/admin/guard";
|
||||||
|
import { prisma } from "@/lib/prisma";
|
||||||
|
import { siteSettings } from "@/lib/services/site-settings";
|
||||||
|
|
||||||
|
export async function updateSetting(formData: FormData): Promise<void> {
|
||||||
|
await requireStaff();
|
||||||
|
const key = String(formData.get("key") ?? "").trim();
|
||||||
|
const value = String(formData.get("value") ?? "");
|
||||||
|
if (!key) return;
|
||||||
|
await prisma.websiteSetting.update({ where: { key }, data: { value } });
|
||||||
|
siteSettings.reload();
|
||||||
|
revalidatePath("/admin/settings");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createSetting(formData: FormData): Promise<void> {
|
||||||
|
await requireStaff();
|
||||||
|
const key = String(formData.get("key") ?? "").trim().slice(0, 255);
|
||||||
|
const value = String(formData.get("value") ?? "");
|
||||||
|
const comment = String(formData.get("comment") ?? "").trim().slice(0, 255);
|
||||||
|
if (!key) return;
|
||||||
|
await prisma.websiteSetting.upsert({
|
||||||
|
where: { key },
|
||||||
|
update: { value },
|
||||||
|
create: { key, value, comment: comment || null },
|
||||||
|
});
|
||||||
|
siteSettings.reload();
|
||||||
|
revalidatePath("/admin/settings");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteSetting(formData: FormData): Promise<void> {
|
||||||
|
await requireStaff();
|
||||||
|
const key = String(formData.get("key") ?? "").trim();
|
||||||
|
if (!key) return;
|
||||||
|
await prisma.websiteSetting.delete({ where: { key } });
|
||||||
|
siteSettings.reload();
|
||||||
|
revalidatePath("/admin/settings");
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ export default async function AdminLayout({ children }: { children: ReactNode })
|
|||||||
<Link href="/admin/users">Users</Link>
|
<Link href="/admin/users">Users</Link>
|
||||||
<Link href="/admin/articles">Articles</Link>
|
<Link href="/admin/articles">Articles</Link>
|
||||||
<Link href="/admin/bans">Bans</Link>
|
<Link href="/admin/bans">Bans</Link>
|
||||||
|
<Link href="/admin/settings">Settings</Link>
|
||||||
</nav>
|
</nav>
|
||||||
</aside>
|
</aside>
|
||||||
<section>{children}</section>
|
<section>{children}</section>
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import { createSetting, deleteSetting, updateSetting } from "@/actions/admin-settings";
|
||||||
|
import { prisma } from "@/lib/prisma";
|
||||||
|
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
export default async function AdminSettings() {
|
||||||
|
const settings = await prisma.websiteSetting.findMany({ orderBy: { key: "asc" } });
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<h1>CMS Settings</h1>
|
||||||
|
<p className="muted">
|
||||||
|
Key/value config read at runtime via <code>siteSettings.get()</code>. Saving busts the cache.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<form action={createSetting} className="card" style={{ marginBottom: "1.5rem" }}>
|
||||||
|
<h3 style={{ marginTop: 0 }}>Add / overwrite</h3>
|
||||||
|
<div style={{ display: "flex", gap: "0.5rem", flexWrap: "wrap" }}>
|
||||||
|
<input name="key" placeholder="key" required />
|
||||||
|
<input name="value" placeholder="value" style={{ flex: 1, minWidth: 160 }} />
|
||||||
|
<input name="comment" placeholder="comment" />
|
||||||
|
<button type="submit">Save</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<div className="grid">
|
||||||
|
{settings.map((s) => (
|
||||||
|
<div key={s.key} className="card">
|
||||||
|
<div style={{ display: "flex", gap: "0.75rem", alignItems: "baseline" }}>
|
||||||
|
<strong style={{ minWidth: 220 }}>{s.key}</strong>
|
||||||
|
<form
|
||||||
|
action={updateSetting}
|
||||||
|
style={{ display: "flex", gap: "0.5rem", flex: 1 }}
|
||||||
|
>
|
||||||
|
<input type="hidden" name="key" value={s.key} />
|
||||||
|
<input name="value" defaultValue={s.value} style={{ flex: 1 }} />
|
||||||
|
<button type="submit">Save</button>
|
||||||
|
</form>
|
||||||
|
<form action={deleteSetting}>
|
||||||
|
<input type="hidden" name="key" value={s.key} />
|
||||||
|
<button type="submit">✕</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{s.comment ? (
|
||||||
|
<p className="muted" style={{ margin: "0.4rem 0 0" }}>
|
||||||
|
{s.comment}
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{settings.length === 0 ? <p className="muted">No settings yet.</p> : null}
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user