feat: basic pages system
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
"use server";
|
||||
|
||||
import { redirect } from "next/navigation";
|
||||
import { Block, PageMeta, PageStatus, PageType } from "./types";
|
||||
import { createPage, deletePage, updatePage } from "./repository";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function savePageAction(params: {
|
||||
id: string | null,
|
||||
slug: string,
|
||||
title: string,
|
||||
type: PageType,
|
||||
status: PageStatus,
|
||||
blocks: Block[],
|
||||
meta?: PageMeta
|
||||
}) {
|
||||
if (params.id) {
|
||||
await updatePage(params.id, params.title, params.status, params.blocks, params.meta);
|
||||
revalidatePath(`/${params.slug}`);
|
||||
revalidatePath("/news");
|
||||
} else {
|
||||
const page = await createPage(params.slug, params.title, params.type, params.status, params.blocks, params.meta);
|
||||
revalidatePath("/news");
|
||||
redirect(`/news/${page.slug}/edit`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function deletePageAction(id: string) {
|
||||
await deletePage(id);
|
||||
revalidatePath("/news");
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
import { getCurrentUser } from "../user";
|
||||
import { Block, PageMeta, PageStatus, PageType } from "./types";
|
||||
import { prisma } from "../prisma";
|
||||
|
||||
export async function getPages(type: PageType, status: PageStatus, limit?: number) {
|
||||
return await prisma.page.findMany({ where: { type, status }, take: limit, orderBy: { publishedAt: "desc" }, });
|
||||
}
|
||||
|
||||
export async function getPageById(id: string) {
|
||||
return await prisma.page.findUnique({ where: { id: id } });
|
||||
}
|
||||
|
||||
export async function getPageBySlug(slug: string) {
|
||||
return await prisma.page.findUnique({ where: { slug } });
|
||||
}
|
||||
|
||||
export async function createPage(slug: string, title: string, type: PageType, status: PageStatus, blocks: Block[], meta?: PageMeta) {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
throw new Error("Failed to fetch user");
|
||||
}
|
||||
|
||||
if (!user.isAdmin) {
|
||||
throw new Error("Access denied")
|
||||
}
|
||||
|
||||
const existingPage = await prisma.page.findUnique({ where: { slug } });
|
||||
if (existingPage) {
|
||||
throw new Error("Page already exists");
|
||||
}
|
||||
|
||||
const page = await prisma.page.create({
|
||||
data: {
|
||||
slug,
|
||||
title,
|
||||
type,
|
||||
status,
|
||||
meta: meta ?? {},
|
||||
publishedAt: status === "PUBLISHED" ? new Date() : null,
|
||||
createdBy: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (blocks.length > 0) {
|
||||
await prisma.block.createMany({
|
||||
data: blocks.map((block) => ({
|
||||
pageId: page.id,
|
||||
parentId: block.parentId,
|
||||
type: block.type,
|
||||
position: block.position,
|
||||
content: block.content,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
status: PageStatus,
|
||||
blocks: Block[],
|
||||
meta?: PageMeta
|
||||
) {
|
||||
const user = await getCurrentUser();
|
||||
if (!user?.isAdmin) throw new Error("Access denied");
|
||||
|
||||
const page = await prisma.page.update({
|
||||
where: { id },
|
||||
data: {
|
||||
title,
|
||||
status,
|
||||
meta: meta ?? {},
|
||||
publishedAt: status === "PUBLISHED" ? new Date() : undefined,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.$transaction([
|
||||
prisma.block.deleteMany({ where: { pageId: id } }),
|
||||
...(blocks.length > 0
|
||||
? [prisma.block.createMany({
|
||||
data: blocks.map((b) => ({
|
||||
pageId: id,
|
||||
parentId: b.parentId,
|
||||
type: b.type,
|
||||
position: b.position,
|
||||
content: b.content as any,
|
||||
})),
|
||||
})]
|
||||
: []),
|
||||
]);
|
||||
|
||||
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 deletePage(id: string) {
|
||||
const user = await getCurrentUser();
|
||||
if (!user?.isAdmin) throw new Error("Access denied");
|
||||
await prisma.page.delete({ where: { id } });
|
||||
}
|
||||
|
||||
export async function getBlocksByPageId(pageId: string) {
|
||||
return prisma.block.findMany({
|
||||
where: { pageId },
|
||||
orderBy: { position: "asc" },
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
export const PAGE_TYPES = ["NEWS"] as const;
|
||||
export type PageType = typeof PAGE_TYPES[number];
|
||||
|
||||
export const PAGE_STATUSES = ["DRAFT", "PUBLISHED", "ARCHIVED"] as const;
|
||||
export type PageStatus = typeof PAGE_STATUSES[number];
|
||||
|
||||
export type PageMeta = {
|
||||
tags?: string[];
|
||||
location?: string;
|
||||
}
|
||||
|
||||
export type Page = {
|
||||
id: string;
|
||||
slug: string;
|
||||
title: string;
|
||||
type: PageType;
|
||||
status: PageStatus;
|
||||
meta: PageMeta;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
publishedAt: Date | null;
|
||||
createdBy: string;
|
||||
}
|
||||
|
||||
export const BLOCK_TYPES = ["PARAGRAPH", "HEADING", "IMAGE", "FILE", "QUOTE", "DIVIDER", "TABLE", "CALLOUT", "LIST", "LINK"] as const;
|
||||
export type BlockType = typeof BLOCK_TYPES[number];
|
||||
|
||||
export type BlockContent =
|
||||
| { type: "PARAGRAPH"; text: string }
|
||||
| { type: "HEADING"; text: string; level: 1 | 2 | 3 }
|
||||
| { type: "IMAGE"; url: string; caption?: string }
|
||||
| { type: "FILE"; url: string; name: string }
|
||||
| { type: "QUOTE"; text: string; author?: string }
|
||||
| { type: "DIVIDER" }
|
||||
| { type: "TABLE"; headers: string[]; rows: string[][] }
|
||||
| { type: "CALLOUT"; text: string; icon?: string; color?: string }
|
||||
| { type: "LIST"; style: "BULLET" | "NUMBERED"; items: string[] }
|
||||
| { type: "LINK"; text?: string; url: string };
|
||||
|
||||
export type Block = {
|
||||
id: string;
|
||||
pageId: string;
|
||||
parentId: string | null;
|
||||
type: BlockType;
|
||||
position: number;
|
||||
content: BlockContent;
|
||||
}
|
||||
|
||||
export type PageVersion = {
|
||||
id: string;
|
||||
pageId: string;
|
||||
title: string;
|
||||
blocks: Block[];
|
||||
createdAt: Date;
|
||||
createdBy: string;
|
||||
};
|
||||
Reference in New Issue
Block a user