améliore l'import (normalisation colonnes)
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -39,3 +39,4 @@ yarn-error.log*
|
||||
# typescript
|
||||
*.tsbuildinfo
|
||||
next-env.d.ts
|
||||
test-74-yald.ods
|
||||
|
||||
@@ -8,7 +8,7 @@ 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';
|
||||
import { parseCSV, parseExcel, getExpectedColumns, downloadTemplate, validateFileType, normalizeParsedData } from '@/lib/file-utils';
|
||||
|
||||
interface ImportFileModalProps {
|
||||
isOpen: boolean;
|
||||
@@ -52,7 +52,9 @@ export default function ImportFileModal({
|
||||
return;
|
||||
}
|
||||
|
||||
setPreview(result.data.slice(0, 5)); // Afficher les 5 premières lignes
|
||||
// Normaliser les données pour correspondre aux colonnes attendues
|
||||
const normalizedData = normalizeParsedData(result.data, type);
|
||||
setPreview(normalizedData.slice(0, 5)); // Afficher les 5 premières lignes
|
||||
}
|
||||
};
|
||||
|
||||
@@ -69,7 +71,9 @@ export default function ImportFileModal({
|
||||
return;
|
||||
}
|
||||
|
||||
onImport(result.data);
|
||||
// Normaliser les données pour correspondre aux colonnes attendues
|
||||
const normalizedData = normalizeParsedData(result.data, type);
|
||||
onImport(normalizedData);
|
||||
onClose();
|
||||
setFile(null);
|
||||
setPreview([]);
|
||||
|
||||
@@ -132,6 +132,55 @@ export function getExpectedColumns(type: 'propositions' | 'participants'): strin
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalise les noms de colonnes pour améliorer la compatibilité
|
||||
*/
|
||||
export function normalizeColumnName(columnName: string): string {
|
||||
if (!columnName) return '';
|
||||
|
||||
const normalized = columnName.toLowerCase().trim();
|
||||
|
||||
// Mappings pour les colonnes communes
|
||||
const mappings: { [key: string]: string } = {
|
||||
'email': 'Email',
|
||||
'e-mail': 'Email',
|
||||
'mail': 'Email',
|
||||
'courriel': 'Email',
|
||||
'prénom': 'Prénom',
|
||||
'prenom': 'Prénom',
|
||||
'firstname': 'Prénom',
|
||||
'first_name': 'Prénom',
|
||||
'nom': 'Nom',
|
||||
'lastname': 'Nom',
|
||||
'last_name': 'Nom',
|
||||
'titre': 'Titre',
|
||||
'title': 'Titre',
|
||||
'description': 'Description',
|
||||
'desc': 'Description'
|
||||
};
|
||||
|
||||
return mappings[normalized] || columnName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalise les données parsées pour correspondre aux colonnes attendues
|
||||
*/
|
||||
export function normalizeParsedData(data: any[], type: 'propositions' | 'participants'): any[] {
|
||||
const expectedColumns = getExpectedColumns(type);
|
||||
|
||||
return data.map(row => {
|
||||
const normalizedRow: any = {};
|
||||
|
||||
// Normaliser chaque colonne
|
||||
Object.keys(row).forEach(key => {
|
||||
const normalizedKey = normalizeColumnName(key);
|
||||
normalizedRow[normalizedKey] = row[key];
|
||||
});
|
||||
|
||||
return normalizedRow;
|
||||
});
|
||||
}
|
||||
|
||||
export async function downloadTemplate(type: 'propositions' | 'participants'): Promise<void> {
|
||||
const columns = getExpectedColumns(type);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user