Overview
TheDocumentUpload component provides a user-friendly file upload interface with drag-and-drop support, document type classification, and automatic metadata extraction. Files are uploaded to the document history system with UUID-based naming.
Location: ~/workspace/source/Fronted/src/components/common/DocumentUpload.jsx:6
Features
- Drag-and-drop file upload interface
- Click-to-browse file selection
- Document type classification dropdown
- File metadata extraction (size, type, extension)
- UUID-based file naming for uniqueness
- Upload progress indication
- File preview before upload
- Dark mode support
- Responsive design
Props
Callback function invoked after successful file upload
The ID of the beneficiary/entity associated with the document. Defaults to ‘GUEST’ if not provided
The name of the beneficiary/entity. Defaults to ‘Anónimo’ if not provided
Import
Usage Examples
- Basic Usage
- With Beneficiary
- In Modal
- Anonymous Upload
Document Types
The component provides a predefined list of document types:- Cédula: Identity card documents
- Tesis / Proyecto: Thesis or project documents
- Acta de Grado: Graduation records
- Diploma: Diploma certificates
- Certificado: General certificates
- Otros: Other document types (default)
Visual States
Empty State (No File Selected)
Displays:- Upload icon with green background
- “Arrastra tu archivo aquí” heading
- “O toca para buscar en tu equipo” subtext
- Interactive drop zone
Active Drag State
- Green border color
- Light green background tint
- Visual feedback for drag-over
File Selected State
Displays:- File icon and metadata card
- File name (truncated if long)
- File size in KB
- Remove file button (X icon)
- Document type selector dropdown
- “Confirmar Carga” button
Uploading State
- Disabled upload button
- Animated spinner icon
- “Subiendo…” text
- Prevents multiple submissions
Upload Flow
- File Selection: User drags file or clicks to browse
- File Validation: File is set in component state
- Type Selection: User selects document type from dropdown
- Confirmation: User clicks “Confirmar Carga”
- Metadata Generation:
- UUID is generated using
crypto.randomUUID() - File extension is extracted
- File size is calculated in MB
- MIME type is captured
- UUID is generated using
- API Upload: Document record is created via
api.history.create() - Success Handling:
- Success toast notification
- File state is cleared
onSuccesscallback is invoked
File Metadata Structure
The component generates comprehensive file metadata:API Payload
The component sends the following payload to the history API:Event Handlers
Drag Events
Drop Event
File Input Change
Styling Features
Responsive Design
- Mobile-friendly layout
- Touch-friendly drop zone
- Responsive button sizing
Animations
- Fade-in animation for file preview
- Scale effect on button hover
- Smooth transitions between states
Dark Mode
- Complete dark mode support
- Appropriate contrast ratios
- Consistent theming with light mode
Error Handling
Upload Failures
Upload Failures
If the API call fails, an error toast is displayed:The uploading state is cleared, allowing the user to retry.
Missing File
Missing File
The upload button is only active when a file is selected. The
handleUpload function includes a guard:Security Considerations
UUID-Based Naming
Files are renamed using UUID v4 to:- Prevent filename collisions
- Avoid path traversal attacks
- Ensure uniqueness across the system
File Validation
The current implementation does not validate file types or sizes. Consider adding:
- File size limits
- MIME type validation
- File extension whitelist
- Malware scanning integration
Dependencies
- lucide-react: Icons (Upload, X, FileText, CheckCircle, AlertCircle, Loader2)
- api.history.create(): History API service
- toast: SweetAlert2 utility for notifications
- crypto.randomUUID(): Native browser UUID generation
Best Practices
Always Provide Beneficiary Context
Always Provide Beneficiary Context
When possible, provide
beneficiaryId and beneficiaryName to properly track document ownership:Implement onSuccess Callback
Implement onSuccess Callback
Always implement the
onSuccess callback to refresh document lists or close modals:Add File Validation
Add File Validation
Consider extending the component to validate:
- Maximum file size (e.g., 10MB limit)
- Allowed file types (e.g., PDF, DOCX, images only)
- File name sanitization
Handle Upload Progress
Handle Upload Progress
For large files, consider integrating upload progress tracking:
Accessibility
- Hidden file input with ref-based triggering
- Keyboard accessible buttons
- Semantic HTML structure
- Clear visual feedback for all states
- Disabled state properly communicated
Notes
The component uses
crypto.randomUUID() which requires a secure context (HTTPS) in production environments.