60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
"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>
|
|
);
|
|
} |