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 />
</>
);
}
+60
View File
@@ -0,0 +1,60 @@
"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 { Loader2 } from "lucide-react";
export default function CreatePageClient() {
const router = useRouter();
const [isPending, startTransition] = useTransition();
const dataRef = useRef<EditorData>({
title: "",
type: "NEWS",
slug: "",
location: "",
tags: [],
blocks: [],
});
function handleSave(status: "DRAFT" | "PUBLISHED") {
startTransition(async () => {
const data = dataRef.current;
await savePageAction({
id: null,
slug: data.slug || data.title.toLowerCase().replace(/\s+/g, "-"),
title: data.title,
type: data.type,
status,
blocks: data.blocks,
meta: { location: data.location, tags: data.tags },
});
router.push("/news");
});
}
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">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 onChange={(data) => { dataRef.current = data; }} />
</div>
);
}
+18
View File
@@ -0,0 +1,18 @@
import Header from "@/components/header";
import Footer from "@/components/footer";
import { getCurrentUser } from "@/lib/user";
import { redirect } from "next/navigation";
import CreatePageClient from "./client";
export default async function CreatePage() {
const user = await getCurrentUser();
if (!user?.isAdmin) redirect("/");
return (
<>
<Header />
<CreatePageClient />
<Footer />
</>
);
}
+16
View File
@@ -142,4 +142,20 @@ h2 {
h3 {
@apply text-xl tracking-tight
}
h4 {
@apply text-lg tracking-tight
}
p {
@apply text-base tracking-tight
}
h5 {
@apply text-sm tracking-tight
}
h6 {
@apply text-xs tracking-tight
}
-35
View File
@@ -1,35 +0,0 @@
import Header from "@/components/header";
import Footer from "@/components/footer";
import { Button } from "@/components/ui/button";
import { getPageBySlug } from "@/lib/pages/repository";
import { getCurrentUser } from "@/lib/user";
import { redirect } from "next/navigation";
import Editor from "@/components/editor";
export default async function EditPage({ params }: { params: Promise<{ slug: string }> }) {
const user = await getCurrentUser();
if (!user?.isAdmin) redirect("/");
const { slug } = await params;
const page = slug === "create" ? null : await getPageBySlug(slug);
return (
<>
<Header />
<div className="flex flex-col p-16 gap-16">
<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-4">
<Button variant="outline">Save as draft</Button>
<Button>Publish</Button>
</div>
</div>
<Editor page={page} />
</div>
<Footer />
</>
);
}
+8 -5
View File
@@ -6,6 +6,7 @@ import { Button } from "@/components/ui/button";
import { getPages } from "@/lib/pages/repository";
import { PageMeta } from "@/lib/pages/types";
import { getCurrentUser } from "@/lib/user";
import { formatDate } from "@/lib/utils";
import { ArrowRight, Calendar, Globe2 } from "lucide-react";
import Link from "next/link";
@@ -22,19 +23,20 @@ export default async function News() {
<h1 className="underline underline-offset-8 decoration-2">News</h1>
<div className="flex flex-row items-center gap-4">
<DraftsDialog drafts={drafts} />
{user?.isAdmin && (<Link href="/news/create/edit"><Button variant="secondary">Create a news page</Button></Link>)}
{user?.isAdmin && (<Link href="/create/"><Button variant="secondary">Create a new page</Button></Link>)}
</div>
</div>
<div className="flex flex-col gap-6">
{
news.length > 0 ? news.map((n) => (
<div className="flex flex-col gap-2" key={n.id}>
<h2 className="w-max hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">{n.title}</h2>
<Link href={`/news/${n.slug}`}><h2 className="w-max hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">{n.title}</h2></Link>
<div className="flex flex-row gap-8 items-center">
<h3 className="flex gap-2 items-baseline"><Calendar size={18} />{(n.publishedAt)?.toString()}</h3>
<h3 className="flex gap-2 items-baseline"><Calendar size={18} />{formatDate(n.publishedAt)}</h3>
<h3 className="flex gap-2 items-baseline"><Globe2 size={18} />{(n.meta as PageMeta)?.location}</h3>
<div className="flex flex-row gap-4">
{((n.meta as PageMeta)?.tags)?.map((i, tag) => (
<Badge key={i}>{tag}</Badge>
{((n.meta as PageMeta)?.tags)?.map((tag) => (
<Badge key={tag}>{tag}</Badge>
))}
</div>
</div>
@@ -43,6 +45,7 @@ export default async function News() {
<h2 className="w-max flex flex-row items-center gap-2 hover:gap-4 hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">See all news<ArrowRight size={32} /></h2>
)) : <h2>There are no news yet...</h2>
}
</div>
</div>
<Footer />
</>
+19 -16
View File
@@ -3,7 +3,9 @@ import Header from "@/components/header";
import { Badge } from "@/components/ui/badge"
import { getPages } from "@/lib/pages/repository";
import { PageMeta } from "@/lib/pages/types";
import { formatDate } from "@/lib/utils";
import { ArrowRight, Calendar, Globe2 } from "lucide-react";
import Link from "next/link";
export default async function Home() {
const news = await getPages("NEWS", "PUBLISHED", 5);
@@ -36,24 +38,25 @@ export default async function Home() {
</div>
<div className="flex flex-col gap-8 mt-8">
<h1 className="underline underline-offset-8 decoration-2">Recent news</h1>
{
news.length > 0 ? news.map((n) => (
<div className="flex flex-col gap-2" key={n.id}>
<h2 className="w-max hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">{n.title}</h2>
<div className="flex flex-row gap-8 items-center">
<h3 className="flex gap-2 items-baseline"><Calendar size={18} />{(n.publishedAt)?.toString()}</h3>
<h3 className="flex gap-2 items-baseline"><Globe2 size={18} />{(n.meta as PageMeta)?.location}</h3>
<div className="flex flex-row gap-4">
{((n.meta as PageMeta)?.tags)?.map((i, tag) => (
<Badge key={i}>{tag}</Badge>
))}
<div className="flex flex-col gap-6">
{
news.length > 0 ? news.map((n) => (
<div className="flex flex-col gap-2" key={n.id}>
<Link href={`/news/${n.slug}`}><h2 className="w-max hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">{n.title}</h2></Link>
<div className="flex flex-row gap-8 items-center">
<h3 className="flex gap-2 items-baseline"><Calendar size={18} />{formatDate(n.publishedAt)}</h3>
<h3 className="flex gap-2 items-baseline"><Globe2 size={18} />{(n.meta as PageMeta)?.location}</h3>
<div className="flex flex-row gap-4">
{((n.meta as PageMeta)?.tags)?.map((tag) => (
<Badge key={tag}>{tag}</Badge>
))}
</div>
</div>
</div>
</div>
), (
<h2 className="w-max flex flex-row items-center gap-2 hover:gap-4 hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">See all news<ArrowRight size={32} /></h2>
)) : <h2>There are no news yet...</h2>
}
)) : <h2>There are no news yet...</h2>
}
</div>
<Link href="/news"><h2 className="w-max flex flex-row items-center gap-2 hover:gap-4 hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">See all news<ArrowRight size={32} /></h2></Link>
</div>
</div>
<Footer />