prod
This commit is contained in:
@@ -1,23 +1,25 @@
|
||||
"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";
|
||||
import { PageType } from "@/lib/pages/types";
|
||||
import { useAction } from "@/hooks/useAction";
|
||||
|
||||
export function ArchiveButton({ pageId, slug, type }: { pageId: string, slug: string, type: PageType }) {
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const { run, isPending } = useAction(archivePageAction, {
|
||||
successMessage: "Page archived",
|
||||
});
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="icon-sm"
|
||||
disabled={isPending}
|
||||
onClick={() => startTransition(() => archivePageAction(pageId, slug, type))}
|
||||
onClick={() => run(pageId, slug, type)}
|
||||
>
|
||||
{isPending ? <Spinner /> : <ArchiveIcon />}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
"use client";
|
||||
|
||||
import { useRef, useState, useTransition } from "react";
|
||||
import { useRef } 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 { validatePage } from "@/lib/pages/validation";
|
||||
import { Block, Page, PageMeta, PageType } from "@/lib/pages/types";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { useAction } from "@/hooks/useAction";
|
||||
|
||||
type Props = {
|
||||
page: Page | null;
|
||||
@@ -18,8 +17,10 @@ type Props = {
|
||||
|
||||
export default function EditPageClient({ page, pageType, initialBlocks = [] }: Props) {
|
||||
const router = useRouter();
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const [fieldErrors, setFieldErrors] = useState<{ title?: string[]; slug?: string[] }>({});
|
||||
const { run, isPending } = useAction(savePageAction, {
|
||||
successMessage: "Page saved",
|
||||
onSuccess: () => router.push(`/${pageType.toLowerCase()}/${dataRef.current.slug}`),
|
||||
});
|
||||
|
||||
const dataRef = useRef<EditorData>({
|
||||
title: page?.title ?? "",
|
||||
@@ -31,47 +32,20 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P
|
||||
});
|
||||
|
||||
function handleSave(status: "DRAFT" | "PUBLISHED") {
|
||||
startTransition(async () => {
|
||||
const data = dataRef.current;
|
||||
const payload = {
|
||||
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 parsed = validatePage(payload);
|
||||
if (!parsed.success) {
|
||||
const errors: { title?: string[]; slug?: string[] } = {};
|
||||
for (const msg of parsed.errors) {
|
||||
if (msg.toLowerCase().includes("title")) {
|
||||
(errors.title ??= []).push(msg);
|
||||
} else if (msg.toLowerCase().includes("slug")) {
|
||||
(errors.slug ??= []).push(msg);
|
||||
}
|
||||
}
|
||||
setFieldErrors(errors);
|
||||
toast.error(parsed.errors.join(", "));
|
||||
return;
|
||||
}
|
||||
|
||||
setFieldErrors({});
|
||||
|
||||
try {
|
||||
await savePageAction(payload);
|
||||
toast.success("Page saved");
|
||||
router.push(`/${pageType.toLowerCase()}/${data.slug}`);
|
||||
} catch (e) {
|
||||
toast.error(e instanceof Error ? e.message : "Failed to save page");
|
||||
}
|
||||
const data = dataRef.current;
|
||||
run({
|
||||
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 },
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col p-16 gap-12">
|
||||
<div className="flex flex-col p-4 md:p-16 gap-6 md:gap-12 mb-4 md:mb-0">
|
||||
<div className="flex flex-row items-center justify-between">
|
||||
<h1>Edit page</h1>
|
||||
<div className="flex flex-row gap-3">
|
||||
@@ -79,7 +53,7 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P
|
||||
disabled={isPending}
|
||||
onClick={() => handleSave("PUBLISHED")}
|
||||
>
|
||||
{isPending && <Loader2 size={14} className="animate-spin mr-1" />}
|
||||
{isPending && <Spinner />}
|
||||
Save
|
||||
</Button>
|
||||
</div>
|
||||
@@ -88,12 +62,10 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P
|
||||
page={page}
|
||||
type={pageType}
|
||||
initialBlocks={initialBlocks}
|
||||
errors={fieldErrors}
|
||||
onChange={(data) => {
|
||||
dataRef.current = data;
|
||||
setFieldErrors({});
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
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";
|
||||
@@ -20,10 +18,6 @@ export default async function EditPage({ params }: { params: Promise<{ type: str
|
||||
const blocks = page ? await getBlocksByPageId(page.id) : [];
|
||||
|
||||
return (
|
||||
<>
|
||||
<Header />
|
||||
<EditPageClient page={page ?? null} pageType={pageType} initialBlocks={blocks} />
|
||||
<Footer />
|
||||
</>
|
||||
<EditPageClient page={page ?? null} pageType={pageType} initialBlocks={blocks} />
|
||||
);
|
||||
}
|
||||
+38
-42
@@ -53,7 +53,7 @@ function BlockRenderer({ block }: { block: Block }) {
|
||||
|
||||
case "CALLOUT":
|
||||
return (
|
||||
<Alert className="w-max">
|
||||
<Alert className="md:w-max">
|
||||
<InfoIcon />
|
||||
<AlertTitle>{b.title}</AlertTitle>
|
||||
<AlertDescription>{b.text}</AlertDescription>
|
||||
@@ -68,11 +68,11 @@ function BlockRenderer({ block }: { block: Block }) {
|
||||
alt={b.caption || "Image"}
|
||||
width={1200}
|
||||
height={800}
|
||||
className="rounded-md border max-w-200 h-auto object-contain"
|
||||
className="w-full md:w-auto md:max-w-200 h-auto rounded-md border object-contain"
|
||||
/>
|
||||
|
||||
{b.caption && (
|
||||
<figcaption className="text-sm text-muted-foreground">
|
||||
<figcaption className="text-xs md:text-sm text-muted-foreground">
|
||||
{b.caption}
|
||||
</figcaption>
|
||||
)}
|
||||
@@ -83,13 +83,13 @@ function BlockRenderer({ block }: { block: Block }) {
|
||||
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>
|
||||
<p className="flex flex-row gap-2 md: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 className="flex flex-row gap-0.5 md:gap-1 md:items-center" key={index}><div><DotIcon className="text-muted-foreground" /></div><p>{item}</p></div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
@@ -124,7 +124,7 @@ function BlockRenderer({ block }: { block: Block }) {
|
||||
|
||||
case "LINK":
|
||||
return <Link href={b.href} target="_blank">
|
||||
<Button variant="secondary" className="w-max">{b.label}</Button>
|
||||
<Button variant="secondary" className="max-w-full">{b.label}</Button>
|
||||
</Link>
|
||||
|
||||
default:
|
||||
@@ -166,44 +166,40 @@ export default async function Page({ params }: { params: Promise<{ type: string;
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Header />
|
||||
<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>{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">
|
||||
{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-6">
|
||||
{isArchived && (
|
||||
<Alert className="w-max">
|
||||
<InfoIcon />
|
||||
<AlertTitle>This page is archived</AlertTitle>
|
||||
<AlertDescription>It can be seen only by administrators. Make any edits to make it visible again.</AlertDescription>
|
||||
</Alert>
|
||||
<div className="flex flex-col p-4 md:p-16 gap-6 md:gap-12 mb-4 md:mb-0">
|
||||
<div className={cn("flex flex-col gap-8 md:gap-4 justify-center", isNews && "md:items-center")}>
|
||||
<div className="flex flex-col md:flex-row items-center gap-2 md:gap-6">
|
||||
<h1>{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 pageId={page.id} isPinned={!!navlink} />}
|
||||
</div>
|
||||
)}
|
||||
{blocks.map((block) => (
|
||||
<BlockRenderer key={block.id} block={block} />
|
||||
))}
|
||||
</div>
|
||||
<div className="flex flex-col md:flex-row gap-2 md:gap-6 md:items-center">
|
||||
{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>
|
||||
<Footer />
|
||||
</>
|
||||
<div className="flex flex-col gap-6">
|
||||
{isArchived && (
|
||||
<Alert className="md:w-max">
|
||||
<InfoIcon />
|
||||
<AlertTitle>This page is archived</AlertTitle>
|
||||
<AlertDescription>It can be seen only by administrators. Make any edits to make it visible again.</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
{blocks.map((block) => (
|
||||
<BlockRenderer key={block.id} block={block} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,23 +1,19 @@
|
||||
"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";
|
||||
import { togglePinAction } from "@/lib/navlinks/actions";
|
||||
import { useAction } from "@/hooks/useAction";
|
||||
|
||||
export function PinToggleButton({ label, pageId, isPinned }: { label: string; pageId: string; isPinned: boolean }) {
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
const handleClick = () => {
|
||||
startTransition(() =>
|
||||
isPinned ? unpinPageAction(pageId) : pinPageAction(pageId)
|
||||
);
|
||||
};
|
||||
export function PinToggleButton({ pageId, isPinned }: { pageId: string; isPinned: boolean }) {
|
||||
const { run, isPending } = useAction(togglePinAction, {
|
||||
successMessage: isPinned ? "Page unpinned" : "Page pinned",
|
||||
});
|
||||
|
||||
return (
|
||||
<Button variant={isPinned ? "secondary" : "secondary"} size="icon-sm" disabled={isPending} onClick={handleClick}>
|
||||
<Button variant="secondary" size="icon-sm" disabled={isPending} onClick={() => run(pageId, isPinned)}>
|
||||
{isPending ? <Spinner /> : isPinned ? <PinOffIcon /> : <PinIcon />}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user