import { useState, useCallback } from 'react'; import { useDropzone } from 'react-dropzone'; import axios from 'axios'; import { Upload, FileText, X, CheckCircle, AlertCircle, Loader } from 'lucide-react'; function FileUpload({ onAnalysisComplete }) { const [file, setFile] = useState(null); const [uploading, setUploading] = useState(false); const [progress, setProgress] = useState(0); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const onDrop = useCallback((acceptedFiles) => { if (acceptedFiles.length > 0) { setFile(acceptedFiles[0]); setError(null); setSuccess(false); } }, []); const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop, accept: { 'application/pdf': ['.pdf'], 'application/vnd.ms-excel': ['.xls'], 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['.xlsx'], 'text/csv': ['.csv'] }, maxFiles: 1, maxSize: 10 * 1024 * 1024 // 10MB }); const handleUpload = async () => { if (!file) return; setUploading(true); setError(null); setProgress(0); const formData = new FormData(); formData.append('file', file); try { const response = await axios.post('/api/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, onUploadProgress: (progressEvent) => { const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total); setProgress(percentCompleted); } }); setSuccess(true); setUploading(false); // Pass analysis data to parent if (onAnalysisComplete) { onAnalysisComplete(response.data); } } catch (err) { setError(err.response?.data?.message || 'Upload failed. Please try again.'); setUploading(false); setProgress(0); } }; const removeFile = () => { setFile(null); setError(null); setSuccess(false); setProgress(0); }; return (
Upload PDF, Excel, or CSV files containing disaster management training data
Drop the file here...
) : ( <>Drag & drop your file here
or click to browse files
Supported formats: PDF, Excel (.xlsx, .xls), CSV (Max 10MB)
> )}{file.name}
{(file.size / 1024 / 1024).toFixed(2)} MB