From c60ab9e8f9849fd6e24e74537e8a187c53b7b432 Mon Sep 17 00:00:00 2001 From: Ivan Rezchikov Date: Tue, 23 Jun 2026 19:03:10 +0300 Subject: [PATCH] feat: citizenship applications --- app/[type]/[slug]/edit/client.tsx | 39 ++- app/[type]/[slug]/edit/page.tsx | 2 +- app/[type]/[slug]/page.tsx | 9 +- .../citizenship/applicationsDialog.tsx | 62 ++++ app/account/citizenship/apply/form.tsx | 291 ++++++++++++++++++ app/account/citizenship/apply/page.tsx | 39 +++ app/account/citizenship/page.tsx | 114 +++++++ app/account/deleteDialog.tsx | 2 +- app/account/logoutButton.tsx | 2 +- app/account/menu.tsx | 53 ++++ app/account/page.tsx | 20 +- app/account/updateButton.tsx | 30 ++ app/actions/delete.tsx | 9 - app/actions/login.tsx | 27 -- app/actions/logout.tsx | 9 - app/api/callback/route.ts | 33 +- app/create/client.tsx | 43 ++- app/layout.tsx | 6 +- app/page.tsx | 12 +- bun.lock | 12 +- components/drafts.tsx | 9 +- components/editor.tsx | 20 +- components/header.tsx | 4 +- components/newsCard.tsx | 2 +- components/ui/accordion.tsx | 85 +++++ components/ui/dropdown-menu.tsx | 2 +- components/ui/radio-group.tsx | 41 +++ components/ui/sonner.tsx | 25 ++ lib/auth/session.ts | 8 +- lib/auth/tokens.ts | 5 + lib/citizenship/actions.ts | 56 ++++ lib/citizenship/repository.ts | 33 ++ lib/citizenship/types.ts | 37 +++ lib/citizenship/validation.ts | 44 +++ lib/pages/actions.ts | 5 + lib/pages/repository.ts | 18 +- lib/pages/validation.ts | 26 ++ lib/user.ts | 8 +- lib/user/actions.ts | 64 ++++ lib/user/repository.ts | 15 +- lib/user/types.ts | 4 +- package.json | 4 +- prisma.config.ts | 11 +- prisma/schema.prisma | 44 ++- 44 files changed, 1254 insertions(+), 130 deletions(-) create mode 100644 app/account/citizenship/applicationsDialog.tsx create mode 100644 app/account/citizenship/apply/form.tsx create mode 100644 app/account/citizenship/apply/page.tsx create mode 100644 app/account/citizenship/page.tsx create mode 100644 app/account/menu.tsx create mode 100644 app/account/updateButton.tsx delete mode 100644 app/actions/delete.tsx delete mode 100644 app/actions/login.tsx delete mode 100644 app/actions/logout.tsx create mode 100644 components/ui/accordion.tsx create mode 100644 components/ui/radio-group.tsx create mode 100644 components/ui/sonner.tsx create mode 100644 lib/citizenship/actions.ts create mode 100644 lib/citizenship/repository.ts create mode 100644 lib/citizenship/types.ts create mode 100644 lib/citizenship/validation.ts create mode 100644 lib/pages/validation.ts create mode 100644 lib/user/actions.ts 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? + +