feat: pages
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { addNavLink, deleteNavLinkByPageId } from "./repository";
|
||||
import { prisma } from "../prisma";
|
||||
|
||||
export async function pinPageAction(pageId: string) {
|
||||
const page = await prisma.page.findUnique({ where: { id: pageId } });
|
||||
if (!page) throw new Error(`Page not found: ${pageId}`);
|
||||
|
||||
|
||||
await addNavLink({ pageId });
|
||||
revalidatePath("/", "layout");
|
||||
}
|
||||
|
||||
export async function unpinPageAction(pageId: string) {
|
||||
await deleteNavLinkByPageId(pageId);
|
||||
revalidatePath("/", "layout");
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { prisma } from "../prisma";
|
||||
|
||||
export async function getNavLinks() {
|
||||
return await prisma.navLink.findMany({
|
||||
orderBy: { order: "asc" },
|
||||
include: {
|
||||
page: true
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function getNavLinkByPageId(pageId: string) {
|
||||
return await prisma.navLink.findFirst({ where: { pageId }});
|
||||
}
|
||||
|
||||
export async function addNavLink(link: { pageId?: string; href?: string; label?: string }) {
|
||||
const last = await prisma.navLink.findFirst({
|
||||
orderBy: { order: "desc" },
|
||||
});
|
||||
|
||||
return await prisma.navLink.create({
|
||||
data: {
|
||||
...link,
|
||||
order: last ? last.order + 1 : 0,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function reorderNavLinks(orderedIds: string[]) {
|
||||
await prisma.$transaction(
|
||||
orderedIds.map((id, index) =>
|
||||
prisma.navLink.update({
|
||||
where: { id },
|
||||
data: { order: index },
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteNavLink(id: string) {
|
||||
await prisma.navLink.delete({ where: { id } });
|
||||
}
|
||||
|
||||
export async function deleteNavLinkByPageId(pageId: string) {
|
||||
await prisma.navLink.deleteMany({ where: { pageId } });
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export type NavLink = {
|
||||
id: string;
|
||||
label: string | null;
|
||||
order: number;
|
||||
pageId: string | null;
|
||||
href: string | null;
|
||||
}
|
||||
+9
-11
@@ -4,6 +4,7 @@ import { redirect } from "next/navigation";
|
||||
import { Block, PageMeta, PageStatus, PageType } from "./types";
|
||||
import { archivePage, createPage, deletePage, updatePage } from "./repository";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { deleteNavLinkByPageId } from "../navlinks/repository";
|
||||
|
||||
export async function savePageAction(params: {
|
||||
id: string | null,
|
||||
@@ -14,29 +15,26 @@ 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(`/${basePath}/${params.slug}`);
|
||||
revalidatePath(`/${basePath}`);
|
||||
revalidatePath(`/${params.type.toLowerCase()}/${params.slug}`);
|
||||
revalidatePath(`/${params.type.toLowerCase()}`);
|
||||
} else {
|
||||
const page = await createPage(params.slug, params.title, params.type, params.status, params.blocks, params.meta);
|
||||
revalidatePath(`/${basePath}`);
|
||||
redirect(`/${basePath}/${page.slug}/edit`);
|
||||
revalidatePath(`/${params.type.toLowerCase()}`);
|
||||
redirect(`/${params.type.toLowerCase()}/${page.slug}`);
|
||||
}
|
||||
}
|
||||
|
||||
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}`);
|
||||
await deleteNavLinkByPageId(id);
|
||||
revalidatePath(`/${type.toLowerCase()}/${slug}`);
|
||||
revalidatePath(`/${type.toLowerCase()}`);
|
||||
}
|
||||
|
||||
export async function deletePageAction(id: string, type: PageType) {
|
||||
await deletePage(id);
|
||||
const basePath = type === "NEWS" ? "news" : "pages";
|
||||
revalidatePath(`/${basePath}`);
|
||||
revalidatePath(`/${type.toLowerCase()}`);
|
||||
}
|
||||
+7
-2
@@ -17,12 +17,13 @@ export type Page = {
|
||||
status: PageStatus;
|
||||
meta: PageMeta;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
updatedAt: Date | null;
|
||||
publishedAt: Date | null;
|
||||
createdBy: string;
|
||||
updatedBy: string | null;
|
||||
}
|
||||
|
||||
export const BLOCK_TYPES = ["PARAGRAPH", "HEADING", "QUOTE", "DIVIDER", "CALLOUT"] as const;
|
||||
export const BLOCK_TYPES = ["PARAGRAPH", "HEADING", "QUOTE", "DIVIDER", "CALLOUT", "IMAGE", "LIST", "TABLE", "LINK"] as const;
|
||||
export type BlockType = typeof BLOCK_TYPES[number];
|
||||
|
||||
export type BlockContent =
|
||||
@@ -31,6 +32,10 @@ export type BlockContent =
|
||||
| { type: "QUOTE"; text: string; author?: string }
|
||||
| { type: "DIVIDER" }
|
||||
| { type: "CALLOUT"; title: string; text: string }
|
||||
| { type: "IMAGE"; url: string; caption?: string }
|
||||
| { type: "LIST"; style: "BULLET" | "ORDERED"; items: string[] }
|
||||
| { type: "TABLE"; headers: string[]; rows: string[][] }
|
||||
| { type: "LINK"; label: string; href: string }
|
||||
|
||||
export type Block = {
|
||||
id: string;
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ export async function getCurrentUser() {
|
||||
return findUserById(userId);
|
||||
}
|
||||
|
||||
export async function getUser(id: string) {
|
||||
export async function getUser(id: string | null) {
|
||||
if (!id) return null;
|
||||
return findUserById(id);
|
||||
}
|
||||
Reference in New Issue
Block a user