feat: pages
This commit is contained in:
@@ -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 ? <Spinner /> : <ArchiveIcon />}
|
||||
</Button>
|
||||
|
||||
@@ -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}`);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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 (
|
||||
|
||||
+143
-37
@@ -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 <h4>{b.text}</h4>
|
||||
return (
|
||||
<p className="leading-relaxed whitespace-pre-wrap">
|
||||
{b.text}
|
||||
</p>
|
||||
);
|
||||
|
||||
case "HEADING":
|
||||
switch (b.level) {
|
||||
case 1:
|
||||
return <h1>{b.text}</h1>
|
||||
return <h1>{b.text}</h1>;
|
||||
case 2:
|
||||
return <h2>{b.text}</h2>
|
||||
return <h2>{b.text}</h2>;
|
||||
case 3:
|
||||
return <h3>{b.text}</h3>
|
||||
return <h3>{b.text}</h3>;
|
||||
default:
|
||||
return <h2>{b.text}</h2>;
|
||||
}
|
||||
|
||||
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>
|
||||
return (
|
||||
<div className="flex flex-col gap-2 border-l-2 border-muted-foreground pl-4">
|
||||
<p className="italic">{b.text}</p>
|
||||
{b.author && (
|
||||
<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>
|
||||
return (
|
||||
<Alert className="w-max">
|
||||
<InfoIcon />
|
||||
<AlertTitle>{b.title}</AlertTitle>
|
||||
<AlertDescription>{b.text}</AlertDescription>
|
||||
</Alert>
|
||||
);
|
||||
|
||||
case "IMAGE":
|
||||
return (
|
||||
<figure className="flex flex-col gap-2">
|
||||
<Image
|
||||
src={b.url}
|
||||
alt={b.caption || "Image"}
|
||||
width={1200}
|
||||
height={800}
|
||||
className="rounded-md border max-w-200 h-auto object-contain"
|
||||
/>
|
||||
|
||||
{b.caption && (
|
||||
<figcaption className="text-sm text-muted-foreground">
|
||||
{b.caption}
|
||||
</figcaption>
|
||||
)}
|
||||
</figure>
|
||||
);
|
||||
|
||||
case "LIST":
|
||||
return b.style === "ORDERED" ? (
|
||||
<div className="flex flex-col gap-2">
|
||||
{b.items.map((item: string, index: number) => (
|
||||
<p className="flex flex-row gap-1 items-center" key={index}><span className="text-muted-foreground">{index+1}.</span> {item}</p>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex flex-col gap-2">
|
||||
{b.items.map((item: string, index: number) => (
|
||||
<p className="flex flex-row gap-1 items-center" key={index}><DotIcon className="text-muted-foreground" /> {item}</p>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
|
||||
case "TABLE":
|
||||
return (
|
||||
<div className="overflow-x-auto">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
{b.headers.map((header: string, index: number) => (
|
||||
<TableHead key={index}>{header}</TableHead>
|
||||
))}
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
|
||||
<TableBody>
|
||||
{b.rows.map((row: string[], rowIndex: number) => (
|
||||
<TableRow key={rowIndex}>
|
||||
{row.map((cell: string, cellIndex: number) => (
|
||||
<TableCell key={cellIndex}>{cell}</TableCell>
|
||||
))}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
);
|
||||
|
||||
case "DIVIDER":
|
||||
return <Separator />
|
||||
return <Separator />;
|
||||
|
||||
case "LINK":
|
||||
return <Link href={b.href} target="_blank">
|
||||
<Button variant="secondary" className="w-max">{b.label}</Button>
|
||||
</Link>
|
||||
|
||||
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 (
|
||||
<>
|
||||
<Header />
|
||||
<div className="flex flex-col p-16 gap-16">
|
||||
<div className="flex flex-col gap-4 items-center justify-center">
|
||||
<div className="flex flex-col p-16 gap-12">
|
||||
<div className={cn("flex flex-col gap-4 justify-center", isNews && "items-center")}>
|
||||
<div className="flex flex-row items-center gap-6">
|
||||
<h1 className="underline underline-offset-8 decoration-2 text-center">{page?.title}</h1>
|
||||
<div className="flex flex-row gap-2 items-center">
|
||||
<Link href={`/news/${page.slug}/edit`}>
|
||||
<Button variant="secondary" size="icon-sm"><PencilIcon /></Button>
|
||||
</Link>
|
||||
{page.status != "ARCHIVED" && <ArchiveButton pageId={page.id} />}
|
||||
</div>
|
||||
<h1 className="underline underline-offset-8 decoration-2">{page?.title}</h1>
|
||||
{user?.isAdmin && (
|
||||
<div className="flex flex-row gap-2 items-center">
|
||||
<Link href={`/${page.type.toLowerCase()}/${page.slug}/edit`}>
|
||||
<Button variant="secondary" size="icon-sm"><PencilIcon /></Button>
|
||||
</Link>
|
||||
{!isArchived && <ArchiveButton pageId={page.id} slug={page.slug} type={page.type} />}
|
||||
{!isArchived && <PinToggleButton label={page.title} pageId={page.id} isPinned={!!navlink} />}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex flex-row gap-6 items-center">
|
||||
<p className="flex gap-1.5 items-center"><CalendarIcon size={16} />{formatDate(page?.publishedAt)}</p>
|
||||
{formatDate(page?.publishedAt) !== formatDate(page?.updatedAt) && <p className="flex gap-2 items-center"><CalendarIcon size={16} />Updated on {formatDate(page?.updatedAt)}</p>}
|
||||
{(page?.meta as PageMeta)?.location && <p className="flex gap-1.5 items-center"><Globe2Icon size={16} />{(page?.meta as PageMeta)?.location}</p>}
|
||||
{user?.isAdmin && <p className="flex gap-1.5 items-center"><UserIcon size={16} />{`${createdBy?.firstName} ${createdBy?.secondName}`}</p>}
|
||||
{user?.isAdmin && page.updatedBy && page.createdBy !== page.updatedBy && <p className="flex gap-2 items-center"><CalendarIcon size={18} />Last update by {updatedBy?.firstName} ${updatedBy?.secondName}</p>}
|
||||
{meta.publishedAt && <p className="flex gap-1.5 items-center"><CalendarIcon size={16} />{meta.publishedAt}</p>}
|
||||
{meta.updatedAt && <p className="flex gap-1.5 items-center"><CalendarIcon size={16} />Updated on {meta.updatedAt}</p>}
|
||||
{meta.location && <p className="flex gap-1.5 items-center"><Globe2Icon size={16} />{meta.location}</p>}
|
||||
{meta.author && <p className="flex gap-1.5 items-center"><UserIcon size={16} />{meta.author}</p>}
|
||||
{meta.updatedBy && <p className="flex gap-1.5 items-center"><UserIcon size={16} />Last update by {meta.updatedBy}</p>}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col gap-8">
|
||||
{page.status == "ARCHIVED" && (
|
||||
<Alert>
|
||||
<div className="flex flex-col gap-6">
|
||||
{isArchived && (
|
||||
<Alert className="w-max">
|
||||
<InfoIcon />
|
||||
<AlertTitle>This page is archived</AlertTitle>
|
||||
<AlertDescription>It can be seen only by admins. Make any edits to make it visible again.</AlertDescription>
|
||||
|
||||
@@ -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 (
|
||||
<Button variant={isPinned ? "secondary" : "secondary"} size="icon-sm" disabled={isPending} onClick={handleClick}>
|
||||
{isPending ? <Spinner /> : isPinned ? <PinOffIcon /> : <PinIcon />}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
+5
-1
@@ -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 (
|
||||
<>
|
||||
|
||||
+8
-7
@@ -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() {
|
||||
<div className="flex flex-col p-16 gap-16">
|
||||
<h1>Welcome to the <span className="underline underline-offset-8 decoration-2">European Federation</span>!</h1>
|
||||
<div className="grid grid-cols-[2fr_1fr_1fr_1fr] gap-8">
|
||||
<Link href="/page/citizenship">
|
||||
<div className="flex items-end h-64 p-4 rounded-lg bg-[#00a2e8] text-white">
|
||||
<h1 className="flex flex-row items-center gap-2 hover:gap-4 hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">Become a citizen<ArrowRightIcon size={30} /></h1>
|
||||
</div>
|
||||
</Link>
|
||||
<div className="grid grid-rows-2 gap-8">
|
||||
<div className="flex items-end h-28 p-2.5 rounded-lg bg-muted">
|
||||
<h2 className="flex flex-row items-center gap-2 hover:gap-4 hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">Learn more about the President</h2>
|
||||
</div>
|
||||
<Link href="/page/president">
|
||||
<div className="flex items-end h-28 p-2.5 rounded-lg bg-muted">
|
||||
<h2 className="flex flex-row items-center gap-2 hover:gap-4 hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">Learn more about the President</h2>
|
||||
</div>
|
||||
</Link>
|
||||
<div className="flex items-end h-28 p-2.5 rounded-lg bg-muted">
|
||||
<h2 className="flex flex-row items-center gap-2 hover:gap-4 hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">Acknowledge with the Federal Government</h2>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user