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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | 1x 1x 1x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 57x 6x 70x 56x 56x 16x 70x 20x 3x 3x 3x 3x 17x 1x 1x 1x 1x 16x 70x 20x 20x 20x 4x 16x 16x 16x 16x 16x 8x 8x 8x 8x 8x 3x 3x 3x 3x 3x 3x 3x 70x 17x 17x 17x 17x 70x 2x 2x 2x 70x 1x 1x 1x 1x 70x 70x 3x 3x 3x 3x 3x 3x 70x 34x 34x 70x 70x 2x 2x 70x 1x 1x 1x 70x 7x 63x | /**
* ProfilePhotoUpload Component
* Story 6.2b: Speaker Profile Update Portal - AC7 (Profile Photo Upload)
*
* Features:
* - Drag-and-drop support (AC7.1)
* - Click to browse (AC7.2)
* - File type validation - JPEG, PNG, SVG (AC7.3; Story 12.12 follow-up aligned the
* list to the backend ProfilePictureService allow-list — webp was frontend-only)
* - File size validation - max 5MB (AC7.4)
* - Image preview after selection (AC7.5)
* - Upload uses presigned URL (AC7.6)
* - Progress indicator during upload (AC7.7)
* - Photo updates immediately after upload (AC7.8)
*/
import { useState, useCallback, useRef, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Upload, X, AlertCircle } from 'lucide-react';
import { BATbernLoader } from '@components/shared/BATbernLoader';
// Code review 2026-05-18 (D1): photo upload moved from the deleted speaker-portal endpoints
// to the CUMS /api/v1/users/me/picture/* endpoints. The eventCode prop is gone — profile
// photo lives on User, not per-event.
import { uploadProfilePicture } from '@/services/api/userAccountApi';
// Constants
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB (AC7.4)
// (AC7.3) — Story 12.12 follow-up: mirror the backend ProfilePictureService allow-list
// EXACTLY (png/jpg/jpeg/svg → image/jpeg, image/png, image/svg+xml). webp was
// frontend-only and 400'd at the presigned-url phase (InvalidFileTypeException).
const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'image/svg+xml'];
interface ProfilePhotoUploadProps {
/** Current profile photo URL (null if none) */
currentPhotoUrl: string | null | undefined;
/** Callback when photo is successfully uploaded */
onPhotoUploaded: (url: string) => void;
/** Callback when an error occurs */
onError: (error: { type: string; message: string }) => void;
}
/**
* Profile photo upload component with drag-and-drop support.
* Uses presigned URL pattern for direct S3 upload via the CUMS user-picture endpoints.
*/
const ProfilePhotoUpload = ({
currentPhotoUrl,
onPhotoUploaded,
onError,
}: ProfilePhotoUploadProps) => {
const { t } = useTranslation();
// State
const [isUploading, setIsUploading] = useState(false);
const [uploadProgress, setUploadProgress] = useState(0);
const [error, setError] = useState<string | null>(null);
const [isDragging, setIsDragging] = useState(false);
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
const [showDropzone, setShowDropzone] = useState(!currentPhotoUrl);
// Refs
const fileInputRef = useRef<HTMLInputElement>(null);
const dropzoneRef = useRef<HTMLDivElement>(null);
// Update showDropzone when currentPhotoUrl changes
useEffect(() => {
if (currentPhotoUrl && !isUploading) {
setShowDropzone(false);
}
}, [currentPhotoUrl, isUploading]);
// Clean up preview URL on unmount
useEffect(() => {
return () => {
if (previewUrl) {
URL.revokeObjectURL(previewUrl);
}
};
}, [previewUrl]);
/**
* Validate file type and size
*/
const validateFile = useCallback(
(file: File): boolean => {
// Validate file type (AC7.3)
if (!ALLOWED_TYPES.includes(file.type)) {
const errorMsg = t('speakerPortal.photo.invalidFileType');
setError(errorMsg);
onError({ type: 'INVALID_FILE_TYPE', message: errorMsg });
return false;
}
// Validate file size (AC7.4)
if (file.size > MAX_FILE_SIZE) {
const errorMsg = t('speakerPortal.photo.fileTooLarge');
setError(errorMsg);
onError({ type: 'FILE_TOO_LARGE', message: errorMsg });
return false;
}
return true;
},
[onError, t]
);
/**
* Handle file upload
*/
const handleUpload = useCallback(
async (file: File) => {
// Reset state
setError(null);
setUploadProgress(0);
// Validate file
if (!validateFile(file)) {
return;
}
// Create preview (AC7.5)
const objectUrl = URL.createObjectURL(file);
setPreviewUrl(objectUrl);
setIsUploading(true);
try {
// Upload using CUMS user-picture endpoints (3-phase presigned URL flow). Returns the
// CDN URL string directly.
const uploadedUrl = await uploadProfilePicture(file, (progress) =>
setUploadProgress(progress)
);
// Clean up preview and notify success
URL.revokeObjectURL(objectUrl);
setPreviewUrl(null);
setIsUploading(false);
setUploadProgress(0);
onPhotoUploaded(uploadedUrl);
} catch (err) {
// Clean up preview on error
URL.revokeObjectURL(objectUrl);
setPreviewUrl(null);
setIsUploading(false);
setUploadProgress(0);
const errorMsg = err instanceof Error ? err.message : 'Upload failed';
setError(errorMsg);
onError({ type: 'UPLOAD_FAILED', message: errorMsg });
}
},
[validateFile, onPhotoUploaded, onError]
);
/**
* Handle file input change
*/
const handleFileChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
Eif (file) {
handleUpload(file);
}
// Reset input to allow selecting same file again
event.target.value = '';
},
[handleUpload]
);
/**
* Handle drag events
*/
const handleDragEnter = useCallback((e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(true);
}, []);
const handleDragLeave = useCallback((e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
// Only set to false if we're leaving the dropzone entirely
Eif (e.currentTarget === e.target) {
setIsDragging(false);
}
}, []);
const handleDragOver = useCallback((e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
}, []);
const handleDrop = useCallback(
(e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
const file = e.dataTransfer.files?.[0];
Eif (file) {
handleUpload(file);
}
},
[handleUpload]
);
/**
* Handle click on dropzone
*/
const handleDropzoneClick = useCallback(() => {
Eif (!isUploading) {
fileInputRef.current?.click();
}
}, [isUploading]);
/**
* Handle keyboard activation of dropzone
*/
const handleDropzoneKeyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
handleDropzoneClick();
}
},
[handleDropzoneClick]
);
/**
* Handle "Change Photo" button click
*/
const handleChangePhoto = useCallback(() => {
setShowDropzone(true);
setError(null);
}, []);
/**
* Handle "Cancel" button click
*/
const handleCancel = useCallback(() => {
setShowDropzone(false);
setError(null);
setPreviewUrl(null);
}, []);
// Render photo display (when photo exists and not changing)
if (!showDropzone && currentPhotoUrl) {
return (
<div className="flex flex-col items-center gap-4">
<div className="relative">
<img
src={currentPhotoUrl}
alt="Profile photo"
className="w-32 h-32 rounded-full object-cover border-4 border-zinc-700"
/>
</div>
<button
type="button"
onClick={handleChangePhoto}
className="text-sm text-blue-400 hover:text-blue-300 transition-colors"
>
{t('speakerPortal.photo.changePhoto')}
</button>
</div>
);
}
// Render dropzone
return (
<div className="flex flex-col items-center gap-4">
{/* Preview during upload (AC7.5) */}
{previewUrl && (
<img
data-testid="photo-preview"
src={previewUrl}
alt="Photo preview"
className="w-32 h-32 rounded-full object-cover border-4 border-zinc-700"
/>
)}
{/* Dropzone (AC7.1, AC7.2) */}
{!previewUrl && (
<div
ref={dropzoneRef}
data-testid="photo-dropzone"
role="button"
tabIndex={0}
aria-disabled={isUploading}
onClick={handleDropzoneClick}
onKeyDown={handleDropzoneKeyDown}
onDragEnter={handleDragEnter}
onDragLeave={handleDragLeave}
onDragOver={handleDragOver}
onDrop={handleDrop}
className={`
w-32 h-32 rounded-full flex flex-col items-center justify-center
border-2 border-dashed transition-all cursor-pointer
${isDragging ? 'border-blue-500 bg-blue-900/20' : 'border-zinc-600 hover:border-zinc-500'}
${isUploading ? 'opacity-50 cursor-not-allowed' : ''}
`}
>
{/* Hidden file input inside dropzone for test accessibility */}
<input
ref={fileInputRef}
type="file"
accept={ALLOWED_TYPES.join(',')}
onChange={handleFileChange}
className="hidden"
aria-label={t('speakerPortal.photo.uploadAria')}
/>
{isUploading ? <BATbernLoader size={64} /> : <Upload className="h-8 w-8 text-zinc-400" />}
</div>
)}
{/* Upload progress (AC7.7) */}
{isUploading && (
<div className="w-full max-w-xs" role="status" aria-live="polite">
<div className="flex justify-between text-sm text-zinc-400 mb-1">
<span>{t('speakerPortal.photo.uploading')}</span>
<span>{uploadProgress}%</span>
</div>
<div className="h-2 bg-zinc-700 rounded-full overflow-hidden">
<div
role="progressbar"
aria-valuenow={uploadProgress}
aria-valuemin={0}
aria-valuemax={100}
className="h-full bg-blue-500 transition-all duration-300"
style={{ width: `${uploadProgress}%` }}
/>
</div>
</div>
)}
{/* Instructions */}
{!isUploading && !previewUrl && (
<div className="text-center">
<p className="text-sm text-zinc-400">
{t('speakerPortal.photo.dragDropHere')}
<br />
{t('speakerPortal.photo.orClickBrowse')}
</p>
<p className="text-xs text-zinc-500 mt-1">{t('speakerPortal.photo.supportedFormats')}</p>
</div>
)}
{/* Error message */}
{error && (
<div
role="alert"
className="flex items-center gap-2 text-sm text-red-400 bg-red-900/20 px-3 py-2 rounded-lg"
>
<AlertCircle className="h-4 w-4 flex-shrink-0" />
<span>{error}</span>
</div>
)}
{/* Cancel button (when changing existing photo) */}
{currentPhotoUrl && !isUploading && (
<button
type="button"
onClick={handleCancel}
className="flex items-center gap-1 text-sm text-zinc-400 hover:text-zinc-300 transition-colors"
aria-label={t('speakerPortal.photo.cancel')}
>
<X className="h-4 w-4" />
{t('speakerPortal.photo.cancel')}
</button>
)}
</div>
);
};
export default ProfilePhotoUpload;
|