Files
eu/lib/pages/actions.ts
T
2026-07-11 21:20:57 +03:00

59 lines
1.9 KiB
TypeScript

"use server";
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";
import { validatePage } from "./validation";
import { requireAdmin } from "../user/require";
import { runAction } from "../run-action";
import type { ActionResult } from "../action-result";
export async function savePageAction(params: {
id: string | null,
slug: string,
title: string,
type: PageType,
status: PageStatus,
blocks: Block[],
meta?: PageMeta
}): Promise<ActionResult> {
return runAction(async () => {
await requireAdmin();
const parsed = validatePage(params);
if (!parsed.success) {
throw new Error(parsed.errors.join("; "));
}
if (params.id) {
await updatePage(params.id, params.title, params.type, params.status, params.blocks, params.meta);
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(`/${params.type.toLowerCase()}`);
redirect(`/${params.type.toLowerCase()}/${page.slug}`);
}
});
}
export async function archivePageAction(id: string, slug: string, type: PageType): Promise<ActionResult> {
return runAction(async () => {
await requireAdmin();
await archivePage(id);
await deleteNavLinkByPageId(id);
revalidatePath(`/${type.toLowerCase()}/${slug}`);
revalidatePath(`/${type.toLowerCase()}`);
});
}
export async function deletePageAction(id: string, type: PageType): Promise<ActionResult> {
return runAction(async () => {
await requireAdmin();
await deletePage(id);
revalidatePath(`/${type.toLowerCase()}`);
});
}