feat: news
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"use client";
|
||||
|
||||
import { useRef, useTransition } 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 { Loader2 } from "lucide-react";
|
||||
|
||||
export default function CreatePageClient() {
|
||||
const router = useRouter();
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const dataRef = useRef<EditorData>({
|
||||
title: "",
|
||||
type: "NEWS",
|
||||
slug: "",
|
||||
location: "",
|
||||
tags: [],
|
||||
blocks: [],
|
||||
});
|
||||
|
||||
function handleSave(status: "DRAFT" | "PUBLISHED") {
|
||||
startTransition(async () => {
|
||||
const data = dataRef.current;
|
||||
await savePageAction({
|
||||
id: null,
|
||||
slug: data.slug || data.title.toLowerCase().replace(/\s+/g, "-"),
|
||||
title: data.title,
|
||||
type: data.type,
|
||||
status,
|
||||
blocks: data.blocks,
|
||||
meta: { location: data.location, tags: data.tags },
|
||||
});
|
||||
router.push("/news");
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col p-16 gap-12">
|
||||
<div className="flex flex-row items-center justify-between">
|
||||
<h1 className="underline underline-offset-8 decoration-2">Create page</h1>
|
||||
<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 disabled={isPending} onClick={() => handleSave("PUBLISHED")}>
|
||||
{isPending && <Loader2 size={14} className="animate-spin mr-1" />}
|
||||
Publish
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<Editor onChange={(data) => { dataRef.current = data; }} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import Header from "@/components/header";
|
||||
import Footer from "@/components/footer";
|
||||
import { getCurrentUser } from "@/lib/user";
|
||||
import { redirect } from "next/navigation";
|
||||
import CreatePageClient from "./client";
|
||||
|
||||
export default async function CreatePage() {
|
||||
const user = await getCurrentUser();
|
||||
if (!user?.isAdmin) redirect("/");
|
||||
|
||||
return (
|
||||
<>
|
||||
<Header />
|
||||
<CreatePageClient />
|
||||
<Footer />
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user