prod
This commit is contained in:
+21
-48
@@ -1,18 +1,20 @@
|
||||
"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 { Loader2 } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { useAction } from "@/hooks/useAction";
|
||||
|
||||
export default function CreatePageClient() {
|
||||
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("/news"),
|
||||
});
|
||||
|
||||
const dataRef = useRef<EditorData>({
|
||||
title: "",
|
||||
type: "NEWS",
|
||||
@@ -23,47 +25,20 @@ export default function CreatePageClient() {
|
||||
});
|
||||
|
||||
function handleSave(status: "DRAFT" | "PUBLISHED") {
|
||||
startTransition(async () => {
|
||||
const data = dataRef.current;
|
||||
const payload = {
|
||||
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 },
|
||||
};
|
||||
|
||||
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("/news");
|
||||
} catch (e) {
|
||||
toast.error(e instanceof Error ? e.message : "Failed to save page");
|
||||
}
|
||||
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-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>Create page</h1>
|
||||
<div className="flex flex-row gap-3">
|
||||
@@ -72,11 +47,11 @@ export default function CreatePageClient() {
|
||||
disabled={isPending}
|
||||
onClick={() => handleSave("DRAFT")}
|
||||
>
|
||||
{isPending && <Loader2 size={14} className="animate-spin mr-1" />}
|
||||
{isPending && <Spinner />}
|
||||
Save as draft
|
||||
</Button>
|
||||
<Button disabled={isPending} onClick={() => handleSave("PUBLISHED")}>
|
||||
{isPending && <Loader2 size={14} className="animate-spin mr-1" />}
|
||||
{isPending && <Spinner />}
|
||||
Publish
|
||||
</Button>
|
||||
</div>
|
||||
@@ -84,10 +59,8 @@ export default function CreatePageClient() {
|
||||
<Editor
|
||||
onChange={(data) => {
|
||||
dataRef.current = data;
|
||||
setFieldErrors({});
|
||||
}}
|
||||
errors={fieldErrors}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user