'use client'; import React, { useState } from 'react'; import { questionBankApi } from '@/lib/api'; import { X, Upload, FileSpreadsheet, Check, AlertCircle, Download } from 'lucide-react'; interface ExcelImportModalProps { onSuccess?: () => void; onCancel?: () => void; } export default function ExcelImportModal({ onSuccess, onCancel }: ExcelImportModalProps) { const [excelFile, setExcelFile] = useState(null); const [uploading, setUploading] = useState(false); const [result, setResult] = useState(null); const [error, setError] = useState(null); const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { if (!file.name.endsWith('.xlsx') && !file.name.endsWith('.xls')) { alert('Por favor sube un archivo Excel (.xlsx o .xls)'); return; } setExcelFile(file); setError(null); } }; const handleUpload = async () => { if (!excelFile) { alert('Selecciona un archivo primero'); return; } try { setUploading(true); setError(null); const formData = new FormData(); formData.append('file', excelFile); const response = await fetch(`${process.env.NEXT_PUBLIC_CMS_API_URL || 'http://localhost:3001'}/question-bank/import-excel`, { method: 'POST', headers: { 'Authorization': `Bearer ${localStorage.getItem('token')}`, }, body: formData, }); if (!response.ok) { throw new Error('Error al importar'); } const data = await response.json(); setResult(data); setTimeout(() => { onSuccess?.(); }, 2000); } catch (err: any) { console.error('Excel import failed:', err); setError(err.message || 'Error al importar'); } finally { setUploading(false); } }; const downloadTemplate = () => { // Create a simple template explanation alert('Descargando plantilla...\n\nColumnas requeridas:\n1. question_text - Texto de la pregunta\n2. question_type - multiple-choice, true-false, etc.\n3. options - ["A","B","C","D"]\n4. correct_answer - 0, 1, 2, o 3\n5. explanation - Explicación (opcional)\n6. difficulty - easy, medium, hard\n7. tags - tag1,tag2,tag3'); }; return (
{/* Header */}

Importar desde Excel

Sube preguntas desde un archivo Excel (.xlsx)

{/* Content */}
{/* Info Box */}

¿Cómo funciona?

  • • Crea un Excel con columnas: question_text, question_type, options, correct_answer, explanation, difficulty, tags
  • • question_type: multiple-choice, true-false, short-answer, etc.
  • • options: Formato JSON ["A","B","C","D"] o separado por comas
  • • correct_answer: Índice de la opción correcta (0, 1, 2, 3)
  • • Todas las preguntas se importarán al banco
{/* Download Template Button */}

Plantilla de ejemplo

Descarga una plantilla con el formato correcto

{/* File Upload */}
{excelFile && (
Archivo seleccionado: {excelFile.name} ({(excelFile.size / 1024 / 1024).toFixed(2)} MB)
)}
{/* Result Messages */} {result && (

¡Importación completada!

Importadas: {result.imported}
Saltadas: {result.skipped}
{result.error && (
Errores: {result.error}
)}
)} {error && (

Error

{error}

)}
{/* Actions */}
); }