talos-access / frontend /src /components /BulkUploadModal.tsx
Greg
Claude Opus 4.7 (1M context)
Preparar despliegue en fly.io: monolito dockerizado + seeder demo
6aa6081
Raw History Blame Contribute Delete
6.32 kB
import { useState, useRef } from 'react';
import { Upload, Download, X, Loader2, AlertCircle, CheckCircle2 } from 'lucide-react';
import api from '../services/api';
interface BulkUploadModalProps {
open: boolean;
onClose: () => void;
title: string;
templateEndpoint: string;
uploadEndpoint: string;
templateFilename: string;
description?: string;
onSuccess?: () => void;
}
interface BulkResult {
created: number;
errors: { row: number; error: string }[];
}
export default function BulkUploadModal({
open, onClose, title, templateEndpoint, uploadEndpoint, templateFilename, description, onSuccess,
}: BulkUploadModalProps) {
const [file, setFile] = useState<File | null>(null);
const [loading, setLoading] = useState(false);
const [result, setResult] = useState<BulkResult | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const handleDownloadTemplate = async () => {
try {
const { data } = await api.get(templateEndpoint, { responseType: 'blob' });
const url = URL.createObjectURL(data);
const a = document.createElement('a');
a.href = url;
a.download = templateFilename;
a.click();
URL.revokeObjectURL(url);
} catch {
alert('Error al descargar plantilla');
}
};
const handleUpload = async () => {
if (!file) return;
setLoading(true);
setResult(null);
try {
const formData = new FormData();
formData.append('file', file);
const { data } = await api.post(uploadEndpoint, formData);
setResult(data);
if (data.errors?.length === 0 && data.created > 0) {
setTimeout(() => { onSuccess?.(); onClose(); }, 1500);
}
} catch (err: any) {
alert(err?.response?.data?.detail || 'Error al procesar archivo');
} finally {
setLoading(false);
}
};
const reset = () => {
setFile(null);
setResult(null);
setLoading(false);
if (inputRef.current) inputRef.current.value = '';
};
const handleClose = () => {
reset();
onClose();
};
if (!open) return null;
return (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4" onClick={handleClose}>
<div className="bg-white rounded-2xl shadow-2xl max-w-lg w-full max-h-[90vh] overflow-y-auto" onClick={(e) => e.stopPropagation()}>
<div className="sticky top-0 bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between rounded-t-2xl">
<h2 className="text-xl font-bold">{title}</h2>
<button onClick={handleClose} className="text-gray-400 hover:text-gray-600 text-2xl leading-none">&times;</button>
</div>
<div className="p-6 space-y-4">
{description && <p className="text-sm text-gray-500">{description}</p>}
<div className="bg-blue-50 border border-blue-200 rounded-xl p-4">
<p className="text-sm text-blue-800 font-medium mb-2">Instrucciones:</p>
<ol className="text-sm text-blue-700 space-y-1 ml-4 list-decimal">
<li>Descarga la plantilla CSV</li>
<li>Llena el archivo con los datos requeridos</li>
<li>Sube el archivo completo para procesar la carga masiva</li>
</ol>
</div>
<button
onClick={handleDownloadTemplate}
className="w-full border-2 border-dashed border-gray-300 rounded-xl py-8 flex flex-col items-center gap-2 hover:border-blue-400 hover:bg-blue-50/50 transition-colors cursor-pointer"
>
<Download size={24} className="text-blue-600" />
<span className="text-sm font-medium text-blue-600">Descargar Plantilla</span>
<span className="text-xs text-gray-400">CSV</span>
</button>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">Seleccionar archivo CSV</label>
<input
ref={inputRef}
type="file"
accept=".csv"
onChange={(e) => { setFile(e.target.files?.[0] || null); setResult(null); }}
className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm file:mr-3 file:py-1 file:px-3 file:rounded file:border-0 file:bg-blue-50 file:text-blue-700 file:text-sm file:font-medium hover:file:bg-blue-100"
/>
{file && <p className="text-xs text-gray-500 mt-1">Archivo: {file.name}</p>}
</div>
{result && (
<div className={`rounded-xl border p-4 ${result.errors?.length > 0 ? 'border-amber-200 bg-amber-50' : 'border-green-200 bg-green-50'}`}>
<div className="flex items-center gap-2 mb-2">
{result.errors?.length > 0 ? (
<AlertCircle size={18} className="text-amber-600" />
) : (
<CheckCircle2 size={18} className="text-green-600" />
)}
<span className="font-medium text-sm">
{result.created} registro{result.created !== 1 ? 's' : ''} creado{result.created !== 1 ? 's' : ''}
</span>
</div>
{result.errors?.length > 0 && (
<div className="max-h-40 overflow-y-auto space-y-1 mt-2">
{result.errors.map((e, i) => (
<p key={i} className="text-xs text-red-600">Fila {e.row}: {e.error}</p>
))}
</div>
)}
</div>
)}
<div className="flex justify-end gap-2 pt-2">
<button type="button" className="bg-gray-200 text-gray-700 px-4 py-2 rounded-lg hover:bg-gray-300 transition text-sm inline-flex items-center justify-center gap-2" onClick={handleClose}>
Cancelar
</button>
<button
type="button"
className="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition text-sm inline-flex items-center justify-center gap-2 disabled:opacity-50"
onClick={handleUpload}
disabled={!file || loading}
>
{loading ? <Loader2 size={16} className="animate-spin" /> : <Upload size={16} />}
{loading ? 'Procesando...' : 'Subir Archivo'}
</button>
</div>
</div>
</div>
</div>
);
}