diff --git a/app/[type]/[slug]/edit/client.tsx b/app/[type]/[slug]/edit/client.tsx new file mode 100644 index 0000000..3fca849 --- /dev/null +++ b/app/[type]/[slug]/edit/client.tsx @@ -0,0 +1,81 @@ +"use client"; + +import { useRef, useTransition } from "react"; +import { useRouter } from "next/navigation"; +import { Button } from "@/components/ui/button"; +import Editor, { EditorData } from "@/components/editor"; +import { savePageAction } from "@/lib/pages/actions"; +import { Block, Page, PageMeta, PageType } from "@/lib/pages/types"; +import { Loader2 } from "lucide-react"; + +type Props = { + page: Page | null; + pageType: PageType; + initialBlocks?: Block[]; +}; + +export default function EditPageClient({ page, pageType, initialBlocks = [] }: Props) { + const router = useRouter(); + const [isPending, startTransition] = useTransition(); + + const dataRef = useRef({ + title: page?.title ?? "", + type: page?.type ?? "", + slug: page?.slug ?? "", + location: (page?.meta as PageMeta)?.location ?? "", + tags: (page?.meta as PageMeta)?.tags ?? [], + blocks: initialBlocks, + }); + + function handleSave(status: "DRAFT" | "PUBLISHED") { + startTransition(async () => { + const data = dataRef.current; + await savePageAction({ + id: page?.id ?? null, + slug: data.slug || data.title.toLowerCase().replace(/\s+/g, "-"), + title: data.title, + type: pageType, + status, + blocks: data.blocks, + meta: { location: data.location, tags: data.tags }, + }); + const basePath = pageType === "NEWS" ? "news" : "pages"; + router.push(`/${basePath}/${data.slug}`); + }); + } + + return ( +
+
+

+ {page ? "Edit page" : "Create page"} +

+
+ + +
+
+ { + dataRef.current = data; + }} + /> +
+ ); +} \ No newline at end of file diff --git a/app/[type]/[slug]/edit/page.tsx b/app/[type]/[slug]/edit/page.tsx new file mode 100644 index 0000000..cd0e54d --- /dev/null +++ b/app/[type]/[slug]/edit/page.tsx @@ -0,0 +1,28 @@ +import Header from "@/components/header"; +import Footer from "@/components/footer"; +import { getPageBySlug, getBlocksByPageId } from "@/lib/pages/repository"; +import { getCurrentUser } from "@/lib/user"; +import { redirect, notFound } from "next/navigation"; +import { PAGE_TYPES, PageType } from "@/lib/pages/types"; +import EditPageClient from "./client"; + +export default async function EditPage({ params }: { params: Promise<{ type: string; slug: string }> }) { + const user = await getCurrentUser(); + if (!user?.isAdmin) redirect("/"); + + const { type, slug } = await params; + + const pageType = type.toUpperCase() as PageType; + if (!PAGE_TYPES.includes(pageType)) notFound(); + + const page = await getPageBySlug(slug); + const blocks = page ? await getBlocksByPageId(page.id) : []; + + return ( + <> +
+ +