diff --git a/frontend/src/pages/Employees/EmployeeManagement.tsx b/frontend/src/pages/Employees/EmployeeManagement.tsx index aade6e0..63a5bd8 100644 --- a/frontend/src/pages/Employees/EmployeeManagement.tsx +++ b/frontend/src/pages/Employees/EmployeeManagement.tsx @@ -1,4 +1,4 @@ -// frontend/src/pages/Employees/EmployeeManagement.tsx - VOLLSTÄNDIG +// frontend/src/pages/Employees/EmployeeManagement.tsx import React, { useState, useEffect } from 'react'; import { Employee } from '../../types/employee'; import { employeeService } from '../../services/employeeService'; @@ -15,8 +15,8 @@ const EmployeeManagement: React.FC = () => { const [loading, setLoading] = useState(true); const [viewMode, setViewMode] = useState('list'); const [selectedEmployee, setSelectedEmployee] = useState(null); - const { hasRole, user: currentUser } = useAuth(); - const { showNotification } = useNotification(); + const { hasRole } = useAuth(); + const { showNotification, confirmDialog } = useNotification(); useEffect(() => { loadEmployees(); @@ -25,10 +25,15 @@ const EmployeeManagement: React.FC = () => { const loadEmployees = async () => { try { setLoading(true); + console.log('🔄 Loading employees...'); + + // Add cache-busting parameter to prevent browser caching const data = await employeeService.getEmployees(); + console.log('✅ Employees loaded:', data); + setEmployees(data); } catch (err: any) { - console.error('Error loading employees:', err); + console.error('❌ Error loading employees:', err); showNotification({ type: 'error', title: 'Fehler', @@ -79,58 +84,73 @@ const EmployeeManagement: React.FC = () => { }); }; + // Verbesserte Lösch-Funktion mit BestĂ€tigungs-Dialog const handleDeleteEmployee = async (employee: Employee) => { - // Warnung basierend auf Rolle - let confirmMessage = `Möchten Sie den Mitarbeiter "${employee.name}" wirklich löschen?\n\nDiese Aktion kann nicht rĂŒckgĂ€ngig gemacht werden.`; - - if (employee.role === 'admin') { - const adminCount = employees.filter(emp => - emp.role === 'admin' && emp.isActive - ).length; - - if (adminCount <= 1) { - showNotification({ - type: 'error', - title: 'Aktion nicht möglich', - message: 'Es muss mindestens ein aktiver Administrator im System verbleiben.' - }); - return; - } - confirmMessage += '\n\n⚠ Achtung: Dieser Benutzer ist ein Administrator!'; - } - - if (!window.confirm(confirmMessage)) { - return; - } - try { + // BestĂ€tigungs-Dialog basierend auf Rolle + let confirmMessage = `Möchten Sie den Mitarbeiter "${employee.name}" wirklich PERMANENT LÖSCHEN?\n\nDie Daten des Mitarbeiters werden unwiderruflich gelöscht. Diese Aktion kann nicht rĂŒckgĂ€ngig gemacht werden.`; + let confirmTitle = 'Mitarbeiter löschen'; + + if (employee.role === 'admin') { + const adminCount = employees.filter(emp => + emp.role === 'admin' && emp.isActive + ).length; + + if (adminCount <= 1) { + showNotification({ + type: 'error', + title: 'Aktion nicht möglich', + message: 'Mindestens ein Administrator muss im System verbleiben' + }); + return; + } + + confirmTitle = 'Administrator löschen'; + confirmMessage = `Möchten Sie den Administrator "${employee.name}" wirklich PERMANENT LÖSCHEN?\n\nAchtung: Diese Aktion ist permanent und kann nicht rĂŒckgĂ€ngig gemacht werden.`; + } + + const confirmed = await confirmDialog({ + title: confirmTitle, + message: confirmMessage, + confirmText: 'Permanent löschen', + cancelText: 'Abbrechen', + type: 'warning' + }); + + if (!confirmed) return; + + console.log('Starting deletion process for employee:', employee.name); await employeeService.deleteEmployee(employee.id); - await loadEmployees(); + console.log('Employee deleted, reloading list'); + + // Force a fresh reload of employees + const updatedEmployees = await employeeService.getEmployees(); + setEmployees(updatedEmployees); + showNotification({ type: 'success', title: 'Erfolg', - message: `Mitarbeiter "${employee.name}" wurde gelöscht` + message: `Mitarbeiter "${employee.name}" wurde erfolgreich gelöscht` }); + } catch (err: any) { - console.error('Error deleting employee:', err); - showNotification({ - type: 'error', - title: 'Fehler', - message: 'Mitarbeiter konnte nicht gelöscht werden: ' + err.message - }); + if (err.message.includes('Mindestens ein Administrator')) { + showNotification({ + type: 'error', + title: 'Aktion nicht möglich', + message: err.message + }); + } else { + showNotification({ + type: 'error', + title: 'Fehler', + message: 'Mitarbeiter konnte nicht gelöscht werden' + }); + } } }; - const handleAvailabilitySaved = () => { - showNotification({ - type: 'success', - title: 'Erfolg', - message: 'VerfĂŒgbarkeiten wurden gespeichert' - }); - setViewMode('list'); - }; - - if (loading) { + if (loading && viewMode === 'list') { return (
⏳ Lade Mitarbeiter...
@@ -139,42 +159,69 @@ const EmployeeManagement: React.FC = () => { } return ( -
- {viewMode === 'list' && ( - <> -
-

đŸ‘„ Mitarbeiterverwaltung

- {hasRole(['admin', 'instandhalter']) && ( - - )} -
+
+
+
+

đŸ‘„ Mitarbeiter Verwaltung

+

+ {employees.length} Mitarbeiter gefunden +

+
- - + {viewMode === 'list' && hasRole(['admin']) && ( + + )} + + {viewMode !== 'list' && ( + + )} +
+ + {/* Inhalt basierend auf View Mode */} + {viewMode === 'list' && ( + )} {viewMode === 'create' && ( @@ -197,7 +244,7 @@ const EmployeeManagement: React.FC = () => { {viewMode === 'availability' && selectedEmployee && ( )}