'use client'; import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Upload, FileText, Download, AlertCircle } from 'lucide-react'; import { BaseModal } from './base/BaseModal'; import { ErrorDisplay } from './base/ErrorDisplay'; import { parseCSV, parseExcel, getExpectedColumns, downloadTemplate, validateFileType } from '@/lib/file-utils'; interface ImportFileModalProps { isOpen: boolean; onClose: () => void; onImport: (data: any[]) => void; type: 'propositions' | 'participants'; campaignTitle?: string; } export default function ImportFileModal({ isOpen, onClose, onImport, type, campaignTitle }: ImportFileModalProps) { const [file, setFile] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [preview, setPreview] = useState([]); const handleFileChange = async (e: React.ChangeEvent) => { const selectedFile = e.target.files?.[0]; if (selectedFile) { // Valider le type de fichier const validation = validateFileType(selectedFile); if (!validation.isValid) { setError(validation.error || 'Type de fichier non supporté'); return; } setFile(selectedFile); setError(''); // Parser le fichier const isCSV = selectedFile.type === 'text/csv' || selectedFile.name.toLowerCase().endsWith('.csv'); const result = isCSV ? await parseCSV(selectedFile) : await parseExcel(selectedFile); if (result.error) { setError(result.error); return; } setPreview(result.data.slice(0, 5)); // Afficher les 5 premières lignes } }; const handleImport = async () => { if (!file) return; setLoading(true); try { const isCSV = file.type === 'text/csv' || file.name.toLowerCase().endsWith('.csv'); const result = isCSV ? await parseCSV(file) : await parseExcel(file); if (result.error) { setError(result.error); return; } onImport(result.data); onClose(); setFile(null); setPreview([]); } catch (error) { setError('Erreur lors de l\'import du fichier.'); } finally { setLoading(false); } }; const handleClose = () => { setFile(null); setPreview([]); setError(''); onClose(); }; const footer = ( <> ); return ( {/* Template download */}
Téléchargez le modèle
{/* Expected columns */}

Colonnes attendues :

{getExpectedColumns(type).join(', ')}
{/* File upload */}
{/* Preview */} {preview.length > 0 && (
{Object.keys(preview[0] || {}).map((header) => ( ))} {preview.map((row, index) => ( {Object.values(row).map((value, cellIndex) => ( ))} ))}
{header}
{String(value)}
)}
); }