diff --git a/app/[type]/[slug]/edit/client.tsx b/app/[type]/[slug]/edit/client.tsx index 92c550e..b8e571b 100644 --- a/app/[type]/[slug]/edit/client.tsx +++ b/app/[type]/[slug]/edit/client.tsx @@ -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({ 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({}); }} /> diff --git a/app/[type]/[slug]/edit/page.tsx b/app/[type]/[slug]/edit/page.tsx index fba4e50..2798a5c 100644 --- a/app/[type]/[slug]/edit/page.tsx +++ b/app/[type]/[slug]/edit/page.tsx @@ -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) : []; diff --git a/app/[type]/[slug]/page.tsx b/app/[type]/[slug]/page.tsx index efd0877..c0c3eea 100644 --- a/app/[type]/[slug]/page.tsx +++ b/app/[type]/[slug]/page.tsx @@ -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"; diff --git a/app/account/citizenship/applicationsDialog.tsx b/app/account/citizenship/applicationsDialog.tsx new file mode 100644 index 0000000..3e15a76 --- /dev/null +++ b/app/account/citizenship/applicationsDialog.tsx @@ -0,0 +1,62 @@ +import { + Dialog, + DialogContent, + DialogDescription, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; +import { CitizenshipApplication } from "@/lib/citizenship/types"; +import { + Accordion, + AccordionContent, + AccordionItem, + AccordionTrigger, +} from "@/components/ui/accordion"; +import { cn, formatDate } from "@/lib/utils"; +import { CheckIcon, ClockFadingIcon, PlusIcon, XIcon } from "lucide-react"; +import { Button } from "@/components/ui/button"; + +export function ApplicationsDialog({ applications }: { applications: CitizenshipApplication[] }) { + return ( + + + + +

Citizenship applications

+ Here are your citizenship applications + + {applications.map((application, i) => ( + +

Application №{i + 1}

+ +
+
+

Created on {formatDate(application.createdAt)}

+
+
+

{application.status === "PENDING" ? "On review" : `Reviewed on ${formatDate(application.reviewedAt)}`}

+
+ {application.status === "REJECTED" && ( +
+
+

Rejected

+
+

{application.rejectionReason}

+
+ )} + {application.status === "APPROVED" && ( +
+

Approved

+
+ )} +
+
+
+ ))} +
+
+
+
+ ); +} \ No newline at end of file diff --git a/app/account/citizenship/apply/form.tsx b/app/account/citizenship/apply/form.tsx new file mode 100644 index 0000000..24230fd --- /dev/null +++ b/app/account/citizenship/apply/form.tsx @@ -0,0 +1,291 @@ +"use client"; + +import { useActionState, useState } from "react"; +import { Button } from "@/components/ui/button"; +import { + Field, + FieldContent, + FieldDescription, + FieldGroup, + FieldLabel, + FieldLegend, + FieldSet, +} from "@/components/ui/field"; +import { Input } from "@/components/ui/input"; +import { Textarea } from "@/components/ui/textarea"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; +import { + Alert, + AlertDescription, + AlertTitle, +} from "@/components/ui/alert"; +import { InfoIcon } from "lucide-react"; +import { submitCitizenshipApplication, type ApplicationState } from "@/lib/citizenship/actions"; +import { EYE_COLORS } from "@/lib/citizenship/types"; +import Link from "next/link"; +import { Spinner } from "@/components/ui/spinner"; + +type Props = { + firstName: string; + secondName: string; + sex: string; + birthDate: Date | null; +}; + +const initialState: ApplicationState = { success: false, errors: [] }; + +function formatDate(d: Date | null): string { + if (!d) return ""; + return d.toISOString().split("T")[0]; +} + +type AppType = "PREVIOUS_CITIZENSHIP" | "BORN_IN_TERRITORY" | "NATURALIZATION"; + +export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDate }: Props) { + const [appType, setAppType] = useState(""); + const [state, formAction, isPending] = useActionState(submitCitizenshipApplication, initialState); + + return ( +
+ +
+ Personal information + + + Important notice + + Personal data is sourced from your account profile. Please ensure that all information is accurate. + If corrections are required, update your details in Kotosoft ID and synchronise your account. + + + + + Surname + + + + Given names + + +
+ + Sex + + + + Date of birth + + +
+
+
+
+ Eligibility basis + + Select the basis on which you are applying for citizenship. + + setAppType(v as AppType)} + required + > + + + + + + Previous citizenship holder + + + I held citizenship of the Duspean Republic or the Country of + Reality on 31 March 2026. + + + + + + + + + Born in territory + + I was born in the European Federation, the former Duspean + Republic, or the Country of Reality, and do not hold citizenship. + + + + + + + + + Naturalisation + + I am a foreign national seeking naturalisation. + + + + + +
+ {appType === "PREVIOUS_CITIZENSHIP" && ( +
+ Previous citizenship details + + + Country + + + + Region of issuance + + + The region or authority that issued your previous citizenship. The list of regions is here. + + + +
+ )} + {appType === "BORN_IN_TERRITORY" && ( +
+ Birth details + + + Region of birth + + + The municipality, region, and country where you were born. The list of regions is here. + + + +
+ )} + {appType === "NATURALIZATION" && ( +
+ Naturalisation statement + + + + Why do you wish to become a citizen of the European Federation? + +