feat: improved news
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useTransition } from "react";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { ArchiveIcon } from "lucide-react";
|
||||||
|
import { Spinner } from "@/components/ui/spinner";
|
||||||
|
import { archivePageAction } from "@/lib/pages/actions";
|
||||||
|
|
||||||
|
export function ArchiveButton({ pageId }: { pageId: string }) {
|
||||||
|
const [isPending, startTransition] = useTransition();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
variant="secondary"
|
||||||
|
size="icon-sm"
|
||||||
|
disabled={isPending}
|
||||||
|
onClick={() => startTransition(() => archivePageAction(pageId))}
|
||||||
|
>
|
||||||
|
{isPending ? <Spinner /> : <ArchiveIcon />}
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -47,24 +47,14 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P
|
|||||||
return (
|
return (
|
||||||
<div className="flex flex-col p-16 gap-12">
|
<div className="flex flex-col p-16 gap-12">
|
||||||
<div className="flex flex-row items-center justify-between">
|
<div className="flex flex-row items-center justify-between">
|
||||||
<h1 className="underline underline-offset-8 decoration-2">
|
<h1 className="underline underline-offset-8 decoration-2">Edit page</h1>
|
||||||
{page ? "Edit page" : "Create page"}
|
|
||||||
</h1>
|
|
||||||
<div className="flex flex-row gap-3">
|
<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
|
<Button
|
||||||
disabled={isPending}
|
disabled={isPending}
|
||||||
onClick={() => handleSave("PUBLISHED")}
|
onClick={() => handleSave("PUBLISHED")}
|
||||||
>
|
>
|
||||||
{isPending && <Loader2 size={14} className="animate-spin mr-1" />}
|
{isPending && <Loader2 size={14} className="animate-spin mr-1" />}
|
||||||
Publish
|
Save
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
import Header from "@/components/header";
|
import Header from "@/components/header";
|
||||||
import Footer from "@/components/footer";
|
import Footer from "@/components/footer";
|
||||||
import { getPageBySlug, getBlocksByPageId } from "@/lib/pages/repository";
|
import { getPageBySlug, getBlocksByPageId } from "@/lib/pages/repository";
|
||||||
import { getCurrentUser } from "@/lib/user";
|
import { getCurrentUser, getUser } from "@/lib/user";
|
||||||
import { notFound } from "next/navigation";
|
import { notFound } from "next/navigation";
|
||||||
import { Block, PAGE_TYPES, PageMeta, PageType } from "@/lib/pages/types";
|
import { Block, PAGE_TYPES, PageMeta, PageType } from "@/lib/pages/types";
|
||||||
import { formatDate } from "@/lib/utils";
|
import { formatDate } from "@/lib/utils";
|
||||||
import { CalendarIcon, Globe2Icon, InfoIcon } from "lucide-react";
|
import { ArchiveIcon, CalendarIcon, Globe2Icon, InfoIcon, PencilIcon, UserIcon } from "lucide-react";
|
||||||
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
||||||
import { Separator } from "@/components/ui/separator";
|
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";
|
||||||
|
|
||||||
function BlockRenderer({ block }: {block: Block }) {
|
function BlockRenderer({ block }: {block: Block }) {
|
||||||
const b = block.content
|
const b = block.content
|
||||||
@@ -50,23 +54,43 @@ export default async function Page({ params }: { params: Promise<{ type: string;
|
|||||||
if (!PAGE_TYPES.includes(pageType)) notFound();
|
if (!PAGE_TYPES.includes(pageType)) notFound();
|
||||||
|
|
||||||
const page = await getPageBySlug(slug);
|
const page = await getPageBySlug(slug);
|
||||||
if (!page) notFound();
|
if (!page || (!user?.isAdmin && page.status == "ARCHIVED")) notFound();
|
||||||
|
|
||||||
const blocks = page ? await getBlocksByPageId(page.id) : [];
|
const blocks = page ? await getBlocksByPageId(page.id) : [];
|
||||||
|
|
||||||
|
const createdBy = await getUser(page?.createdBy);
|
||||||
|
const updatedBy = await getUser(page?.updatedBy);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Header />
|
<Header />
|
||||||
<div className="flex flex-col p-16 gap-16">
|
<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 gap-4 items-center justify-center">
|
||||||
|
<div className="flex flex-row items-center gap-6">
|
||||||
<h1 className="underline underline-offset-8 decoration-2 text-center">{page?.title}</h1>
|
<h1 className="underline underline-offset-8 decoration-2 text-center">{page?.title}</h1>
|
||||||
<div className="flex flex-row gap-4 items-center">
|
<div className="flex flex-row gap-2 items-center">
|
||||||
<h3 className="flex gap-2 items-baseline"><CalendarIcon size={18} />{formatDate(page?.publishedAt)}</h3>
|
<Link href={`/news/${page.slug}/edit`}>
|
||||||
{formatDate(page?.publishedAt) !== formatDate(page?.updatedAt) && <h3 className="flex gap-2 items-baseline"><CalendarIcon size={18} />Updated on {formatDate(page?.updatedAt)}</h3>}
|
<Button variant="secondary" size="icon-sm"><PencilIcon /></Button>
|
||||||
{<h3 className="flex gap-2 items-baseline"><Globe2Icon size={18} />{(page?.meta as PageMeta)?.location}</h3>}
|
</Link>
|
||||||
|
{page.status != "ARCHIVED" && <ArchiveButton pageId={page.id} />}
|
||||||
|
</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>}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-col gap-8">
|
<div className="flex flex-col gap-8">
|
||||||
|
{page.status == "ARCHIVED" && (
|
||||||
|
<Alert>
|
||||||
|
<InfoIcon />
|
||||||
|
<AlertTitle>This page is archived</AlertTitle>
|
||||||
|
<AlertDescription>It can be seen only by admins. Make any edits to make it visible again.</AlertDescription>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
{blocks.map((block) => (
|
{blocks.map((block) => (
|
||||||
<BlockRenderer key={block.id} block={block} />
|
<BlockRenderer key={block.id} block={block} />
|
||||||
))}
|
))}
|
||||||
|
|||||||
+8
-20
@@ -1,13 +1,10 @@
|
|||||||
import DraftsDialog from "@/components/drafts";
|
import DraftsDialog from "@/components/drafts";
|
||||||
import Footer from "@/components/footer";
|
import Footer from "@/components/footer";
|
||||||
import Header from "@/components/header";
|
import Header from "@/components/header";
|
||||||
import { Badge } from "@/components/ui/badge"
|
import { NewsCard } from "@/components/newsCard";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { getPages } from "@/lib/pages/repository";
|
import { getPages } from "@/lib/pages/repository";
|
||||||
import { PageMeta } from "@/lib/pages/types";
|
|
||||||
import { getCurrentUser } from "@/lib/user";
|
import { getCurrentUser } from "@/lib/user";
|
||||||
import { formatDate } from "@/lib/utils";
|
|
||||||
import { ArrowRight, Calendar, Globe2 } from "lucide-react";
|
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
|
|
||||||
export default async function News() {
|
export default async function News() {
|
||||||
@@ -21,29 +18,20 @@ export default async function News() {
|
|||||||
<div className="flex flex-col p-16 gap-16">
|
<div className="flex flex-col p-16 gap-16">
|
||||||
<div className="flex flex-row items-center justify-between">
|
<div className="flex flex-row items-center justify-between">
|
||||||
<h1 className="underline underline-offset-8 decoration-2">News</h1>
|
<h1 className="underline underline-offset-8 decoration-2">News</h1>
|
||||||
|
<div className="flex flex-row items-center gap-4">
|
||||||
|
{user?.isAdmin && (
|
||||||
<div className="flex flex-row items-center gap-4">
|
<div className="flex flex-row items-center gap-4">
|
||||||
<DraftsDialog drafts={drafts} />
|
<DraftsDialog drafts={drafts} />
|
||||||
{user?.isAdmin && (<Link href="/create/"><Button variant="secondary">Create a new page</Button></Link>)}
|
<Link href="/create/"><Button variant="secondary">Create a new page</Button></Link>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-col gap-6">
|
<div className="flex flex-col gap-6">
|
||||||
{
|
{
|
||||||
news.length > 0 ? news.map((n) => (
|
news.length > 0 ? news.map((n) => (
|
||||||
<div className="flex flex-col gap-2" key={n.id}>
|
<NewsCard news={n} 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>
|
)) : <h2 className="text-muted-foreground">There are no news yet...</h2>
|
||||||
<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>
|
|
||||||
), (
|
|
||||||
<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>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+6
-16
@@ -1,10 +1,11 @@
|
|||||||
import Footer from "@/components/footer";
|
import Footer from "@/components/footer";
|
||||||
import Header from "@/components/header";
|
import Header from "@/components/header";
|
||||||
|
import { NewsCard } from "@/components/newsCard";
|
||||||
import { Badge } from "@/components/ui/badge"
|
import { Badge } from "@/components/ui/badge"
|
||||||
import { getPages } from "@/lib/pages/repository";
|
import { getPages } from "@/lib/pages/repository";
|
||||||
import { PageMeta } from "@/lib/pages/types";
|
import { PageMeta } from "@/lib/pages/types";
|
||||||
import { formatDate } from "@/lib/utils";
|
import { formatDate } from "@/lib/utils";
|
||||||
import { ArrowRight, Calendar, Globe2 } from "lucide-react";
|
import { ArrowRightIcon, CalendarIcon, Globe2Icon } from "lucide-react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
|
|
||||||
export default async function Home() {
|
export default async function Home() {
|
||||||
@@ -17,7 +18,7 @@ export default async function Home() {
|
|||||||
<h1>Welcome to the <span className="underline underline-offset-8 decoration-2">European Federation</span>!</h1>
|
<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">
|
<div className="grid grid-cols-[2fr_1fr_1fr_1fr] gap-8">
|
||||||
<div className="flex items-end h-64 p-4 rounded-lg bg-[#00a2e8] text-white">
|
<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<ArrowRight size={32} /></h1>
|
<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>
|
</div>
|
||||||
<div className="grid grid-rows-2 gap-8">
|
<div className="grid grid-rows-2 gap-8">
|
||||||
<div className="flex items-end h-28 p-2.5 rounded-lg bg-muted">
|
<div className="flex items-end h-28 p-2.5 rounded-lg bg-muted">
|
||||||
@@ -41,22 +42,11 @@ export default async function Home() {
|
|||||||
<div className="flex flex-col gap-6">
|
<div className="flex flex-col gap-6">
|
||||||
{
|
{
|
||||||
news.length > 0 ? news.map((n) => (
|
news.length > 0 ? news.map((n) => (
|
||||||
<div className="flex flex-col gap-2" key={n.id}>
|
<NewsCard news={n} 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>
|
)) : <h2 className="text-muted-foreground">There are no news yet...</h2>
|
||||||
<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>
|
|
||||||
)) : <h2>There are no news yet...</h2>
|
|
||||||
}
|
}
|
||||||
</div>
|
</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>
|
<Link href="/news"><h3 className="w-max flex flex-row items-center gap-2 hover:gap-2.5 hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">See all news<ArrowRightIcon size={20} /></h3></Link>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Footer />
|
<Footer />
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ export default function DraftsDialog({ drafts }: { drafts: Draft[]}) {
|
|||||||
<div className="flex flex-col items-center gap-2">
|
<div className="flex flex-col items-center gap-2">
|
||||||
{drafts.map((draft) => (
|
{drafts.map((draft) => (
|
||||||
<div className="w-full flex flex-row items-center justify-between" key={draft.id}>
|
<div className="w-full flex flex-row items-center justify-between" key={draft.id}>
|
||||||
<h3 className="w-64 hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer truncate">
|
<h3 className="w-64 truncate">
|
||||||
{draft.title}
|
{draft.title}
|
||||||
</h3>
|
</h3>
|
||||||
<div className="flex flex-row items-center gap-2">
|
<div className="flex flex-row items-center gap-2">
|
||||||
@@ -61,7 +61,7 @@ export default function DraftsDialog({ drafts }: { drafts: Draft[]}) {
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<h4>There are no drafts yet...</h4>
|
<h4 className="text-muted-foreground">There are no drafts yet...</h4>
|
||||||
)}
|
)}
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ export default function Editor({
|
|||||||
</Field>
|
</Field>
|
||||||
<Field>
|
<Field>
|
||||||
<FieldLabel htmlFor="type">Page type</FieldLabel>
|
<FieldLabel htmlFor="type">Page type</FieldLabel>
|
||||||
<Select defaultValue={type} onValueChange={(e) => setType(e)}>
|
<Select defaultValue={type} onValueChange={(e) => setType(e)} disabled={true}>
|
||||||
<SelectTrigger>
|
<SelectTrigger>
|
||||||
<SelectValue placeholder="Type" />
|
<SelectValue placeholder="Type" />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
@@ -139,6 +139,7 @@ export default function Editor({
|
|||||||
value={slug}
|
value={slug}
|
||||||
onChange={(e) => setSlug(e.target.value)}
|
onChange={(e) => setSlug(e.target.value)}
|
||||||
placeholder="page-slug"
|
placeholder="page-slug"
|
||||||
|
disabled={true}
|
||||||
/>
|
/>
|
||||||
</InputGroup>
|
</InputGroup>
|
||||||
</Field>
|
</Field>
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { Page, PageMeta } from "@/lib/pages/types";
|
||||||
|
import { CalendarIcon, Globe2Icon } from "lucide-react";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { Badge } from "./ui/badge";
|
||||||
|
import { formatDate } from "@/lib/utils";
|
||||||
|
|
||||||
|
export function NewsCard({ news }: { news: Page }) {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<Link href={`/news/${news.slug}`} className="w-max">
|
||||||
|
<h2 className="hover:underline underline-offset-8 decoration-2 duration-200 cursor-pointer">{news.title}</h2>
|
||||||
|
</Link>
|
||||||
|
<div className="flex flex-row gap-8 items-center">
|
||||||
|
<p className="flex gap-2 items-center"><CalendarIcon size={16} />{formatDate(news.publishedAt)}</p>
|
||||||
|
{(news?.meta as PageMeta)?.location && <p className="flex gap-2 items-center"><Globe2Icon size={16} />{(news.meta as PageMeta)?.location}</p>}
|
||||||
|
<div className="flex flex-row gap-4">
|
||||||
|
{((news.meta as PageMeta)?.tags)?.map((tag) => (
|
||||||
|
<Badge key={tag}>{tag}</Badge>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
+18
-7
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { Block, PageMeta, PageStatus, PageType } from "./types";
|
import { Block, PageMeta, PageStatus, PageType } from "./types";
|
||||||
import { createPage, deletePage, updatePage } from "./repository";
|
import { archivePage, createPage, deletePage, updatePage } from "./repository";
|
||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
export async function savePageAction(params: {
|
export async function savePageAction(params: {
|
||||||
@@ -14,18 +14,29 @@ export async function savePageAction(params: {
|
|||||||
blocks: Block[],
|
blocks: Block[],
|
||||||
meta?: PageMeta
|
meta?: PageMeta
|
||||||
}) {
|
}) {
|
||||||
|
const basePath = params.type === "NEWS" ? "news" : "pages";
|
||||||
|
|
||||||
if (params.id) {
|
if (params.id) {
|
||||||
await updatePage(params.id, params.title, params.type, params.status, params.blocks, params.meta);
|
await updatePage(params.id, params.title, params.type, params.status, params.blocks, params.meta);
|
||||||
revalidatePath(`/${params.slug}`);
|
revalidatePath(`/${basePath}/${params.slug}`);
|
||||||
revalidatePath("/news");
|
revalidatePath(`/${basePath}`);
|
||||||
} else {
|
} else {
|
||||||
const page = await createPage(params.slug, params.title, params.type, params.status, params.blocks, params.meta);
|
const page = await createPage(params.slug, params.title, params.type, params.status, params.blocks, params.meta);
|
||||||
revalidatePath("/news");
|
revalidatePath(`/${basePath}`);
|
||||||
redirect(`/news/${page.slug}/edit`);
|
redirect(`/${basePath}/${page.slug}/edit`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deletePageAction(id: string) {
|
export async function archivePageAction(id: string, slug: string, type: PageType) {
|
||||||
|
await archivePage(id);
|
||||||
|
const basePath = type === "NEWS" ? "news" : "pages";
|
||||||
|
revalidatePath(`/${basePath}/${slug}`);
|
||||||
|
revalidatePath(`/${basePath}`);
|
||||||
|
redirect(`/${basePath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deletePageAction(id: string, type: PageType) {
|
||||||
await deletePage(id);
|
await deletePage(id);
|
||||||
revalidatePath("/news");
|
const basePath = type === "NEWS" ? "news" : "pages";
|
||||||
|
revalidatePath(`/${basePath}`);
|
||||||
}
|
}
|
||||||
+9
-43
@@ -37,7 +37,7 @@ export async function createPage(slug: string, title: string, type: PageType, st
|
|||||||
status,
|
status,
|
||||||
meta: meta ?? {},
|
meta: meta ?? {},
|
||||||
publishedAt: status === "PUBLISHED" ? new Date() : null,
|
publishedAt: status === "PUBLISHED" ? new Date() : null,
|
||||||
createdBy: user.id,
|
createdBy: user.id
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -53,44 +53,13 @@ export async function createPage(slug: string, title: string, type: PageType, st
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status === "PUBLISHED") {
|
|
||||||
await prisma.pageVersion.create({
|
|
||||||
data: {
|
|
||||||
pageId: page.id,
|
|
||||||
title: page.title,
|
|
||||||
blocks: blocks,
|
|
||||||
createdBy: user.id,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function restoreVersion(pageId: string, versionId: string) {
|
|
||||||
const version = await prisma.pageVersion.findUnique({
|
|
||||||
where: { id: versionId }
|
|
||||||
});
|
|
||||||
|
|
||||||
await prisma.$transaction([
|
|
||||||
prisma.block.deleteMany({ where: { pageId } }),
|
|
||||||
prisma.block.createMany({
|
|
||||||
data: (version?.blocks as Block[]).map(block => ({
|
|
||||||
...block,
|
|
||||||
pageId,
|
|
||||||
id: crypto.randomUUID(),
|
|
||||||
}))
|
|
||||||
}),
|
|
||||||
prisma.page.update({
|
|
||||||
where: { id: pageId },
|
|
||||||
data: { updatedAt: new Date() }
|
|
||||||
})
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function updatePage(
|
export async function updatePage(
|
||||||
id: string,
|
id: string,
|
||||||
title: string,
|
title: string,
|
||||||
|
type: PageType,
|
||||||
status: PageStatus,
|
status: PageStatus,
|
||||||
blocks: Block[],
|
blocks: Block[],
|
||||||
meta?: PageMeta
|
meta?: PageMeta
|
||||||
@@ -102,10 +71,12 @@ export async function updatePage(
|
|||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
title,
|
title,
|
||||||
|
type,
|
||||||
status,
|
status,
|
||||||
meta: meta ?? {},
|
meta: meta ?? {},
|
||||||
publishedAt: status === "PUBLISHED" ? new Date() : undefined,
|
publishedAt: status === "PUBLISHED" ? new Date() : undefined,
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
|
updatedBy: user.id
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -124,18 +95,13 @@ export async function updatePage(
|
|||||||
: []),
|
: []),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (status === "PUBLISHED") {
|
return page;
|
||||||
await prisma.pageVersion.create({
|
|
||||||
data: {
|
|
||||||
pageId: id,
|
|
||||||
title: page.title,
|
|
||||||
blocks: blocks as any,
|
|
||||||
createdBy: user.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return page;
|
export async function archivePage(id: string) {
|
||||||
|
const user = await getCurrentUser();
|
||||||
|
if (!user?.isAdmin) throw new Error("Access denied");
|
||||||
|
await prisma.page.update({ where: { id }, data: { status: "ARCHIVED", updatedBy: user.id } });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deletePage(id: string) {
|
export async function deletePage(id: string) {
|
||||||
|
|||||||
@@ -40,12 +40,3 @@ export type Block = {
|
|||||||
position: number;
|
position: number;
|
||||||
content: BlockContent;
|
content: BlockContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PageVersion = {
|
|
||||||
id: string;
|
|
||||||
pageId: string;
|
|
||||||
title: string;
|
|
||||||
blocks: Block[];
|
|
||||||
createdAt: Date;
|
|
||||||
createdBy: string;
|
|
||||||
};
|
|
||||||
@@ -6,3 +6,8 @@ export async function getCurrentUser() {
|
|||||||
if (!userId) return null;
|
if (!userId) return null;
|
||||||
return findUserById(userId);
|
return findUserById(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getUser(id: string) {
|
||||||
|
if (!id) return null;
|
||||||
|
return findUserById(id);
|
||||||
|
}
|
||||||
+13
-18
@@ -12,19 +12,24 @@ datasource db {
|
|||||||
provider = "postgresql"
|
provider = "postgresql"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Sex {
|
||||||
|
MALE
|
||||||
|
FEMALE
|
||||||
|
}
|
||||||
|
|
||||||
model User {
|
model User {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
kotosoftId String @unique
|
kotosoftId String @unique
|
||||||
email String @unique
|
email String @unique
|
||||||
firstName String
|
firstName String
|
||||||
secondName String
|
secondName String
|
||||||
sex String
|
sex Sex
|
||||||
birthDate DateTime
|
birthDate DateTime
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
isAdmin Boolean @default(false)
|
isAdmin Boolean @default(false)
|
||||||
|
|
||||||
pages Page[]
|
pages Page[] @relation("PageAuthor")
|
||||||
versions PageVersion[]
|
editedPages Page[] @relation("PageEditor")
|
||||||
}
|
}
|
||||||
|
|
||||||
enum PageStatus {
|
enum PageStatus {
|
||||||
@@ -34,6 +39,7 @@ enum PageStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum PageType {
|
enum PageType {
|
||||||
|
PAGE
|
||||||
NEWS
|
NEWS
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,10 +55,12 @@ model Page {
|
|||||||
publishedAt DateTime?
|
publishedAt DateTime?
|
||||||
|
|
||||||
createdBy String
|
createdBy String
|
||||||
author User @relation(fields: [createdBy], references: [id])
|
author User @relation("PageAuthor", fields: [createdBy], references: [id])
|
||||||
|
|
||||||
|
updatedBy String?
|
||||||
|
editor User? @relation("PageEditor", fields: [updatedBy], references: [id])
|
||||||
|
|
||||||
blocks Block[]
|
blocks Block[]
|
||||||
versions PageVersion[]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
model Block {
|
model Block {
|
||||||
@@ -70,16 +78,3 @@ model Block {
|
|||||||
|
|
||||||
@@index([pageId, position])
|
@@index([pageId, position])
|
||||||
}
|
}
|
||||||
|
|
||||||
model PageVersion {
|
|
||||||
id String @id @default(uuid())
|
|
||||||
pageId String
|
|
||||||
page Page @relation(fields: [pageId], references: [id], onDelete: Cascade)
|
|
||||||
|
|
||||||
title String
|
|
||||||
blocks Json
|
|
||||||
|
|
||||||
createdAt DateTime @default(now())
|
|
||||||
createdBy String
|
|
||||||
author User @relation(fields: [createdBy], references: [id])
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user