prod
This commit is contained in:
@@ -64,7 +64,7 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
|
||||
<FieldGroup>
|
||||
<FieldSet>
|
||||
<FieldLegend>Personal information</FieldLegend>
|
||||
<Alert variant="warning">
|
||||
<Alert className="mt-2" variant="warning">
|
||||
<InfoIcon />
|
||||
<AlertTitle>Important notice</AlertTitle>
|
||||
<AlertDescription>
|
||||
@@ -251,7 +251,7 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
|
||||
<FieldLegend>Additional information</FieldLegend>
|
||||
<FieldDescription>Provide details required for your identity document.</FieldDescription>
|
||||
<FieldGroup>
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="grid md:grid-cols-2 gap-4">
|
||||
<Field data-invalid={!!errors.height?.length}>
|
||||
<FieldLabel htmlFor="height">Height (in cm)</FieldLabel>
|
||||
<Input
|
||||
@@ -293,6 +293,8 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
|
||||
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
|
||||
/>
|
||||
<ImageUploadField
|
||||
id="signature"
|
||||
@@ -300,6 +302,7 @@ export function CitizenshipApplicationForm({ firstName, secondName, sex, birthDa
|
||||
label="Signature"
|
||||
description="Select a signature to upload."
|
||||
error={errors.signature?.[0]}
|
||||
previewClassName="aspect-5/2"
|
||||
/>
|
||||
</FieldGroup>
|
||||
</FieldSet>
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRef, useState } from "react";
|
||||
import { Field, FieldDescription, FieldError, FieldLabel } from "@/components/ui/field";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { XIcon, ImageIcon } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { removeBackground } from "@imgly/background-removal";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
|
||||
type Props = {
|
||||
id: string;
|
||||
@@ -11,41 +15,74 @@ type Props = {
|
||||
label: string;
|
||||
description: string;
|
||||
error?: string;
|
||||
previewClassName?: string;
|
||||
removeBg?: boolean;
|
||||
};
|
||||
|
||||
export function ImageUploadField({ id, name, label, description, error }: Props) {
|
||||
const MAX_SIZE = 5 * 1024 * 1024;
|
||||
const ACCEPTED_TYPES = ["image/jpeg", "image/png", "image/webp"];
|
||||
|
||||
function formatFileSize(bytes: number): string {
|
||||
return bytes < 1024 * 1024 ? `${Math.round(bytes / 1024)} KB` : `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
||||
}
|
||||
|
||||
export function ImageUploadField({ id, name, label, description, error, previewClassName, removeBg }: Props) {
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
|
||||
const [fileInfo, setFileInfo] = useState<{ name: string; size: number } | null>(null);
|
||||
const [clientError, setClientError] = useState<string | null>(null);
|
||||
const [isProcessing, setIsProcessing] = useState(false);
|
||||
|
||||
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];
|
||||
function reset() {
|
||||
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
||||
setPreviewUrl(null);
|
||||
setFileInfo(null);
|
||||
if (inputRef.current) inputRef.current.value = "";
|
||||
}
|
||||
|
||||
if (!file) {
|
||||
setPreviewUrl(null);
|
||||
setClientError(null);
|
||||
return;
|
||||
}
|
||||
async function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) { reset(); setClientError(null); return; }
|
||||
|
||||
if (!ACCEPTED_TYPES.includes(file.type)) {
|
||||
setClientError("Only JPEG, PNG, or WEBP images are allowed.");
|
||||
setPreviewUrl(null);
|
||||
e.target.value = "";
|
||||
reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if (file.size > MAX_SIZE) {
|
||||
setClientError("Image must be smaller than 5MB.");
|
||||
setPreviewUrl(null);
|
||||
e.target.value = "";
|
||||
reset();
|
||||
return;
|
||||
}
|
||||
|
||||
setClientError(null);
|
||||
setPreviewUrl(URL.createObjectURL(file));
|
||||
|
||||
if (!removeBg) {
|
||||
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
||||
setPreviewUrl(URL.createObjectURL(file));
|
||||
setFileInfo({ name: file.name, size: file.size });
|
||||
return;
|
||||
}
|
||||
|
||||
setIsProcessing(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 (inputRef.current) inputRef.current.files = dt.files;
|
||||
|
||||
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
||||
setPreviewUrl(URL.createObjectURL(processedFile));
|
||||
setFileInfo({ name: processedFile.name, size: processedFile.size });
|
||||
} catch {
|
||||
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
||||
setPreviewUrl(URL.createObjectURL(file));
|
||||
setFileInfo({ name: file.name, size: file.size });
|
||||
} finally {
|
||||
setIsProcessing(false);
|
||||
}
|
||||
}
|
||||
|
||||
const displayedError = clientError ?? error;
|
||||
@@ -53,27 +90,47 @@ export function ImageUploadField({ id, name, label, description, error }: Props)
|
||||
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"
|
||||
)}
|
||||
<div className="flex flex-col md:flex-row items-start gap-4">
|
||||
<div
|
||||
className={cn(
|
||||
"w-28 rounded-md border border-dashed border-foreground/15 bg-muted flex items-center justify-center overflow-hidden shrink-0",
|
||||
previewClassName ?? "aspect-4/5"
|
||||
)}
|
||||
>
|
||||
{isProcessing ? (
|
||||
<Spinner className="text-muted-foreground" />
|
||||
) : previewUrl ? (
|
||||
<img src={previewUrl} alt="" className="w-full h-full object-cover" />
|
||||
) : (
|
||||
<ImageIcon className="text-muted-foreground" size={24} />
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col gap-1 flex-1 min-w-0">
|
||||
<Input
|
||||
ref={inputRef}
|
||||
id={id}
|
||||
name={name}
|
||||
type="file"
|
||||
accept="image/jpeg,image/png,image/webp"
|
||||
aria-invalid={!!displayedError}
|
||||
onChange={handleChange}
|
||||
disabled={isProcessing}
|
||||
required={!fileInfo}
|
||||
/>
|
||||
)}
|
||||
<Input
|
||||
id={id}
|
||||
name={name}
|
||||
type="file"
|
||||
accept="image/jpeg,image/png,image/webp"
|
||||
aria-invalid={!!displayedError}
|
||||
onChange={handleChange}
|
||||
required
|
||||
/>
|
||||
{fileInfo && (
|
||||
<div className="flex items-center gap-2 text-sm text-muted-foreground w-full min-w-0">
|
||||
<span className="flex-1 min-w-0 truncate">
|
||||
{fileInfo.name} ({formatFileSize(fileInfo.size)})
|
||||
</span>
|
||||
<Button type="button" variant="ghost" size="icon" className="h-6 w-6" onClick={reset} disabled={isProcessing}>
|
||||
<XIcon size={14} />
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
<FieldDescription className="mt-1">{description}</FieldDescription>
|
||||
</div>
|
||||
</div>
|
||||
<FieldDescription>{description}</FieldDescription>
|
||||
{displayedError && <FieldError>{displayedError}</FieldError>}
|
||||
</Field>
|
||||
);
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import Footer from "@/components/footer";
|
||||
import Header from "@/components/header";
|
||||
import { getCurrentUser } from "@/lib/user";
|
||||
import { redirect } from "next/navigation";
|
||||
import { AccountMenu } from "../../account-menu";
|
||||
@@ -17,34 +15,30 @@ export default async function CitizenshipApplication() {
|
||||
const regions = await prisma.region.findMany({ orderBy: { name: "asc" } });
|
||||
|
||||
return (
|
||||
<>
|
||||
<Header />
|
||||
<div className="flex flex-row py-16 gap-8">
|
||||
<AccountMenu />
|
||||
<div className="flex flex-col gap-8">
|
||||
<div className="flex flex-col gap-6">
|
||||
<h2>Citizenship application</h2>
|
||||
<div className="w-96 flex flex-col gap-6">
|
||||
{!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 className="flex flex-col md:flex-row p-4 md:p-16 gap-8 md:gap-16 mb-4 md:mb-0">
|
||||
<AccountMenu />
|
||||
<div className="flex flex-col gap-8 w-full">
|
||||
<div className="flex flex-col gap-6">
|
||||
<h2>Citizenship application</h2>
|
||||
<div className="max-w-96 w-full flex flex-col gap-6">
|
||||
{!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>
|
||||
<Footer />
|
||||
</>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user