This commit is contained in:
Ivan Rezchikov
2026-07-11 21:21:38 +03:00
parent 18161f4604
commit 4c42b5aa1d
20 changed files with 369 additions and 260 deletions
+46 -7
View File
@@ -1,6 +1,6 @@
"use client";
import { useActionState, useEffect, useState } from "react";
import { useActionState, useEffect, useRef, useState } from "react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import {
@@ -29,7 +29,8 @@ 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";
import { ImageUploader } from "@/components/image-uploader";
import { removeBackground } from "@imgly/background-removal";
type Props = {
firstName: string;
@@ -50,6 +51,9 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
const [appType, setAppType] = useState<AppType | "">("");
const [state, formAction, isPending] = useActionState(submitCitizenshipApplication, initialFormState);
const [isEuResident, setIsEuResident] = useState<boolean | null>(null);
const [profileProcessing, setProfileProcessing] = useState(false);
const profileInputRef = useRef<HTMLInputElement>(null);
const signatureInputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (state.error) {
@@ -59,6 +63,37 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
const errors = state.fieldErrors ?? {};
async function handleProfileSelect(file: File) {
setProfileProcessing(true);
try {
const resultBlob = await removeBackground(file);
const processedFile = new File([resultBlob], file.name, { type: "image/png" });
const dt = new DataTransfer();
dt.items.add(processedFile);
if (profileInputRef.current) profileInputRef.current.files = dt.files;
} catch {
const dt = new DataTransfer();
dt.items.add(file);
if (profileInputRef.current) profileInputRef.current.files = dt.files;
} finally {
setProfileProcessing(false);
}
}
function handleSignatureSelect(file: File) {
const dt = new DataTransfer();
dt.items.add(file);
if (signatureInputRef.current) signatureInputRef.current.files = dt.files;
}
function handleProfileRemove() {
if (profileInputRef.current) profileInputRef.current.value = "";
}
function handleSignatureRemove() {
if (signatureInputRef.current) signatureInputRef.current.value = "";
}
return (
<form action={formAction}>
<FieldGroup>
@@ -287,22 +322,26 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
<FieldLegend>Images</FieldLegend>
<FieldDescription>Provide your profile image and your signature for your identity document.</FieldDescription>
<FieldGroup>
<ImageUploadField
<input ref={profileInputRef} type="hidden" name="profile" />
<input ref={signatureInputRef} type="hidden" name="signature" />
<ImageUploader
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]}
previewClassName="aspect-4/5"
removeBg
onFileSelect={handleProfileSelect}
onRemove={handleProfileRemove}
isUploading={profileProcessing}
/>
<ImageUploadField
<ImageUploader
id="signature"
name="signature"
label="Signature"
description="Select a signature to upload."
error={errors.signature?.[0]}
previewClassName="aspect-5/2"
onFileSelect={handleSignatureSelect}
onRemove={handleSignatureRemove}
/>
</FieldGroup>
</FieldSet>