Replace the plain articles table with a responsive card grid showing thumbnails, title, date and author. Add a slug field to the article form with live auto-suggestion, i18n-driven labels, a larger image preview and delete confirmation. Persist a user-provided slug (falls back to an auto-generated one) on create and update.
85 lines
2.4 KiB
TypeScript
85 lines
2.4 KiB
TypeScript
import { Newspaper } from "lucide-react";
|
|
import Link from "next/link";
|
|
import { notFound, redirect } from "next/navigation";
|
|
import { getTranslations } from "next-intl/server";
|
|
import { deleteArticle, updateArticle } from "@/actions/admin-articles";
|
|
import { ArticleForm } from "@/components/admin/article-form";
|
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export default async function EditArticle({
|
|
params,
|
|
searchParams,
|
|
}: {
|
|
params: Promise<{ id: string }>;
|
|
searchParams: Promise<{ error?: string }>;
|
|
}) {
|
|
const { session, permissions } = await getAdminContext();
|
|
if (!canAccess(permissions, PERMS.NEWS_VIEW, session.user.rank)) {
|
|
redirect("/admin");
|
|
}
|
|
|
|
const { id } = await params;
|
|
const { error } = await searchParams;
|
|
const t = await getTranslations("pages.admin.articles");
|
|
const article = await prisma.websiteArticles
|
|
.findUnique({
|
|
where: { id: BigInt(id) },
|
|
select: {
|
|
id: true,
|
|
slug: true,
|
|
title: true,
|
|
shortStory: true,
|
|
fullStory: true,
|
|
image: true,
|
|
},
|
|
})
|
|
.catch(() => null);
|
|
if (!article) notFound();
|
|
|
|
return (
|
|
<main>
|
|
<div className="flex items-center gap-3 mb-6">
|
|
<div className="w-10 h-10 rounded-xl bg-gradient-to-br from-[var(--admin-accent)]/20 to-[var(--admin-accent)]/5 grid place-items-center">
|
|
<Newspaper size={20} className="text-[var(--admin-accent)]" />
|
|
</div>
|
|
<div>
|
|
<h1 className="m-0 text-xl font-extrabold text-[var(--admin-text)]">
|
|
{t("form.title")}
|
|
</h1>
|
|
<p className="m-0 text-xs text-[var(--admin-text-muted)] mt-0.5">
|
|
<Link href="/admin/articles">← {t("title")}</Link>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
{error ? (
|
|
<p className="text-xs text-[var(--admin-error)] dark:text-[var(--admin-error)] mb-4">
|
|
Error: {error}
|
|
</p>
|
|
) : null}
|
|
<ArticleForm
|
|
action={async (fd) => {
|
|
"use server";
|
|
fd.set("id", String(article.id));
|
|
await updateArticle(fd);
|
|
}}
|
|
defaultValues={{
|
|
title: article.title,
|
|
slug: article.slug,
|
|
image: article.image,
|
|
shortStory: article.shortStory,
|
|
fullStory: article.fullStory,
|
|
}}
|
|
/>
|
|
<form action={deleteArticle} style={{ marginTop: "1rem" }}>
|
|
<input type="hidden" name="id" value={String(article.id)} />
|
|
<button type="submit" className="btn btn-primary">
|
|
{t("form.deleteArticle")}
|
|
</button>
|
|
</form>
|
|
</main>
|
|
);
|
|
}
|