feat: images upload
This commit is contained in:
@@ -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>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user