From 95fd6ae716abae690cd3c95e301a469f9903fa4c Mon Sep 17 00:00:00 2001 From: Ivan Rezchikov Date: Sat, 20 Jun 2026 18:30:39 +0300 Subject: [PATCH] feat: news --- app/[type]/[slug]/edit/client.tsx | 81 ++++++++++ app/[type]/[slug]/edit/page.tsx | 28 ++++ app/[type]/[slug]/page.tsx | 78 +++++++++ app/create/client.tsx | 60 +++++++ app/create/page.tsx | 18 +++ app/globals.css | 16 ++ app/news/[slug]/edit/page.tsx | 35 ---- app/news/page.tsx | 13 +- app/page.tsx | 35 ++-- components/drafts.tsx | 11 +- components/editor.tsx | 259 ++++++++++++++++++++---------- components/header.tsx | 5 +- components/ui/alert.tsx | 76 +++++++++ components/ui/select.tsx | 190 ++++++++++++++++++++++ components/ui/table.tsx | 116 +++++++++++++ lib/pages/actions.ts | 2 +- lib/pages/types.ts | 11 +- lib/utils.ts | 9 ++ todo | 2 +- 19 files changed, 886 insertions(+), 159 deletions(-) create mode 100644 app/[type]/[slug]/edit/client.tsx create mode 100644 app/[type]/[slug]/edit/page.tsx create mode 100644 app/[type]/[slug]/page.tsx create mode 100644 app/create/client.tsx create mode 100644 app/create/page.tsx delete mode 100644 app/news/[slug]/edit/page.tsx create mode 100644 components/ui/alert.tsx create mode 100644 components/ui/select.tsx create mode 100644 components/ui/table.tsx 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 ( + <> +
+ +