From 64923cd1b22e8e86d290f238a43614c52b480cf2 Mon Sep 17 00:00:00 2001 From: Ivan Rezchikov Date: Mon, 22 Jun 2026 16:44:06 +0300 Subject: [PATCH] feat: pages --- app/[type]/[slug]/archiveButton.tsx | 5 +- app/[type]/[slug]/edit/client.tsx | 3 +- app/[type]/[slug]/edit/page.tsx | 1 + app/[type]/[slug]/page.tsx | 180 +++++-- app/[type]/[slug]/pinToggleButton.tsx | 23 + app/news/page.tsx | 6 +- app/page.tsx | 15 +- components/drafts.tsx | 2 - components/editor.tsx | 674 ++++++++++++++++++-------- components/footer.tsx | 10 +- components/header.tsx | 12 +- components/ui/dropdown-menu.tsx | 267 ++++++++++ lib/navlinks/actions.ts | 19 + lib/navlinks/repository.ts | 46 ++ lib/navlinks/types.ts | 7 + lib/pages/actions.ts | 20 +- lib/pages/types.ts | 9 +- lib/user.ts | 2 +- next.config.ts | 9 +- prisma/schema.prisma | 11 + 20 files changed, 1038 insertions(+), 283 deletions(-) create mode 100644 app/[type]/[slug]/pinToggleButton.tsx create mode 100644 components/ui/dropdown-menu.tsx create mode 100644 lib/navlinks/actions.ts create mode 100644 lib/navlinks/repository.ts create mode 100644 lib/navlinks/types.ts diff --git a/app/[type]/[slug]/archiveButton.tsx b/app/[type]/[slug]/archiveButton.tsx index 6e53c92..a6f4e36 100644 --- a/app/[type]/[slug]/archiveButton.tsx +++ b/app/[type]/[slug]/archiveButton.tsx @@ -5,8 +5,9 @@ import { Button } from "@/components/ui/button"; import { ArchiveIcon } from "lucide-react"; import { Spinner } from "@/components/ui/spinner"; import { archivePageAction } from "@/lib/pages/actions"; +import { PageType } from "@/lib/pages/types"; -export function ArchiveButton({ pageId }: { pageId: string }) { +export function ArchiveButton({ pageId, slug, type }: { pageId: string, slug: string, type: PageType }) { const [isPending, startTransition] = useTransition(); return ( @@ -14,7 +15,7 @@ export function ArchiveButton({ pageId }: { pageId: string }) { variant="secondary" size="icon-sm" disabled={isPending} - onClick={() => startTransition(() => archivePageAction(pageId))} + onClick={() => startTransition(() => archivePageAction(pageId, slug, type))} > {isPending ? : } diff --git a/app/[type]/[slug]/edit/client.tsx b/app/[type]/[slug]/edit/client.tsx index 8ed41ac..036d5de 100644 --- a/app/[type]/[slug]/edit/client.tsx +++ b/app/[type]/[slug]/edit/client.tsx @@ -39,8 +39,7 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P blocks: data.blocks, meta: { location: data.location, tags: data.tags }, }); - const basePath = pageType === "NEWS" ? "news" : "pages"; - router.push(`/${basePath}/${data.slug}`); + router.push(`/${page?.type.toLowerCase()}/${data.slug}`); }); } diff --git a/app/[type]/[slug]/edit/page.tsx b/app/[type]/[slug]/edit/page.tsx index cd0e54d..fba4e50 100644 --- a/app/[type]/[slug]/edit/page.tsx +++ b/app/[type]/[slug]/edit/page.tsx @@ -16,6 +16,7 @@ export default async function EditPage({ params }: { params: Promise<{ type: str if (!PAGE_TYPES.includes(pageType)) notFound(); const page = await getPageBySlug(slug); + if (!page) notFound(); const blocks = page ? await getBlocksByPageId(page.id) : []; return ( diff --git a/app/[type]/[slug]/page.tsx b/app/[type]/[slug]/page.tsx index a3d4481..90eacd2 100644 --- a/app/[type]/[slug]/page.tsx +++ b/app/[type]/[slug]/page.tsx @@ -4,44 +4,131 @@ import { getPageBySlug, getBlocksByPageId } from "@/lib/pages/repository"; import { getCurrentUser, getUser } from "@/lib/user"; import { notFound } from "next/navigation"; import { Block, PAGE_TYPES, PageMeta, PageType } from "@/lib/pages/types"; -import { formatDate } from "@/lib/utils"; -import { ArchiveIcon, CalendarIcon, Globe2Icon, InfoIcon, PencilIcon, UserIcon } from "lucide-react"; +import { cn, formatDate } from "@/lib/utils"; +import { CalendarIcon, DotIcon, Globe2Icon, InfoIcon, PencilIcon, UserIcon } from "lucide-react"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Separator } from "@/components/ui/separator"; import Link from "next/link"; import { Button } from "@/components/ui/button"; -import { archivePageAction } from "@/lib/pages/actions"; import { ArchiveButton } from "./archiveButton"; +import { PinToggleButton } from "./pinToggleButton"; +import { getNavLinkByPageId } from "@/lib/navlinks/repository"; +import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; +import Image from "next/image"; + +function BlockRenderer({ block }: { block: Block }) { + const b = block.content; -function BlockRenderer({ block }: {block: Block }) { - const b = block.content switch (b.type) { case "PARAGRAPH": - return

{b.text}

+ return ( +

+ {b.text} +

+ ); + case "HEADING": switch (b.level) { case 1: - return

{b.text}

+ return

{b.text}

; case 2: - return

{b.text}

+ return

{b.text}

; case 3: - return

{b.text}

+ return

{b.text}

; + default: + return

{b.text}

; } + case "QUOTE": - return
-

{b.text}

-
{b.author}
-
+ return ( +
+

{b.text}

+ {b.author && ( +
+ — {b.author} +
+ )} +
+ ); + case "CALLOUT": - return - - {b.title} - {b.text} - + return ( + + + {b.title} + {b.text} + + ); + + case "IMAGE": + return ( +
+ {b.caption + + {b.caption && ( +
+ {b.caption} +
+ )} +
+ ); + + case "LIST": + return b.style === "ORDERED" ? ( +
+ {b.items.map((item: string, index: number) => ( +

{index+1}. {item}

+ ))} +
+ ) : ( +
+ {b.items.map((item: string, index: number) => ( +

{item}

+ ))} +
+ ); + + case "TABLE": + return ( +
+ + + + {b.headers.map((header: string, index: number) => ( + {header} + ))} + + + + + {b.rows.map((row: string[], rowIndex: number) => ( + + {row.map((cell: string, cellIndex: number) => ( + {cell} + ))} + + ))} + +
+
+ ); + case "DIVIDER": - return + return ; + + case "LINK": + return + + + default: - return + return null; } } @@ -55,37 +142,56 @@ export default async function Page({ params }: { params: Promise<{ type: string; const page = await getPageBySlug(slug); if (!page || (!user?.isAdmin && page.status == "ARCHIVED")) notFound(); + + const navlink = await getNavLinkByPageId(page.id); const blocks = page ? await getBlocksByPageId(page.id) : []; const createdBy = await getUser(page?.createdBy); const updatedBy = await getUser(page?.updatedBy); + + const isNews = page.type === "NEWS"; + const isArchived = page.status === "ARCHIVED"; + const wasUpdated = formatDate(page.publishedAt) !== formatDate(page.updatedAt); + const location = (page.meta as PageMeta)?.location; + const wasUpdatedByOther = page.updatedBy && page.createdBy !== page.updatedBy; + + const meta = { + publishedAt: isNews ? formatDate(page.publishedAt) : null, + updatedAt: (wasUpdated || !isNews) ? formatDate(page.updatedAt) : null, + location: location ?? null, + author: user?.isAdmin ? `${createdBy?.firstName} ${createdBy?.secondName}` : null, + updatedBy: user?.isAdmin && wasUpdatedByOther ? `${updatedBy?.firstName} ${updatedBy?.secondName}` : null, + }; return ( <>
-
-
+
+
-

{page?.title}

-
- - - - {page.status != "ARCHIVED" && } -
+

{page?.title}

+ {user?.isAdmin && ( +
+ + + + {!isArchived && } + {!isArchived && } +
+ )}
-

{formatDate(page?.publishedAt)}

- {formatDate(page?.publishedAt) !== formatDate(page?.updatedAt) &&

Updated on {formatDate(page?.updatedAt)}

} - {(page?.meta as PageMeta)?.location &&

{(page?.meta as PageMeta)?.location}

} - {user?.isAdmin &&

{`${createdBy?.firstName} ${createdBy?.secondName}`}

} - {user?.isAdmin && page.updatedBy && page.createdBy !== page.updatedBy &&

Last update by {updatedBy?.firstName} ${updatedBy?.secondName}

} + {meta.publishedAt &&

{meta.publishedAt}

} + {meta.updatedAt &&

Updated on {meta.updatedAt}

} + {meta.location &&

{meta.location}

} + {meta.author &&

{meta.author}

} + {meta.updatedBy &&

Last update by {meta.updatedBy}

}
-
- {page.status == "ARCHIVED" && ( - +
+ {isArchived && ( + This page is archived It can be seen only by admins. Make any edits to make it visible again. diff --git a/app/[type]/[slug]/pinToggleButton.tsx b/app/[type]/[slug]/pinToggleButton.tsx new file mode 100644 index 0000000..2683d5b --- /dev/null +++ b/app/[type]/[slug]/pinToggleButton.tsx @@ -0,0 +1,23 @@ +"use client"; + +import { useTransition } from "react"; +import { Button } from "@/components/ui/button"; +import { PinIcon, PinOffIcon } from "lucide-react"; +import { Spinner } from "@/components/ui/spinner"; +import { pinPageAction, unpinPageAction } from "@/lib/navlinks/actions"; + +export function PinToggleButton({ label, pageId, isPinned }: { label: string; pageId: string; isPinned: boolean }) { + const [isPending, startTransition] = useTransition(); + + const handleClick = () => { + startTransition(() => + isPinned ? unpinPageAction(pageId) : pinPageAction(pageId) + ); + }; + + return ( + + ); +} \ No newline at end of file diff --git a/app/news/page.tsx b/app/news/page.tsx index 25266db..8483812 100644 --- a/app/news/page.tsx +++ b/app/news/page.tsx @@ -10,7 +10,11 @@ import Link from "next/link"; export default async function News() { const user = await getCurrentUser(); const news = await getPages("NEWS", "PUBLISHED"); - const drafts = await getPages("NEWS", "DRAFT"); + + const newsDrafts = await getPages("NEWS", "DRAFT"); + const pageDrafts = await getPages("PAGE", "DRAFT"); + + const drafts = [...newsDrafts, ...pageDrafts]; return ( <> diff --git a/app/page.tsx b/app/page.tsx index ca1eb44..2eef71a 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,11 +1,8 @@ import Footer from "@/components/footer"; import Header from "@/components/header"; import { NewsCard } from "@/components/newsCard"; -import { Badge } from "@/components/ui/badge" import { getPages } from "@/lib/pages/repository"; -import { PageMeta } from "@/lib/pages/types"; -import { formatDate } from "@/lib/utils"; -import { ArrowRightIcon, CalendarIcon, Globe2Icon } from "lucide-react"; +import { ArrowRightIcon } from "lucide-react"; import Link from "next/link"; export default async function Home() { @@ -17,13 +14,17 @@ export default async function Home() {

Welcome to the European Federation!

+

Become a citizen

+
-
-

Learn more about the President

-
+ +
+

Learn more about the President

+
+

Acknowledge with the Federal Government

diff --git a/components/drafts.tsx b/components/drafts.tsx index 163aba8..d71ab52 100644 --- a/components/drafts.tsx +++ b/components/drafts.tsx @@ -17,8 +17,6 @@ import { Spinner } from "@/components/ui/spinner"; type Draft = { id: string; title: string; slug: string; updatedAt: Date }; - - export default function DraftsDialog({ drafts }: { drafts: Draft[]}) { const [isDeletingId, setIsDeletingId] = useState(""); diff --git a/components/editor.tsx b/components/editor.tsx index 141899c..3c580c1 100644 --- a/components/editor.tsx +++ b/components/editor.tsx @@ -1,23 +1,19 @@ "use client"; -import { useState, useEffect } from "react"; +import { useState, useEffect, useRef } from "react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; -import { - Field, - FieldGroup, - FieldLabel, - FieldSet, -} from "@/components/ui/field"; -import { - InputGroup, - InputGroupAddon, - InputGroupButton, - InputGroupInput, -} from "@/components/ui/input-group"; +import { Field, FieldGroup, FieldLabel, FieldSet } from "@/components/ui/field"; +import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group"; import { Block, BlockType, Page, PageMeta, PageType } from "@/lib/pages/types"; -import { Globe2, Plus, Trash2, X } from "lucide-react"; +import { Globe2Icon, PlusIcon, Trash2Icon, XIcon, GripVerticalIcon, ChevronDownIcon, ImageIcon, QuoteIcon, AlertCircleIcon, MinusIcon, ListIcon, TableIcon, TypeIcon, AlignLeftIcon, InfoIcon, DotIcon, ListOrderedIcon, LinkIcon } from "lucide-react"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "./ui/select"; +import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; +import { cn } from "@/lib/utils"; +import { Input } from "./ui/input"; +import { Separator } from "./ui/separator"; +import { Alert } from "./ui/alert"; +import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; export type EditorData = { title: string; @@ -35,20 +31,42 @@ type EditorProps = { onChange: (data: EditorData) => void; }; -export default function Editor({ - page, - initialBlocks = [], - onChange, -}: EditorProps) { +const BLOCK_ICONS: Record = { + PARAGRAPH: , + HEADING: , + IMAGE: , + QUOTE: , + CALLOUT: , + DIVIDER: , + LIST: , + TABLE: , + LINK: +}; + +const BLOCK_LABELS: Record = { + PARAGRAPH: "Text", + HEADING: "Heading", + IMAGE: "Image", + QUOTE: "Quote", + CALLOUT: "Callout", + DIVIDER: "Divider", + LIST: "List", + TABLE: "Table", + LINK: "Link" +}; + +export default function Editor({ page, initialBlocks = [], onChange }: EditorProps) { const meta = (page?.meta as PageMeta) ?? {}; const [title, setTitle] = useState(page?.title ?? ""); - const [type, setType] = useState(page?.type ?? "NEWS"); + const [type, setType] = useState(page?.type ?? "NEWS"); const [slug, setSlug] = useState(page?.slug ?? ""); const [location, setLocation] = useState(meta.location ?? ""); const [tags, setTags] = useState(meta.tags ?? []); const [tagInput, setTagInput] = useState(""); const [blocks, setBlocks] = useState(initialBlocks); + const [dragIndex, setDragIndex] = useState(null); + const [dropIndex, setDropIndex] = useState(null); useEffect(() => { onChange({ @@ -72,16 +90,16 @@ export default function Editor({ setTags(tags.filter((t) => t !== tag)); } - function addBlock(type: BlockType) { + function addBlock(blockType: BlockType) { setBlocks([ ...blocks, { id: crypto.randomUUID(), pageId: page?.id ?? "", parentId: null, - type, + type: blockType, position: blocks.length, - content: getDefaultContent(type), + content: getDefaultContent(blockType), }, ]); } @@ -94,10 +112,32 @@ export default function Editor({ setBlocks(blocks.map((b) => (b.id === id ? { ...b, content } : b))); } + function moveBlock(from: number, to: number) { + const updated = [...blocks]; + const [moved] = updated.splice(from, 1); + updated.splice(to, 0, moved); + setBlocks(updated.map((b, i) => ({ ...b, position: i }))); + } + + function handleDragStart(index: number) { + setDragIndex(index); + } + + function handleDragOver(e: React.DragEvent, index: number) { + e.preventDefault(); + setDropIndex(index); + } + + function handleDrop(index: number) { + if (dragIndex !== null && dragIndex !== index) moveBlock(dragIndex, index); + setDragIndex(null); + setDropIndex(null); + } + return (
-

Meta

+

Meta

Title @@ -105,21 +145,17 @@ export default function Editor({ { setTitle(e.target.value); - if (!page) { - setSlug( - e.target.value.toLowerCase().replace(/\s+/g, "-") - ); - } + if (!page) setSlug(e.target.value.toLowerCase().replace(/\s+/g, "-")); }} - placeholder="Enter page title" /> Page type - setType(e as PageType)}> @@ -134,122 +170,105 @@ export default function Editor({ Slug - setSlug(e.target.value)} - placeholder="page-slug" - disabled={true} - /> + setSlug(e.target.value)} /> - - Location - - - - - setLocation(e.target.value)} - placeholder="Liberty City, European Federation" - /> - - - - Tags - - setTagInput(e.target.value)} - onKeyDown={(e) => e.key === "Enter" && addTag()} - placeholder="Add a tag and press Enter" - /> - - Add - - - {tags.length > 0 && ( -
- {tags.map((tag) => ( - removeTag(tag)} - > - {tag} - - - ))} -
- )} -
+ {type === "NEWS" && ( + + Location + + + + + setLocation(e.target.value)} /> + + + )} + {type === "NEWS" && ( + + Tags + + setTagInput(e.target.value)} + onKeyDown={(e) => e.key === "Enter" && addTag()} + /> + + Add + + + {tags.length > 0 && ( +
+ {tags.map((tag) => ( + removeTag(tag)}> + {tag} + + + ))} +
+ )} +
+ )}
-
-

Content

-
- {blocks.length === 0 && ( -

- No blocks yet. Add one below. -

- )} - {blocks.map((block) => ( - +

Content

+ {blocks.length === 0 && ( +

No blocks yet. Add one below.

+ )} +
+ {blocks.map((block, index) => ( +
updateBlock(block.id, content)} - onRemove={() => removeBlock(block.id)} - /> - ))} -
-
- {( - [ - "PARAGRAPH", - "HEADING", - "IMAGE", - "QUOTE", - "CALLOUT", - "DIVIDER", - "LIST", - "TABLE", - ] as BlockType[] - ).map((t) => ( - +
+ updateBlock(block.id, content)} /> +
+
+ + +
+
))}
-
-
- ); -} -function BlockEditor({ block, onChange, onRemove }: { block: Block; onChange: (content: Block["content"]) => void; onRemove: () => void }) { - return ( -
-
- +
+ + + + + + {(["PARAGRAPH", "HEADING", "IMAGE", "QUOTE", "CALLOUT", "DIVIDER", "LIST", "TABLE", "LINK"] as BlockType[]).map((t) => ( + addBlock(t)} className="gap-2"> +

{BLOCK_ICONS[t]}

+

{BLOCK_LABELS[t]}

+
+ ))} +
+
+
-
); } @@ -258,111 +277,346 @@ function BlockInput({ block, onChange }: { block: Block; onChange: (c: Block["co switch (block.type) { case "PARAGRAPH": return ( -