feat: citizenship applications

This commit is contained in:
Ivan Rezchikov
2026-06-23 19:03:10 +03:00
parent 58ec75c948
commit c60ab9e8f9
44 changed files with 1254 additions and 130 deletions
+38 -5
View File
@@ -1,15 +1,18 @@
"use client";
import { useRef, useTransition } from "react";
import { useRef, useState, 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 { validatePage } from "@/lib/pages/validation";
import { Loader2 } from "lucide-react";
import { toast } from "sonner";
export default function CreatePageClient() {
const router = useRouter();
const [isPending, startTransition] = useTransition();
const [fieldErrors, setFieldErrors] = useState<{ title?: string[]; slug?: string[] }>({});
const dataRef = useRef<EditorData>({
title: "",
type: "NEWS",
@@ -22,7 +25,7 @@ export default function CreatePageClient() {
function handleSave(status: "DRAFT" | "PUBLISHED") {
startTransition(async () => {
const data = dataRef.current;
await savePageAction({
const payload = {
id: null,
slug: data.slug || data.title.toLowerCase().replace(/\s+/g, "-"),
title: data.title,
@@ -30,8 +33,32 @@ export default function CreatePageClient() {
status,
blocks: data.blocks,
meta: { location: data.location, tags: data.tags },
});
router.push("/news");
};
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");
}
});
}
@@ -54,7 +81,13 @@ export default function CreatePageClient() {
</Button>
</div>
</div>
<Editor onChange={(data) => { dataRef.current = data; }} />
<Editor
onChange={(data) => {
dataRef.current = data;
setFieldErrors({});
}}
errors={fieldErrors}
/>
</div>
);
}