Files
eu/app/create/client.tsx
T
2026-07-15 22:19:26 +03:00

67 lines
1.8 KiB
TypeScript

"use client";
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 { Spinner } from "@/components/ui/spinner";
import { useAction } from "@/hooks/useAction";
export default function CreatePageClient() {
const router = useRouter();
const { run, isPending } = useAction(savePageAction, {
successMessage: "Page saved",
onSuccess: () => router.push("/news"),
});
const dataRef = useRef<EditorData>({
title: "",
type: "PAGE",
slug: "",
location: "",
tags: [],
blocks: [],
});
function handleSave(status: "DRAFT" | "PUBLISHED") {
const data = dataRef.current;
run({
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 },
});
}
return (
<div className="flex flex-col p-4 md:p-16 gap-6 md:gap-12 mb-4 md:mb-0">
<div className="flex flex-col gap-4 md:flex-row md:items-center justify-between">
<h1>Create page</h1>
<div className="flex flex-row gap-3">
<Button
variant="outline"
disabled={isPending}
onClick={() => handleSave("DRAFT")}
>
{isPending && <Spinner />}
Save as draft
</Button>
<Button disabled={isPending} onClick={() => handleSave("PUBLISHED")}>
{isPending && <Spinner />}
Publish
</Button>
</div>
</div>
<Editor
onChange={(data) => {
dataRef.current = data;
}}
/>
</div>
);
}