feat: images upload

This commit is contained in:
Ivan Rezchikov
2026-07-04 15:45:05 +03:00
parent 2eb0de5357
commit e192717466
38 changed files with 1334 additions and 615 deletions
+89 -93
View File
@@ -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<AppType | "">("");
const [state, formAction, isPending] = useActionState(submitCitizenshipApplication, initialState);
const [state, formAction, isPending] = useActionState(submitCitizenshipApplication, initialFormState);
const [isEuResident, setIsEuResident] = useState<boolean | null>(null);
useEffect(() => {
if (state.error) {
toast.error(state.error);
}
}, [state.error]);
const errors = state.fieldErrors ?? {};
return (
<form action={formAction}>
<FieldGroup>
@@ -70,65 +75,42 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
<FieldGroup>
<Field>
<FieldLabel htmlFor="surname">Surname</FieldLabel>
<Input
id="surname"
value={secondName}
readOnly
className="bg-muted text-muted-foreground"
/>
<Input id="surname" value={secondName} readOnly className="bg-muted text-muted-foreground" />
</Field>
<Field>
<FieldLabel htmlFor="givenNames">Given names</FieldLabel>
<Input
id="givenNames"
value={firstName}
readOnly
className="bg-muted text-muted-foreground"
/>
<Input id="givenNames" value={firstName} readOnly className="bg-muted text-muted-foreground" />
</Field>
<div className="grid grid-cols-2 gap-4">
<Field>
<FieldLabel htmlFor="sex">Sex</FieldLabel>
<Input
id="sex"
value={sex === "MALE" ? "Male" : "Female"}
readOnly
className="bg-muted text-muted-foreground"
/>
<Input id="sex" value={sex === "MALE" ? "Male" : "Female"} readOnly className="bg-muted text-muted-foreground" />
</Field>
<Field>
<FieldLabel htmlFor="birthDate">Date of birth</FieldLabel>
<Input
id="birthDate"
value={formatDate(birthDate)}
readOnly
className="bg-muted text-muted-foreground"
/>
<Input id="birthDate" value={formatDate(birthDate)} readOnly className="bg-muted text-muted-foreground" />
</Field>
</div>
</FieldGroup>
</FieldSet>
<FieldSet>
<FieldLegend>Eligibility basis</FieldLegend>
<FieldDescription>
Select the basis on which you are applying for citizenship.
</FieldDescription>
<FieldDescription>Select the basis on which you are applying for citizenship.</FieldDescription>
<RadioGroup
name="applicationType"
value={appType}
onValueChange={(v) => setAppType(v as AppType)}
aria-invalid={!!errors.applicationType?.length}
required
>
<FieldLabel htmlFor="prev-citizen">
<Field orientation="horizontal">
<RadioGroupItem value="PREVIOUS_CITIZENSHIP" id="prev-citizen" />
<FieldContent>
<span className="text-sm font-medium">
Previous citizenship holder
</span>
<span className="text-sm font-medium">Previous citizenship holder</span>
<FieldDescription>
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.
</FieldDescription>
</FieldContent>
</Field>
@@ -138,22 +120,22 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
<RadioGroupItem value="NATURALIZATION" id="naturalization" />
<FieldContent>
<span className="text-sm font-medium">Naturalisation</span>
<FieldDescription>
I am a foreign national seeking naturalisation.
</FieldDescription>
<FieldDescription>I am a foreign national seeking naturalisation.</FieldDescription>
</FieldContent>
</Field>
</FieldLabel>
</RadioGroup>
{errors.applicationType && <FieldError>{errors.applicationType[0]}</FieldError>}
</FieldSet>
{appType === "PREVIOUS_CITIZENSHIP" && (
<FieldSet>
<FieldLegend>Previous citizenship details</FieldLegend>
<FieldGroup>
<Field>
<Field data-invalid={!!errors.previousCountry?.length}>
<FieldLabel htmlFor="previousCountry">Country</FieldLabel>
<Select name="previousCountry" defaultValue="">
<SelectTrigger id="previousCountry">
<SelectTrigger id="previousCountry" aria-invalid={!!errors.previousCountry?.length}>
<SelectValue placeholder="Select country" />
</SelectTrigger>
<SelectContent>
@@ -161,27 +143,29 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
<SelectItem value="REA">Country of Reality</SelectItem>
</SelectContent>
</Select>
{errors.previousCountry && <FieldError>{errors.previousCountry[0]}</FieldError>}
</Field>
<Field>
<Field data-invalid={!!errors.previousRegion?.length}>
<FieldLabel htmlFor="previousRegion">Region of issuance</FieldLabel>
<Input
id="previousRegion"
name="previousRegion"
placeholder="State, region, or territory"
aria-invalid={!!errors.previousRegion?.length}
required
/>
<FieldDescription>
The region or authority that issued your previous citizenship.
</FieldDescription>
<FieldDescription>The region or authority that issued your previous citizenship.</FieldDescription>
{errors.previousRegion && <FieldError>{errors.previousRegion[0]}</FieldError>}
</Field>
</FieldGroup>
</FieldSet>
)}
{appType === "NATURALIZATION" && (
<FieldSet>
<FieldLegend>Naturalisation statement</FieldLegend>
<FieldGroup>
<Field>
<Field data-invalid={!!errors.naturalizationReason?.length}>
<FieldLabel htmlFor="naturalizationReason">
Why do you wish to become a citizen of the European Federation?
</FieldLabel>
@@ -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 && <FieldError>{errors.naturalizationReason[0]}</FieldError>}
</Field>
</FieldGroup>
</FieldSet>
)}
<FieldSet>
<FieldLegend>Place of residence</FieldLegend>
<FieldDescription>
This will appear on your identity document.
</FieldDescription>
<FieldDescription>This will appear on your identity document.</FieldDescription>
<FieldGroup>
<Field>
<FieldLabel>Is your residence in the European Federation?</FieldLabel>
<RadioGroup
name="isEuResident"
onValueChange={(v) => setIsEuResident(v === "yes")}
>
<RadioGroup name="isEuResident" onValueChange={(v) => setIsEuResident(v === "yes")}>
<Field orientation="horizontal">
<RadioGroupItem value="yes" id="eu-yes" />
<FieldLabel htmlFor="eu-yes">Yes</FieldLabel>
@@ -221,56 +203,56 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
{isEuResident === true && (
<>
<Field>
<Field data-invalid={!!errors.regionId?.length}>
<FieldLabel htmlFor="regionId">Region</FieldLabel>
<Select name="regionId" required>
<SelectTrigger id="regionId">
<SelectTrigger id="regionId" aria-invalid={!!errors.regionId?.length}>
<SelectValue placeholder="Select region" />
</SelectTrigger>
<SelectContent>
{regions.map((r) => (
<SelectItem key={r.id} value={r.id}>
{r.name}
</SelectItem>
<SelectItem key={r.id} value={r.id}>{r.name}</SelectItem>
))}
</SelectContent>
</Select>
{errors.regionId && <FieldError>{errors.regionId[0]}</FieldError>}
</Field>
<Field>
<Field data-invalid={!!errors.city?.length}>
<FieldLabel htmlFor="city">City</FieldLabel>
<Input id="city" name="city" placeholder="City or town" required />
<Input id="city" name="city" placeholder="City or town" aria-invalid={!!errors.city?.length} required />
{errors.city && <FieldError>{errors.city[0]}</FieldError>}
</Field>
<Field>
<Field data-invalid={!!errors.street?.length}>
<FieldLabel htmlFor="street">Street and house number</FieldLabel>
<Input id="street" name="street" placeholder="12 Via di Roma" required />
<Input id="street" name="street" placeholder="12 Via di Roma" aria-invalid={!!errors.street?.length} required />
{errors.street && <FieldError>{errors.street[0]}</FieldError>}
</Field>
</>
)}
{isEuResident === false && (
<Field>
<Field data-invalid={!!errors.customAddress?.length}>
<FieldLabel htmlFor="customAddress">Full address</FieldLabel>
<Input
id="customAddress"
name="customAddress"
placeholder="Street, City, Country"
aria-invalid={!!errors.customAddress?.length}
required
/>
<FieldDescription>
Enter your full address as it should appear on the document.
</FieldDescription>
<FieldDescription>Enter your full address as it should appear on the document.</FieldDescription>
{errors.customAddress && <FieldError>{errors.customAddress[0]}</FieldError>}
</Field>
)}
</FieldGroup>
</FieldSet>
<FieldSet>
<FieldLegend>Additional information</FieldLegend>
<FieldDescription>
Provide details required for your identity document.
</FieldDescription>
<FieldDescription>Provide details required for your identity document.</FieldDescription>
<FieldGroup>
<div className="grid grid-cols-2 gap-4">
<Field>
<Field data-invalid={!!errors.height?.length}>
<FieldLabel htmlFor="height">Height (in cm)</FieldLabel>
<Input
id="height"
@@ -278,40 +260,54 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
name="height"
min={60}
max={240}
aria-invalid={!!errors.height?.length}
required
/>
{errors.height && <FieldError>{errors.height[0]}</FieldError>}
</Field>
<Field>
<Field data-invalid={!!errors.eyeColor?.length}>
<FieldLabel htmlFor="eyeColor">Eye colour</FieldLabel>
<Select name="eyeColor" defaultValue="">
<SelectTrigger id="eyeColor">
<SelectTrigger id="eyeColor" aria-invalid={!!errors.eyeColor?.length}>
<SelectValue placeholder="Select eye colour" />
</SelectTrigger>
<SelectContent>
{EYE_COLORS.map((color) => (
<SelectItem key={color} value={color}>
{color}
</SelectItem>
<SelectItem key={color} value={color}>{color}</SelectItem>
))}
</SelectContent>
</Select>
{errors.eyeColor && <FieldError>{errors.eyeColor[0]}</FieldError>}
</Field>
</div>
</FieldGroup>
</FieldSet>
{state.errors.length > 0 && (
<Alert className="bg-red-50 p-3 text-red-900">
<ul className="list-disc pl-4">
{state.errors.map((err, i) => (
<li key={i}>{err}</li>
))}
</ul>
</Alert>
)}
<FieldSet>
<FieldLegend>Images</FieldLegend>
<FieldDescription>Provide your profile image and your signature for your identity document.</FieldDescription>
<FieldGroup>
<ImageUploadField
id="profile"
name="profile"
label="Profile"
description="Select a profile image to upload. The image should be taken on a neutral background."
error={errors.profile?.[0]}
/>
<ImageUploadField
id="signature"
name="signature"
label="Signature"
description="Select a signature to upload."
error={errors.signature?.[0]}
/>
</FieldGroup>
</FieldSet>
<Field orientation="horizontal">
<Button type="submit" disabled={isPending}>{isPending && <Spinner />}Submit application</Button>
</Field>
</FieldGroup>
</form>
);
}
}
@@ -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<string | null>(null);
const [clientError, setClientError] = useState<string | null>(null);
const MAX_SIZE = 5 * 1024 * 1024;
const ACCEPTED_TYPES = ["image/jpeg", "image/png", "image/webp"];
function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
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 (
<Field data-invalid={!!displayedError}>
<FieldLabel htmlFor={id}>{label}</FieldLabel>
<div className="flex items-center gap-4">
{previewUrl && (
<img
src={previewUrl}
alt=""
className={cn(
"w-16 h-16 rounded-md object-cover ring-1 ring-foreground/10 shrink-0"
)}
/>
)}
<Input
id={id}
name={name}
type="file"
accept="image/jpeg,image/png,image/webp"
aria-invalid={!!displayedError}
onChange={handleChange}
required
/>
</div>
<FieldDescription>{description}</FieldDescription>
{displayedError && <FieldError>{displayedError}</FieldError>}
</Field>
);
}
+20 -13
View File
@@ -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() {
<div className="flex flex-col gap-6">
<h2>Citizenship application</h2>
<div className="w-96 flex flex-col gap-6">
<CitizenshipApplicationForm
firstName={user.firstName}
secondName={user.secondName}
sex={user.sex}
birthDate={user.birthDate}
regions={regions}
/>
{!eligibility.eligible ? (
<Alert variant="warning">
<InfoIcon />
<AlertTitle>Cannot submit application</AlertTitle>
<AlertDescription>{eligibility.reason}</AlertDescription>
</Alert>
) : (
<CitizenshipApplicationForm
firstName={user.firstName}
secondName={user.secondName}
sex={user.sex}
birthDate={user.birthDate!}
regions={regions}
/>
)}
</div>
</div>
</div>
@@ -40,4 +47,4 @@ export default async function CitizenshipApplication() {
<Footer />
</>
);
}
}