Files
eu/app/account/citizenship/apply/form.tsx
T
2026-07-04 15:45:05 +03:00

313 lines
13 KiB
TypeScript

"use client";
import { useActionState, useEffect, useState } from "react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import {
Field,
FieldContent,
FieldDescription,
FieldError,
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 } from "@/lib/citizenship/actions";
import { initialFormState } from "@/lib/form-action";
import { EYE_COLORS } from "@/lib/citizenship/types";
import { Spinner } from "@/components/ui/spinner";
import { ImageUploadField } from "./image-upload-field";
type Props = {
firstName: string;
secondName: string;
sex: string;
birthDate: Date;
regions: { id: string; name: string }[];
};
function formatDate(d: Date | null): string {
if (!d) return "";
return d.toISOString().split("T")[0];
}
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, 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>
<FieldSet>
<FieldLegend>Personal information</FieldLegend>
<Alert variant="warning">
<InfoIcon />
<AlertTitle>Important notice</AlertTitle>
<AlertDescription>
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.
</AlertDescription>
</Alert>
<FieldGroup>
<Field>
<FieldLabel htmlFor="surname">Surname</FieldLabel>
<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" />
</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" />
</Field>
<Field>
<FieldLabel htmlFor="birthDate">Date of birth</FieldLabel>
<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>
<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>
<FieldDescription>
I held citizenship of the Duspean Republic or the Country of Reality on 31 March 2026.
</FieldDescription>
</FieldContent>
</Field>
</FieldLabel>
<FieldLabel htmlFor="naturalization">
<Field orientation="horizontal">
<RadioGroupItem value="NATURALIZATION" id="naturalization" />
<FieldContent>
<span className="text-sm font-medium">Naturalisation</span>
<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 data-invalid={!!errors.previousCountry?.length}>
<FieldLabel htmlFor="previousCountry">Country</FieldLabel>
<Select name="previousCountry" defaultValue="">
<SelectTrigger id="previousCountry" aria-invalid={!!errors.previousCountry?.length}>
<SelectValue placeholder="Select country" />
</SelectTrigger>
<SelectContent>
<SelectItem value="DUS">Duspean Republic</SelectItem>
<SelectItem value="REA">Country of Reality</SelectItem>
</SelectContent>
</Select>
{errors.previousCountry && <FieldError>{errors.previousCountry[0]}</FieldError>}
</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>
{errors.previousRegion && <FieldError>{errors.previousRegion[0]}</FieldError>}
</Field>
</FieldGroup>
</FieldSet>
)}
{appType === "NATURALIZATION" && (
<FieldSet>
<FieldLegend>Naturalisation statement</FieldLegend>
<FieldGroup>
<Field data-invalid={!!errors.naturalizationReason?.length}>
<FieldLabel htmlFor="naturalizationReason">
Why do you wish to become a citizen of the European Federation?
</FieldLabel>
<Textarea
id="naturalizationReason"
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>
<FieldGroup>
<Field>
<FieldLabel>Is your residence in the European Federation?</FieldLabel>
<RadioGroup name="isEuResident" onValueChange={(v) => setIsEuResident(v === "yes")}>
<Field orientation="horizontal">
<RadioGroupItem value="yes" id="eu-yes" />
<FieldLabel htmlFor="eu-yes">Yes</FieldLabel>
</Field>
<Field orientation="horizontal">
<RadioGroupItem value="no" id="eu-no" />
<FieldLabel htmlFor="eu-no">No</FieldLabel>
</Field>
</RadioGroup>
</Field>
{isEuResident === true && (
<>
<Field data-invalid={!!errors.regionId?.length}>
<FieldLabel htmlFor="regionId">Region</FieldLabel>
<Select name="regionId" required>
<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>
))}
</SelectContent>
</Select>
{errors.regionId && <FieldError>{errors.regionId[0]}</FieldError>}
</Field>
<Field data-invalid={!!errors.city?.length}>
<FieldLabel htmlFor="city">City</FieldLabel>
<Input id="city" name="city" placeholder="City or town" aria-invalid={!!errors.city?.length} required />
{errors.city && <FieldError>{errors.city[0]}</FieldError>}
</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" aria-invalid={!!errors.street?.length} required />
{errors.street && <FieldError>{errors.street[0]}</FieldError>}
</Field>
</>
)}
{isEuResident === false && (
<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>
{errors.customAddress && <FieldError>{errors.customAddress[0]}</FieldError>}
</Field>
)}
</FieldGroup>
</FieldSet>
<FieldSet>
<FieldLegend>Additional information</FieldLegend>
<FieldDescription>Provide details required for your identity document.</FieldDescription>
<FieldGroup>
<div className="grid grid-cols-2 gap-4">
<Field data-invalid={!!errors.height?.length}>
<FieldLabel htmlFor="height">Height (in cm)</FieldLabel>
<Input
id="height"
type="number"
name="height"
min={60}
max={240}
aria-invalid={!!errors.height?.length}
required
/>
{errors.height && <FieldError>{errors.height[0]}</FieldError>}
</Field>
<Field data-invalid={!!errors.eyeColor?.length}>
<FieldLabel htmlFor="eyeColor">Eye colour</FieldLabel>
<Select name="eyeColor" defaultValue="">
<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>
))}
</SelectContent>
</Select>
{errors.eyeColor && <FieldError>{errors.eyeColor[0]}</FieldError>}
</Field>
</div>
</FieldGroup>
</FieldSet>
<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>
);
}