All files / src/components/company-profile CertificationModal.tsx

100% Statements 43/43
100% Branches 42/42
100% Functions 14/14
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273                                                  8x                       76x 76x 76x 76x     76x 76x 76x 76x   76x 32x                   76x   76x 5x 3x   2x       76x   76x 5x 4x 4x 4x 3x   1x   4x       76x 6x 6x 5x                 76x                                                                                 25x                               2x             228x                               3x                             2x                                       1x                             6x 6x               1x                                                               1x          
import { useState, useMemo, useRef, type FormEvent } from "react";
import { X, Upload, Loader2, X as XIcon } from "lucide-react";
import { notify } from "../../lib/notify";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { ConfirmDialog } from "../ui/confirm-dialog";
import { useUnsavedChanges, useDialogAccessibility } from "../../hooks";
import { getErrorMessage } from "../../lib/api";
import { getImageUrl } from "../../lib/api/base";
import { uploadImage } from "../../lib/upload";
import type { ProCertification } from "../../lib/api";
 
interface CertificationModalProps {
	certification: ProCertification | null;
	proId: string;
	onSave: (data: {
		title: string;
		issuer?: string;
		year?: number;
		type: "certification" | "award" | "membership";
		imageUrl?: string;
	}) => void;
	onClose: () => void;
}
 
const CERTIFICATION_TYPES = [
	{ value: "certification", label: "Certification" },
	{ value: "award", label: "Award" },
	{ value: "membership", label: "Membership" },
];
 
export function CertificationModal({
	certification,
	proId,
	onSave,
	onClose,
}: CertificationModalProps) {
	const [title, setTitle] = useState(certification?.title || "");
	const [issuer, setIssuer] = useState(certification?.issuer || "");
	const [year, setYear] = useState(certification?.year?.toString() || "");
	const [type, setType] = useState<"certification" | "award" | "membership">(
		certification?.type || "certification",
	);
	const [imageUrl, setImageUrl] = useState(certification?.imageUrl || "");
	const [isUploading, setIsUploading] = useState(false);
	const [showDiscardConfirm, setShowDiscardConfirm] = useState(false);
	const fileInputRef = useRef<HTMLInputElement>(null);
 
	const initialValues = useMemo(
		() => ({
			title: certification?.title || "",
			issuer: certification?.issuer || "",
			year: certification?.year?.toString() || "",
			type: certification?.type || "certification",
			imageUrl: certification?.imageUrl || "",
		}),
		[certification],
	);
 
	const isDirty = useUnsavedChanges(initialValues, { title, issuer, year, type, imageUrl });
 
	const handleClose = () => {
		if (isDirty) {
			setShowDiscardConfirm(true);
		} else {
			onClose();
		}
	};
 
	const { dialogRef, handleFocusTrap } = useDialogAccessibility(handleClose);
 
	const handleImageUpload = async (file: File) => {
		if (!proId) return;
		setIsUploading(true);
		try {
			const result = await uploadImage(proId, file, { type: "certification" });
			setImageUrl(result.storageKey);
		} catch (err) {
			notify.error(getErrorMessage(err));
		} finally {
			setIsUploading(false);
		}
	};
 
	const handleSubmit = (e: FormEvent) => {
		e.preventDefault();
		if (!title) return;
		onSave({
			title,
			issuer: issuer || undefined,
			year: year ? Number.parseInt(year, 10) : undefined,
			type,
			imageUrl: imageUrl || undefined,
		});
	};
 
	return (
		<div role="dialog" className="fixed inset-0 flex items-center justify-center z-50" onKeyDown={handleFocusTrap}>
			<button
				type="button"
				aria-label="Close modal"
				className="fixed inset-0 bg-black/50 cursor-default"
				onClick={handleClose}
				tabIndex={-1}
			/>
			<div
				ref={dialogRef}
				role="dialog"
				aria-modal="true"
				aria-labelledby="certification-modal-title"
				tabIndex={-1}
				className="relative bg-background-elevated rounded-lg shadow-xl w-full max-w-md mx-4 focus:outline-none"
			>
				<div className="flex items-center justify-between p-4 border-b">
					<h3 id="certification-modal-title" className="font-semibold">
						{certification ? "Edit Certification" : "Add Certification"}
					</h3>
					<button
						type="button"
						onClick={handleClose}
						aria-label="Close"
						className="text-foreground-subtle hover:text-foreground-muted"
					>
						<X className="h-5 w-5" />
					</button>
				</div>
				<form onSubmit={handleSubmit} className="p-4 space-y-4">
					<div className="space-y-2">
						<label
							htmlFor="cert-title"
							className="text-sm font-medium text-foreground-default"
						>
							Title *
						</label>
						<Input
							id="cert-title"
							value={title}
							onChange={(e) => setTitle(e.target.value)}
							placeholder="e.g., ISO 9001 Certified"
							required
						/>
					</div>
					<div className="space-y-2">
						<label
							htmlFor="cert-type"
							className="text-sm font-medium text-foreground-default"
						>
							Type *
						</label>
						<select
							id="cert-type"
							value={type}
							onChange={(e) =>
								setType(
									e.target.value as "certification" | "award" | "membership",
								)
							}
							className="flex h-10 w-full rounded-md border border-default bg-background-elevated px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
						>
							{CERTIFICATION_TYPES.map((t) => (
								<option key={t.value} value={t.value}>
									{t.label}
								</option>
							))}
						</select>
					</div>
					<div className="space-y-2">
						<label
							htmlFor="cert-issuer"
							className="text-sm font-medium text-foreground-default"
						>
							Issuer
						</label>
						<Input
							id="cert-issuer"
							value={issuer}
							onChange={(e) => setIssuer(e.target.value)}
							placeholder="e.g., Bureau of Indian Standards"
						/>
					</div>
					<div className="space-y-2">
						<label
							htmlFor="cert-year"
							className="text-sm font-medium text-foreground-default"
						>
							Year
						</label>
						<Input
							id="cert-year"
							type="number"
							value={year}
							onChange={(e) => setYear(e.target.value)}
							placeholder="e.g., 2023"
							min="1990"
							max={new Date().getFullYear()}
						/>
					</div>
					{/* Certificate Image */}
					<div className="space-y-2">
						<span className="text-sm font-medium text-foreground-default">
							Certificate Image
						</span>
						{imageUrl ? (
							<div className="flex items-start gap-3">
								<img
									src={getImageUrl(imageUrl)}
									alt="Certificate"
									className="h-20 w-20 rounded-md object-cover border border-default"
								/>
								<button
									type="button"
									onClick={() => setImageUrl("")}
									className="text-foreground-subtle hover:text-notification-error-text p-1"
									aria-label="Remove image"
								>
									<XIcon className="h-4 w-4" />
								</button>
							</div>
						) : (
							<div>
								<input
									ref={fileInputRef}
									type="file"
									accept="image/*"
									className="hidden"
									onChange={(e) => {
										const file = e.target.files?.[0];
										if (file) handleImageUpload(file);
									}}
								/>
								<Button
									type="button"
									variant="outline"
									size="sm"
									disabled={isUploading}
									onClick={() => fileInputRef.current?.click()}
								>
									{isUploading ? (
										<>
											<Loader2 className="h-4 w-4 mr-2 animate-spin" />
											Uploading...
										</>
									) : (
										<>
											<Upload className="h-4 w-4 mr-2" />
											Upload Certificate Image
										</>
									)}
								</Button>
							</div>
						)}
					</div>
					<div className="flex justify-end gap-2 pt-4">
						<Button type="button" variant="outline" onClick={handleClose}>
							Cancel
						</Button>
						<Button type="submit">{certification ? "Update" : "Add"}</Button>
					</div>
				</form>
			</div>
			<ConfirmDialog
				open={showDiscardConfirm}
				title="Discard unsaved changes?"
				description="You have unsaved changes that will be lost if you close this form."
				confirmLabel="Discard"
				variant="destructive"
				onConfirm={onClose}
				onCancel={() => setShowDiscardConfirm(false)}
			/>
		</div>
	);
}