-
{page?.title}
-
-
{formatDate(page?.publishedAt)}
- {formatDate(page?.publishedAt) !== formatDate(page?.updatedAt) &&
Updated on {formatDate(page?.updatedAt)}}
- {
{(page?.meta as PageMeta)?.location}}
+
+
{page?.title}
+
+
+
+
+ {page.status != "ARCHIVED" &&
}
+
+
+
+
{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}
}
+ {page.status == "ARCHIVED" && (
+
+
+ This page is archived
+ It can be seen only by admins. Make any edits to make it visible again.
+
+ )}
{blocks.map((block) => (
))}
diff --git a/app/news/page.tsx b/app/news/page.tsx
index ab29821..25266db 100644
--- a/app/news/page.tsx
+++ b/app/news/page.tsx
@@ -1,13 +1,10 @@
import DraftsDialog from "@/components/drafts";
import Footer from "@/components/footer";
import Header from "@/components/header";
-import { Badge } from "@/components/ui/badge"
+import { NewsCard } from "@/components/newsCard";
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";
export default async function News() {
@@ -22,28 +19,19 @@ export default async function News() {
News
-
- {user?.isAdmin && (
Create a new page )}
+ {user?.isAdmin && (
+
+
+ Create a new page
+
+ )}
{
news.length > 0 ? news.map((n) => (
-
-
{n.title}
-
-
{formatDate(n.publishedAt)}
-
{(n.meta as PageMeta)?.location}
-
- {((n.meta as PageMeta)?.tags)?.map((tag) => (
- {tag}
- ))}
-
-
-
- ), (
-
See all news
- )) :
There are no news yet...
+
+ )) :
There are no news yet...
}
diff --git a/app/page.tsx b/app/page.tsx
index 0035114..ca1eb44 100644
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -1,10 +1,11 @@
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 { ArrowRight, Calendar, Globe2 } from "lucide-react";
+import { ArrowRightIcon, CalendarIcon, Globe2Icon } from "lucide-react";
import Link from "next/link";
export default async function Home() {
@@ -17,7 +18,7 @@ export default async function Home() {
Welcome to the European Federation !
-
Become a citizen
+
Become a citizen
@@ -41,22 +42,11 @@ export default async function Home() {
{
news.length > 0 ? news.map((n) => (
-
-
{n.title}
-
-
{formatDate(n.publishedAt)}
-
{(n.meta as PageMeta)?.location}
-
- {((n.meta as PageMeta)?.tags)?.map((tag) => (
- {tag}
- ))}
-
-
-
- )) :
There are no news yet...
+
+ )) :
There are no news yet...
}
-
See all news
+
See all news
diff --git a/components/drafts.tsx b/components/drafts.tsx
index b6c563a..163aba8 100644
--- a/components/drafts.tsx
+++ b/components/drafts.tsx
@@ -46,7 +46,7 @@ export default function DraftsDialog({ drafts }: { drafts: Draft[]}) {
{drafts.map((draft) => (
-
+
{draft.title}
@@ -61,7 +61,7 @@ export default function DraftsDialog({ drafts }: { drafts: Draft[]}) {
))}
) : (
- There are no drafts yet...
+ There are no drafts yet...
)}
diff --git a/components/editor.tsx b/components/editor.tsx
index 6c10b36..141899c 100644
--- a/components/editor.tsx
+++ b/components/editor.tsx
@@ -119,7 +119,7 @@ export default function Editor({
Page type
- setType(e)}>
+ setType(e)} disabled={true}>
@@ -139,6 +139,7 @@ export default function Editor({
value={slug}
onChange={(e) => setSlug(e.target.value)}
placeholder="page-slug"
+ disabled={true}
/>
diff --git a/components/newsCard.tsx b/components/newsCard.tsx
new file mode 100644
index 0000000..49365e8
--- /dev/null
+++ b/components/newsCard.tsx
@@ -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 (
+
+
+
{news.title}
+
+
+
{formatDate(news.publishedAt)}
+ {(news?.meta as PageMeta)?.location &&
{(news.meta as PageMeta)?.location}
}
+
+ {((news.meta as PageMeta)?.tags)?.map((tag) => (
+ {tag}
+ ))}
+
+
+
+ )
+}
\ No newline at end of file
diff --git a/lib/pages/actions.ts b/lib/pages/actions.ts
index 8547a6b..f9d6130 100644
--- a/lib/pages/actions.ts
+++ b/lib/pages/actions.ts
@@ -2,7 +2,7 @@
import { redirect } from "next/navigation";
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";
export async function savePageAction(params: {
@@ -14,18 +14,29 @@ export async function savePageAction(params: {
blocks: Block[],
meta?: PageMeta
}) {
+ const basePath = params.type === "NEWS" ? "news" : "pages";
+
if (params.id) {
await updatePage(params.id, params.title, params.type, params.status, params.blocks, params.meta);
- revalidatePath(`/${params.slug}`);
- revalidatePath("/news");
+ revalidatePath(`/${basePath}/${params.slug}`);
+ revalidatePath(`/${basePath}`);
} else {
const page = await createPage(params.slug, params.title, params.type, params.status, params.blocks, params.meta);
- revalidatePath("/news");
- redirect(`/news/${page.slug}/edit`);
+ revalidatePath(`/${basePath}`);
+ 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);
- revalidatePath("/news");
+ const basePath = type === "NEWS" ? "news" : "pages";
+ revalidatePath(`/${basePath}`);
}
\ No newline at end of file
diff --git a/lib/pages/repository.ts b/lib/pages/repository.ts
index 50df5e4..22c374c 100644
--- a/lib/pages/repository.ts
+++ b/lib/pages/repository.ts
@@ -37,7 +37,7 @@ export async function createPage(slug: string, title: string, type: PageType, st
status,
meta: meta ?? {},
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;
}
-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(
id: string,
title: string,
+ type: PageType,
status: PageStatus,
blocks: Block[],
meta?: PageMeta
@@ -102,10 +71,12 @@ export async function updatePage(
where: { id },
data: {
title,
+ type,
status,
meta: meta ?? {},
publishedAt: status === "PUBLISHED" ? new Date() : undefined,
updatedAt: new Date(),
+ updatedBy: user.id
},
});
@@ -124,20 +95,15 @@ export async function updatePage(
: []),
]);
- if (status === "PUBLISHED") {
- 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) {
const user = await getCurrentUser();
if (!user?.isAdmin) throw new Error("Access denied");
diff --git a/lib/pages/types.ts b/lib/pages/types.ts
index e997ac2..c0d79ff 100644
--- a/lib/pages/types.ts
+++ b/lib/pages/types.ts
@@ -39,13 +39,4 @@ export type Block = {
type: BlockType;
position: number;
content: BlockContent;
-}
-
-export type PageVersion = {
- id: string;
- pageId: string;
- title: string;
- blocks: Block[];
- createdAt: Date;
- createdBy: string;
-};
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/lib/user.ts b/lib/user.ts
index c0431c7..86e2ddc 100644
--- a/lib/user.ts
+++ b/lib/user.ts
@@ -5,4 +5,9 @@ export async function getCurrentUser() {
const userId = await getSessionUserId();
if (!userId) return null;
return findUserById(userId);
+}
+
+export async function getUser(id: string) {
+ if (!id) return null;
+ return findUserById(id);
}
\ No newline at end of file
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index edb2e43..db9e38d 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -12,19 +12,24 @@ datasource db {
provider = "postgresql"
}
+enum Sex {
+ MALE
+ FEMALE
+}
+
model User {
id String @id @default(uuid())
kotosoftId String @unique
email String @unique
firstName String
secondName String
- sex String
+ sex Sex
birthDate DateTime
createdAt DateTime @default(now())
isAdmin Boolean @default(false)
- pages Page[]
- versions PageVersion[]
+ pages Page[] @relation("PageAuthor")
+ editedPages Page[] @relation("PageEditor")
}
enum PageStatus {
@@ -34,25 +39,28 @@ enum PageStatus {
}
enum PageType {
+ PAGE
NEWS
}
model Page {
- id String @id @default(uuid())
- slug String @unique
- title String
- type PageType
- status PageStatus @default(DRAFT)
- meta Json @default("{}")
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
+ id String @id @default(uuid())
+ slug String @unique
+ title String
+ type PageType
+ status PageStatus @default(DRAFT)
+ meta Json @default("{}")
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
publishedAt DateTime?
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[]
- versions PageVersion[]
}
model Block {
@@ -69,17 +77,4 @@ model Block {
content Json
@@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])
}
\ No newline at end of file