From e192717466645414451a9ecdd5bbd9c2abd0f744 Mon Sep 17 00:00:00 2001 From: Ivan Rezchikov Date: Sat, 4 Jul 2026 15:45:05 +0300 Subject: [PATCH] feat: images upload --- app/account/account-menu.tsx | 7 +- .../citizenship/applications-dialog.tsx | 60 ++-- app/account/citizenship/apply/form.tsx | 182 ++++++------- .../citizenship/apply/image-upload-field.tsx | 80 ++++++ app/account/citizenship/apply/page.tsx | 33 ++- app/account/citizenship/has-citizenship.tsx | 44 +++ app/account/citizenship/no-citizenship.tsx | 90 ++++++ app/account/citizenship/page.tsx | 140 ++-------- .../citizenship/renunciation-dialog.tsx | 55 ++++ .../citizenship/review/application-card.tsx | 43 ++- app/account/citizenship/review/client.tsx | 15 +- app/account/citizenship/review/data-table.tsx | 2 +- app/account/citizenship/review/page.tsx | 21 +- app/account/delete-dialog.tsx | 8 +- app/account/logout-button.tsx | 13 +- app/account/page.tsx | 2 +- app/account/update-button.tsx | 22 +- bun.lock | 206 ++++++++++++-- components/{id-card.tsx => identity-card.tsx} | 47 +++- hooks/useAction.ts | 23 ++ lib/action-result.ts | 12 + lib/address.ts | 19 ++ lib/citizenship/actions.ts | 257 ++++++++++-------- lib/citizenship/repository.ts | 73 ++++- lib/citizenship/types.ts | 15 +- lib/citizenship/validation.ts | 77 ++++-- lib/form-action.ts | 34 +++ lib/id/repository.ts | 58 ++-- lib/image.ts | 61 +++++ lib/minio.ts | 9 + lib/run-action.ts | 29 ++ lib/user/actions.ts | 72 ++--- lib/user/require.ts | 15 + next.config.ts | 8 +- package.json | 2 + .../migration.sql | 31 +++ prisma/schema.prisma | 34 ++- todo | 50 +--- 38 files changed, 1334 insertions(+), 615 deletions(-) create mode 100644 app/account/citizenship/apply/image-upload-field.tsx create mode 100644 app/account/citizenship/has-citizenship.tsx create mode 100644 app/account/citizenship/no-citizenship.tsx create mode 100644 app/account/citizenship/renunciation-dialog.tsx rename components/{id-card.tsx => identity-card.tsx} (78%) create mode 100644 hooks/useAction.ts create mode 100644 lib/action-result.ts create mode 100644 lib/address.ts create mode 100644 lib/form-action.ts create mode 100644 lib/image.ts create mode 100644 lib/minio.ts create mode 100644 lib/run-action.ts create mode 100644 lib/user/require.ts create mode 100644 prisma/migrations/20260703120000_citizenship_address_and_document_renewal/migration.sql diff --git a/app/account/account-menu.tsx b/app/account/account-menu.tsx index be5311a..6b18788 100644 --- a/app/account/account-menu.tsx +++ b/app/account/account-menu.tsx @@ -15,12 +15,7 @@ const menuItems = [ href: "/account/citizenship", label: "Citizenship", icon: BookOpenTextIcon, - }, - { - href: "/account/signature", - label: "Signature", - icon: SignatureIcon, - }, + } ]; export function AccountMenu() { diff --git a/app/account/citizenship/applications-dialog.tsx b/app/account/citizenship/applications-dialog.tsx index 65c8e9e..28d9a19 100644 --- a/app/account/citizenship/applications-dialog.tsx +++ b/app/account/citizenship/applications-dialog.tsx @@ -25,37 +25,37 @@ export function ApplicationsDialog({ applications }: { applications: Citizenship

Citizenship applications

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

Application №{i + 1}

- -
-
-

Created on {formatDate(application.createdAt)}

-
-
-

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

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

Rejected

-
-

{application.rejectionReason}

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

Approved

-
- )} -
-
-
- ))} -
+ + {applications.map((application, i) => ( + +

Application №{i + 1}

+ +
+
+

Created on {formatDate(application.createdAt)}

+
+
+

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

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

Rejected

+
+

{application.rejectionReason}

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

Approved

+
+ )} +
+
+
+ ))} +
); diff --git a/app/account/citizenship/apply/form.tsx b/app/account/citizenship/apply/form.tsx index 111218b..12368e8 100644 --- a/app/account/citizenship/apply/form.tsx +++ b/app/account/citizenship/apply/form.tsx @@ -1,11 +1,13 @@ "use client"; -import { useActionState, useState } from "react"; +import { useActionState, useEffect, useState } from "react"; +import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Field, FieldContent, FieldDescription, + FieldError, FieldGroup, FieldLabel, FieldLegend, @@ -21,27 +23,22 @@ import { SelectValue, } from "@/components/ui/select"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; -import { - Alert, - AlertDescription, - AlertTitle, -} from "@/components/ui/alert"; +import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { InfoIcon } from "lucide-react"; -import { submitCitizenshipApplication, type ApplicationState } from "@/lib/citizenship/actions"; +import { submitCitizenshipApplication } from "@/lib/citizenship/actions"; +import { initialFormState } from "@/lib/form-action"; import { EYE_COLORS } from "@/lib/citizenship/types"; -import Link from "next/link"; import { Spinner } from "@/components/ui/spinner"; +import { ImageUploadField } from "./image-upload-field"; type Props = { firstName: string; secondName: string; sex: string; - birthDate: Date | null; + birthDate: Date; regions: { id: string; name: string }[]; }; -const initialState: ApplicationState = { success: false, errors: [] }; - function formatDate(d: Date | null): string { if (!d) return ""; return d.toISOString().split("T")[0]; @@ -51,9 +48,17 @@ type AppType = "PREVIOUS_CITIZENSHIP" | "NATURALIZATION"; export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDate, regions }: Props) { const [appType, setAppType] = useState(""); - const [state, formAction, isPending] = useActionState(submitCitizenshipApplication, initialState); + const [state, formAction, isPending] = useActionState(submitCitizenshipApplication, initialFormState); const [isEuResident, setIsEuResident] = useState(null); + useEffect(() => { + if (state.error) { + toast.error(state.error); + } + }, [state.error]); + + const errors = state.fieldErrors ?? {}; + return (
@@ -70,65 +75,42 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa Surname - + Given names - +
Sex - + Date of birth - +
+
Eligibility basis - - Select the basis on which you are applying for citizenship. - + Select the basis on which you are applying for citizenship. setAppType(v as AppType)} + aria-invalid={!!errors.applicationType?.length} required > - - Previous citizenship holder - + Previous citizenship holder - I held citizenship of the Duspean Republic or the Country of - Reality on 31 March 2026. + I held citizenship of the Duspean Republic or the Country of Reality on 31 March 2026. @@ -138,22 +120,22 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa Naturalisation - - I am a foreign national seeking naturalisation. - + I am a foreign national seeking naturalisation. + {errors.applicationType && {errors.applicationType[0]}}
+ {appType === "PREVIOUS_CITIZENSHIP" && (
Previous citizenship details - + Country + {errors.previousCountry && {errors.previousCountry[0]}} - + Region of issuance - - The region or authority that issued your previous citizenship. - + The region or authority that issued your previous citizenship. + {errors.previousRegion && {errors.previousRegion[0]}}
)} + {appType === "NATURALIZATION" && (
Naturalisation statement - + Why do you wish to become a citizen of the European Federation? @@ -190,24 +174,22 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa name="naturalizationReason" placeholder="Explain your motivation for seeking citizenship..." rows={4} + aria-invalid={!!errors.naturalizationReason?.length} required /> + {errors.naturalizationReason && {errors.naturalizationReason[0]}}
)} +
Place of residence - - This will appear on your identity document. - + This will appear on your identity document. Is your residence in the European Federation? - setIsEuResident(v === "yes")} - > + setIsEuResident(v === "yes")}> Yes @@ -221,56 +203,56 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa {isEuResident === true && ( <> - + Region + {errors.regionId && {errors.regionId[0]}} - + City - + + {errors.city && {errors.city[0]}} - + Street and house number - + + {errors.street && {errors.street[0]}} )} {isEuResident === false && ( - + Full address - - Enter your full address as it should appear on the document. - + Enter your full address as it should appear on the document. + {errors.customAddress && {errors.customAddress[0]}} )}
+
Additional information - - Provide details required for your identity document. - + Provide details required for your identity document.
- + Height (in cm) + {errors.height && {errors.height[0]}} - + Eye colour + {errors.eyeColor && {errors.eyeColor[0]}}
- {state.errors.length > 0 && ( - -
    - {state.errors.map((err, i) => ( -
  • {err}
  • - ))} -
-
- )} + +
+ Images + Provide your profile image and your signature for your identity document. + + + + +
+
); -} +} \ No newline at end of file diff --git a/app/account/citizenship/apply/image-upload-field.tsx b/app/account/citizenship/apply/image-upload-field.tsx new file mode 100644 index 0000000..5e2435e --- /dev/null +++ b/app/account/citizenship/apply/image-upload-field.tsx @@ -0,0 +1,80 @@ +"use client"; + +import { useState } from "react"; +import { Field, FieldDescription, FieldError, FieldLabel } from "@/components/ui/field"; +import { Input } from "@/components/ui/input"; +import { cn } from "@/lib/utils"; + +type Props = { + id: string; + name: string; + label: string; + description: string; + error?: string; +}; + +export function ImageUploadField({ id, name, label, description, error }: Props) { + const [previewUrl, setPreviewUrl] = useState(null); + const [clientError, setClientError] = useState(null); + + const MAX_SIZE = 5 * 1024 * 1024; + const ACCEPTED_TYPES = ["image/jpeg", "image/png", "image/webp"]; + + function handleChange(e: React.ChangeEvent) { + const file = e.target.files?.[0]; + if (previewUrl) URL.revokeObjectURL(previewUrl); + + if (!file) { + setPreviewUrl(null); + setClientError(null); + return; + } + + if (!ACCEPTED_TYPES.includes(file.type)) { + setClientError("Only JPEG, PNG, or WEBP images are allowed."); + setPreviewUrl(null); + e.target.value = ""; + return; + } + + if (file.size > MAX_SIZE) { + setClientError("Image must be smaller than 5MB."); + setPreviewUrl(null); + e.target.value = ""; + return; + } + + setClientError(null); + setPreviewUrl(URL.createObjectURL(file)); + } + + const displayedError = clientError ?? error; + + return ( + + {label} +
+ {previewUrl && ( + + )} + +
+ {description} + {displayedError && {displayedError}} +
+ ); +} \ No newline at end of file diff --git a/app/account/citizenship/apply/page.tsx b/app/account/citizenship/apply/page.tsx index bf14b87..4d1932c 100644 --- a/app/account/citizenship/apply/page.tsx +++ b/app/account/citizenship/apply/page.tsx @@ -4,17 +4,16 @@ import { getCurrentUser } from "@/lib/user"; import { redirect } from "next/navigation"; import { AccountMenu } from "../../account-menu"; import { CitizenshipApplicationForm } from "./form"; -import { getUserApplications } from "@/lib/citizenship/repository"; +import { getApplicationEligibility } from "@/lib/citizenship/repository"; import { prisma } from "@/lib/prisma"; +import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; +import { InfoIcon } from "lucide-react"; export default async function CitizenshipApplication() { const user = await getCurrentUser(); if (!user) redirect("/"); - const applications = await getUserApplications(user.id); - const canApply = applications.length === 0 || applications.every((a) => a.status === "REJECTED"); - if (!canApply) redirect("/account/citizenship"); - + const eligibility = await getApplicationEligibility(user.id, user.birthDate); const regions = await prisma.region.findMany({ orderBy: { name: "asc" } }); return ( @@ -26,13 +25,21 @@ export default async function CitizenshipApplication() {

Citizenship application

- + {!eligibility.eligible ? ( + + + Cannot submit application + {eligibility.reason} + + ) : ( + + )}
@@ -40,4 +47,4 @@ export default async function CitizenshipApplication() {