feat: news

This commit is contained in:
Ivan Rezchikov
2026-06-20 18:30:39 +03:00
parent 674fd11aa9
commit 95fd6ae716
19 changed files with 886 additions and 159 deletions
+81
View File
@@ -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<EditorData>({
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 (
<div className="flex flex-col p-16 gap-12">
<div className="flex flex-row items-center justify-between">
<h1 className="underline underline-offset-8 decoration-2">
{page ? "Edit page" : "Create page"}
</h1>
<div className="flex flex-row gap-3">
<Button
variant="outline"
disabled={isPending}
onClick={() => handleSave("DRAFT")}
>
{isPending && <Loader2 size={14} className="animate-spin mr-1" />}
Save as draft
</Button>
<Button
disabled={isPending}
onClick={() => handleSave("PUBLISHED")}
>
{isPending && <Loader2 size={14} className="animate-spin mr-1" />}
Publish
</Button>
</div>
</div>
<Editor
page={page}
type={pageType}
initialBlocks={initialBlocks}
onChange={(data) => {
dataRef.current = data;
}}
/>
</div>
);
}
+28
View File
@@ -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 (
<>
<Header />
<EditPageClient page={page ?? null} pageType={pageType} initialBlocks={blocks} />
<Footer />
</>
);
}
+78
View File
@@ -0,0 +1,78 @@
import Header from "@/components/header";
import Footer from "@/components/footer";
import { getPageBySlug, getBlocksByPageId } from "@/lib/pages/repository";
import { getCurrentUser } from "@/lib/user";
import { notFound } from "next/navigation";
import { Block, PAGE_TYPES, PageMeta, PageType } from "@/lib/pages/types";
import { formatDate } from "@/lib/utils";
import { CalendarIcon, Globe2Icon, InfoIcon } from "lucide-react";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Separator } from "@/components/ui/separator";
function BlockRenderer({ block }: {block: Block }) {
const b = block.content
switch (b.type) {
case "PARAGRAPH":
return <h4>{b.text}</h4>
case "HEADING":
switch (b.level) {
case 1:
return <h1>{b.text}</h1>
case 2:
return <h2>{b.text}</h2>
case 3:
return <h3>{b.text}</h3>
}
case "QUOTE":
return <div className="flex flex-col gap-2 border-l-2 border-muted-foreground pl-4">
<p>{b.text}</p>
<h5 className="text-sm text-muted-foreground">{b.author}</h5>
</div>
case "CALLOUT":
return <Alert>
<InfoIcon />
<AlertTitle>{b.title}</AlertTitle>
<AlertDescription>{b.text}</AlertDescription>
</Alert>
case "DIVIDER":
return <Separator />
default:
return
}
}
export default async function Page({ params }: { params: Promise<{ type: string; slug: string }> }) {
const user = await getCurrentUser();
const { type, slug } = await params;
const pageType = type.toUpperCase() as PageType;
if (!PAGE_TYPES.includes(pageType)) notFound();
const page = await getPageBySlug(slug);
if (!page) notFound();
const blocks = page ? await getBlocksByPageId(page.id) : [];
return (
<>
<Header />
<div className="flex flex-col p-16 gap-16">
<div className="flex flex-col gap-4 items-center justify-center">
<h1 className="underline underline-offset-8 decoration-2 text-center">{page?.title}</h1>
<div className="flex flex-row gap-4 items-center">
<h3 className="flex gap-2 items-baseline"><CalendarIcon size={18} />{formatDate(page?.publishedAt)}</h3>
{formatDate(page?.publishedAt) !== formatDate(page?.updatedAt) && <h3 className="flex gap-2 items-baseline"><CalendarIcon size={18} />Updated on {formatDate(page?.updatedAt)}</h3>}
{<h3 className="flex gap-2 items-baseline"><Globe2Icon size={18} />{(page?.meta as PageMeta)?.location}</h3>}
</div>
</div>
<div className="flex flex-col gap-8">
{blocks.map((block) => (
<BlockRenderer key={block.id} block={block} />
))}
</div>
</div>
<Footer />
</>
);
}