feat: improved news
This commit is contained in:
+18
-7
@@ -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
@@ -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
@@ -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;
|
||||
};
|
||||
}
|
||||
@@ -5,4 +5,9 @@ export async function getCurrentUser() {
|
||||
const userId = await getSessionUserId();
|
||||
if (!userId) return null;
|
||||
return findUserById(userId);
|
||||
}
|
||||
|
||||
export async function getUser(id: string) {
|
||||
if (!id) return null;
|
||||
return findUserById(id);
|
||||
}
|
||||
Reference in New Issue
Block a user