Spaces:
Runtime error
Runtime error
File size: 6,320 Bytes
6aa6081 | 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 | 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">×</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>
);
}
|