Erste ansprechende Version
This commit is contained in:
80
seniorendienst-frontend/src/components/CSVImport.jsx
Normal file
80
seniorendienst-frontend/src/components/CSVImport.jsx
Normal file
@@ -0,0 +1,80 @@
|
||||
// seniorendienst-frontend/src/components/CSVImport.jsx
|
||||
import React, { useRef, useState } from 'react';
|
||||
|
||||
const CSVImport = ({ onImport }) => {
|
||||
const fileInputRef = useRef(null);
|
||||
const [isProcessing, setIsProcessing] = useState(false);
|
||||
|
||||
const handleFileUpload = async (event) => {
|
||||
const file = event.target.files[0];
|
||||
if (!file) return;
|
||||
|
||||
setIsProcessing(true);
|
||||
|
||||
try {
|
||||
const text = await file.text();
|
||||
const lines = text.split('\n').filter(line => line.trim());
|
||||
|
||||
// Erste Zeile als Header überspringen
|
||||
const dataLines = lines.slice(1);
|
||||
|
||||
const entries = dataLines.map(line => {
|
||||
// CSV-Zeile parsen (einfaches Komma-getrennt)
|
||||
const columns = line.split(',').map(col => col.trim());
|
||||
|
||||
// Erwartetes Format: Name,Vorname,Straße,PLZ/Ort,Telefon,Email,Anfrage,Termin,Zeit,Fahrtzeit,Strecke,Transport,Dauer,Durchgeführt,Bemerkungen,Bezahlt
|
||||
return {
|
||||
name: columns[0] || '',
|
||||
firstName: columns[1] || '',
|
||||
street: columns[2] || '',
|
||||
zipCity: columns[3] || '',
|
||||
phone: columns[4] || '',
|
||||
email: columns[5] || '',
|
||||
requestDate: columns[6] ? new Date(columns[6]) : new Date(),
|
||||
appointmentDate: columns[7] && columns[8] ? new Date(`${columns[7]}T${columns[8]}`) : new Date(),
|
||||
travelTime: Number(columns[9]) || 0,
|
||||
distance: Number(columns[10]) || 0,
|
||||
transport: columns[11] || 'Auto',
|
||||
workDuration: Number(columns[12]) || 0,
|
||||
taskDone: columns[13] || '',
|
||||
remarks: columns[14] || '',
|
||||
paidAmount: Number(columns[15]) || 0,
|
||||
};
|
||||
});
|
||||
|
||||
await onImport(entries);
|
||||
alert(`${entries.length} Einträge erfolgreich importiert!`);
|
||||
|
||||
// Input zurücksetzen
|
||||
if (fileInputRef.current) {
|
||||
fileInputRef.current.value = '';
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Import:', error);
|
||||
alert('Fehler beim Importieren der CSV-Datei. Bitte überprüfen Sie das Format.');
|
||||
} finally {
|
||||
setIsProcessing(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="csv-import-container">
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept=".csv"
|
||||
onChange={handleFileUpload}
|
||||
style={{ display: 'none' }}
|
||||
id="csv-file-input"
|
||||
/>
|
||||
<label htmlFor="csv-file-input" className="csv-import-button">
|
||||
{isProcessing ? '⏳ Importiere...' : '📁 CSV importieren'}
|
||||
</label>
|
||||
<small className="csv-hint">
|
||||
Format: Name,Vorname,Straße,PLZ/Ort,Telefon,Email,Anfrage,Termin,Zeit,Fahrtzeit,Strecke,Transport,Dauer,Durchgeführt,Bemerkungen,Bezahlt
|
||||
</small>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default CSVImport;
|
||||
Reference in New Issue
Block a user