feat: citizenship applications
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
"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 { Block, Page, PageMeta, PageType } from "@/lib/pages/types";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
type Props = {
|
||||
page: Page | null;
|
||||
@@ -17,10 +19,11 @@ type Props = {
|
||||
export default function EditPageClient({ page, pageType, initialBlocks = [] }: Props) {
|
||||
const router = useRouter();
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const [fieldErrors, setFieldErrors] = useState<{ title?: string[]; slug?: string[] }>({});
|
||||
|
||||
const dataRef = useRef<EditorData>({
|
||||
title: page?.title ?? "",
|
||||
type: page?.type ?? "",
|
||||
type: pageType,
|
||||
slug: page?.slug ?? "",
|
||||
location: (page?.meta as PageMeta)?.location ?? "",
|
||||
tags: (page?.meta as PageMeta)?.tags ?? [],
|
||||
@@ -30,7 +33,7 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P
|
||||
function handleSave(status: "DRAFT" | "PUBLISHED") {
|
||||
startTransition(async () => {
|
||||
const data = dataRef.current;
|
||||
await savePageAction({
|
||||
const payload = {
|
||||
id: page?.id ?? null,
|
||||
slug: data.slug || data.title.toLowerCase().replace(/\s+/g, "-"),
|
||||
title: data.title,
|
||||
@@ -38,8 +41,32 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P
|
||||
status,
|
||||
blocks: data.blocks,
|
||||
meta: { location: data.location, tags: data.tags },
|
||||
});
|
||||
router.push(`/${page?.type.toLowerCase()}/${data.slug}`);
|
||||
};
|
||||
|
||||
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(`/${pageType.toLowerCase()}/${data.slug}`);
|
||||
} catch (e) {
|
||||
toast.error(e instanceof Error ? e.message : "Failed to save page");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -61,8 +88,10 @@ export default function EditPageClient({ page, pageType, initialBlocks = [] }: P
|
||||
page={page}
|
||||
type={pageType}
|
||||
initialBlocks={initialBlocks}
|
||||
errors={fieldErrors}
|
||||
onChange={(data) => {
|
||||
dataRef.current = data;
|
||||
setFieldErrors({});
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -15,7 +15,7 @@ export default async function EditPage({ params }: { params: Promise<{ type: str
|
||||
const pageType = type.toUpperCase() as PageType;
|
||||
if (!PAGE_TYPES.includes(pageType)) notFound();
|
||||
|
||||
const page = await getPageBySlug(slug);
|
||||
const page = await getPageBySlug(slug, pageType);
|
||||
if (!page) notFound();
|
||||
const blocks = page ? await getBlocksByPageId(page.id) : [];
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import Header from "@/components/header";
|
||||
import Footer from "@/components/footer";
|
||||
import { getPageBySlug, getBlocksByPageId } from "@/lib/pages/repository";
|
||||
import { getCurrentUser, getUser } from "@/lib/user";
|
||||
import { getCurrentUser, getUsers } from "@/lib/user";
|
||||
import { notFound } from "next/navigation";
|
||||
import { Block, PAGE_TYPES, PageMeta, PageType } from "@/lib/pages/types";
|
||||
import { cn, formatDate } from "@/lib/utils";
|
||||
@@ -140,15 +140,16 @@ export default async function Page({ params }: { params: Promise<{ type: string;
|
||||
const pageType = type.toUpperCase() as PageType;
|
||||
if (!PAGE_TYPES.includes(pageType)) notFound();
|
||||
|
||||
const page = await getPageBySlug(slug);
|
||||
const page = await getPageBySlug(slug, pageType);
|
||||
if (!page || (!user?.isAdmin && page.status == "ARCHIVED")) notFound();
|
||||
|
||||
const navlink = await getNavLinkByPageId(page.id);
|
||||
|
||||
const blocks = page ? await getBlocksByPageId(page.id) : [];
|
||||
|
||||
const createdBy = await getUser(page?.createdBy);
|
||||
const updatedBy = await getUser(page?.updatedBy);
|
||||
const users = await getUsers(page.createdBy, page.updatedBy);
|
||||
const createdBy = users.find((u) => u.id === page.createdBy) ?? null;
|
||||
const updatedBy = users.find((u) => u.id === page.updatedBy) ?? null;
|
||||
|
||||
const isNews = page.type === "NEWS";
|
||||
const isArchived = page.status === "ARCHIVED";
|
||||
|
||||
Reference in New Issue
Block a user