feat: improved news

This commit is contained in:
Ivan Rezchikov
2026-06-21 13:35:32 +03:00
parent 95fd6ae716
commit 48656b562a
13 changed files with 154 additions and 147 deletions
+18 -7
View File
@@ -2,7 +2,7 @@
import { redirect } from "next/navigation";
import { Block, PageMeta, PageStatus, PageType } from "./types";
import { createPage, deletePage, updatePage } from "./repository";
import { archivePage, createPage, deletePage, updatePage } from "./repository";
import { revalidatePath } from "next/cache";
export async function savePageAction(params: {
@@ -14,18 +14,29 @@ export async function savePageAction(params: {
blocks: Block[],
meta?: PageMeta
}) {
const basePath = params.type === "NEWS" ? "news" : "pages";
if (params.id) {
await updatePage(params.id, params.title, params.type, params.status, params.blocks, params.meta);
revalidatePath(`/${params.slug}`);
revalidatePath("/news");
revalidatePath(`/${basePath}/${params.slug}`);
revalidatePath(`/${basePath}`);
} else {
const page = await createPage(params.slug, params.title, params.type, params.status, params.blocks, params.meta);
revalidatePath("/news");
redirect(`/news/${page.slug}/edit`);
revalidatePath(`/${basePath}`);
redirect(`/${basePath}/${page.slug}/edit`);
}
}
export async function deletePageAction(id: string) {
export async function archivePageAction(id: string, slug: string, type: PageType) {
await archivePage(id);
const basePath = type === "NEWS" ? "news" : "pages";
revalidatePath(`/${basePath}/${slug}`);
revalidatePath(`/${basePath}`);
redirect(`/${basePath}`);
}
export async function deletePageAction(id: string, type: PageType) {
await deletePage(id);
revalidatePath("/news");
const basePath = type === "NEWS" ? "news" : "pages";
revalidatePath(`/${basePath}`);
}
+10 -44
View File
@@ -37,7 +37,7 @@ export async function createPage(slug: string, title: string, type: PageType, st
status,
meta: meta ?? {},
publishedAt: status === "PUBLISHED" ? new Date() : null,
createdBy: user.id,
createdBy: user.id
},
});
@@ -53,44 +53,13 @@ export async function createPage(slug: string, title: string, type: PageType, st
});
}
if (status === "PUBLISHED") {
await prisma.pageVersion.create({
data: {
pageId: page.id,
title: page.title,
blocks: blocks,
createdBy: user.id,
}
});
}
return page;
}
export async function restoreVersion(pageId: string, versionId: string) {
const version = await prisma.pageVersion.findUnique({
where: { id: versionId }
});
await prisma.$transaction([
prisma.block.deleteMany({ where: { pageId } }),
prisma.block.createMany({
data: (version?.blocks as Block[]).map(block => ({
...block,
pageId,
id: crypto.randomUUID(),
}))
}),
prisma.page.update({
where: { id: pageId },
data: { updatedAt: new Date() }
})
]);
}
export async function updatePage(
id: string,
title: string,
type: PageType,
status: PageStatus,
blocks: Block[],
meta?: PageMeta
@@ -102,10 +71,12 @@ export async function updatePage(
where: { id },
data: {
title,
type,
status,
meta: meta ?? {},
publishedAt: status === "PUBLISHED" ? new Date() : undefined,
updatedAt: new Date(),
updatedBy: user.id
},
});
@@ -124,20 +95,15 @@ export async function updatePage(
: []),
]);
if (status === "PUBLISHED") {
await prisma.pageVersion.create({
data: {
pageId: id,
title: page.title,
blocks: blocks as any,
createdBy: user.id,
},
});
}
return page;
}
export async function archivePage(id: string) {
const user = await getCurrentUser();
if (!user?.isAdmin) throw new Error("Access denied");
await prisma.page.update({ where: { id }, data: { status: "ARCHIVED", updatedBy: user.id } });
}
export async function deletePage(id: string) {
const user = await getCurrentUser();
if (!user?.isAdmin) throw new Error("Access denied");
+1 -10
View File
@@ -39,13 +39,4 @@ export type Block = {
type: BlockType;
position: number;
content: BlockContent;
}
export type PageVersion = {
id: string;
pageId: string;
title: string;
blocks: Block[];
createdAt: Date;
createdBy: string;
};
}