// frontend/src/pages/Settings/Settings.tsx - UPDATED WITH NEW STYLES import React, { useState, useEffect, useRef } from 'react'; import { useAuth } from '../../contexts/AuthContext'; import { employeeService } from '../../services/employeeService'; import { useNotification } from '../../contexts/NotificationContext'; import AvailabilityManager from '../Employees/components/AvailabilityManager'; import { Employee } from '../../models/Employee'; import { styles } from './type/SettingsType'; const Settings: React.FC = () => { const { user: currentUser, updateUser } = useAuth(); const { showNotification } = useNotification(); const [activeTab, setActiveTab] = useState<'profile' | 'password' | 'availability'>('profile'); const [loading, setLoading] = useState(false); const [showAvailabilityManager, setShowAvailabilityManager] = useState(false); // Profile form state - updated for firstname/lastname const [profileForm, setProfileForm] = useState({ firstname: currentUser?.firstname || '', lastname: currentUser?.lastname || '' }); // Password form state const [passwordForm, setPasswordForm] = useState({ currentPassword: '', newPassword: '', confirmPassword: '' }); // Password visibility states const [showCurrentPassword, setShowCurrentPassword] = useState(false); const [showNewPassword, setShowNewPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); // Refs for timeout management const currentPasswordTimeoutRef = useRef(null); const newPasswordTimeoutRef = useRef(null); const confirmPasswordTimeoutRef = useRef(null); useEffect(() => { if (currentUser) { setProfileForm({ firstname: currentUser.firstname || '', lastname: currentUser.lastname || '' }); } }, [currentUser]); // Cleanup timeouts on unmount useEffect(() => { return () => { [currentPasswordTimeoutRef, newPasswordTimeoutRef, confirmPasswordTimeoutRef].forEach(ref => { if (ref.current) { clearTimeout(ref.current); } }); }; }, []); const handleProfileChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setProfileForm(prev => ({ ...prev, [name]: value })); }; const handlePasswordChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setPasswordForm(prev => ({ ...prev, [name]: value })); }; // Password visibility handlers for current password const handleCurrentPasswordMouseDown = () => { currentPasswordTimeoutRef.current = setTimeout(() => { setShowCurrentPassword(true); }, 300); }; const handleCurrentPasswordMouseUp = () => { if (currentPasswordTimeoutRef.current) { clearTimeout(currentPasswordTimeoutRef.current); currentPasswordTimeoutRef.current = null; } setShowCurrentPassword(false); }; // Password visibility handlers for new password const handleNewPasswordMouseDown = () => { newPasswordTimeoutRef.current = setTimeout(() => { setShowNewPassword(true); }, 300); }; const handleNewPasswordMouseUp = () => { if (newPasswordTimeoutRef.current) { clearTimeout(newPasswordTimeoutRef.current); newPasswordTimeoutRef.current = null; } setShowNewPassword(false); }; // Password visibility handlers for confirm password const handleConfirmPasswordMouseDown = () => { confirmPasswordTimeoutRef.current = setTimeout(() => { setShowConfirmPassword(true); }, 300); }; const handleConfirmPasswordMouseUp = () => { if (confirmPasswordTimeoutRef.current) { clearTimeout(confirmPasswordTimeoutRef.current); confirmPasswordTimeoutRef.current = null; } setShowConfirmPassword(false); }; // Touch event handlers const handleTouchStart = (setter: () => void) => (e: React.TouchEvent) => { e.preventDefault(); setter(); }; const handleTouchEnd = (cleanup: () => void) => (e: React.TouchEvent) => { e.preventDefault(); cleanup(); }; // Prevent context menu const handleContextMenu = (e: React.MouseEvent) => { e.preventDefault(); }; const handleProfileUpdate = async (e: React.FormEvent) => { e.preventDefault(); if (!currentUser) return; // Validation if (!profileForm.firstname.trim() || !profileForm.lastname.trim()) { showNotification({ type: 'error', title: 'Fehler', message: 'Vorname und Nachname sind erforderlich' }); return; } try { setLoading(true); await employeeService.updateEmployee(currentUser.id, { firstname: profileForm.firstname.trim(), lastname: profileForm.lastname.trim() }); // Update the auth context with new user data const updatedUser = await employeeService.getEmployee(currentUser.id); updateUser(updatedUser); showNotification({ type: 'success', title: 'Erfolg', message: 'Profil erfolgreich aktualisiert' }); } catch (error: any) { showNotification({ type: 'error', title: 'Fehler', message: error.message || 'Profil konnte nicht aktualisiert werden' }); } finally { setLoading(false); } }; const handlePasswordUpdate = async (e: React.FormEvent) => { e.preventDefault(); if (!currentUser) return; // Validation if (passwordForm.newPassword.length < 6) { showNotification({ type: 'error', title: 'Fehler', message: 'Das neue Passwort muss mindestens 6 Zeichen lang sein' }); return; } if (passwordForm.newPassword !== passwordForm.confirmPassword) { showNotification({ type: 'error', title: 'Fehler', message: 'Die Passwörter stimmen nicht überein' }); return; } try { setLoading(true); // Use the actual password change endpoint await employeeService.changePassword(currentUser.id, { currentPassword: passwordForm.currentPassword, newPassword: passwordForm.newPassword }); showNotification({ type: 'success', title: 'Erfolg', message: 'Passwort erfolgreich geändert' }); // Clear password form setPasswordForm({ currentPassword: '', newPassword: '', confirmPassword: '' }); } catch (error: any) { showNotification({ type: 'error', title: 'Fehler', message: error.message || 'Passwort konnte nicht geändert werden' }); } finally { setLoading(false); } }; const handleAvailabilitySave = () => { setShowAvailabilityManager(false); showNotification({ type: 'success', title: 'Erfolg', message: 'Verfügbarkeit erfolgreich gespeichert' }); }; const handleAvailabilityCancel = () => { setShowAvailabilityManager(false); }; if (!currentUser) { return
Nicht eingeloggt
; } if (showAvailabilityManager) { return ( ); } return (
{/* Left Sidebar with Tabs */}

Einstellungen

Verwalten Sie Ihre Kontoeinstellungen und Präferenzen
{/* Right Content Area */}
{/* Profile Tab */} {activeTab === 'profile' && ( <>

Profilinformationen

Verwalten Sie Ihre persönlichen Informationen und Kontaktdaten

{/* Read-only information */}

Systeminformationen

E-Mail wird automatisch aus Vor- und Nachname generiert

Persönliche Informationen

{/* Editable name fields */}
{ e.target.style.borderColor = '#1a1325'; e.target.style.boxShadow = '0 0 0 3px rgba(26, 19, 37, 0.1)'; }} onBlur={(e) => { e.target.style.borderColor = '#e8e8e8'; e.target.style.boxShadow = 'none'; }} />
{ e.target.style.borderColor = '#1a1325'; e.target.style.boxShadow = '0 0 0 3px rgba(26, 19, 37, 0.1)'; }} onBlur={(e) => { e.target.style.borderColor = '#e8e8e8'; e.target.style.boxShadow = 'none'; }} />
)} {/* Password Tab */} {activeTab === 'password' && ( <>

Passwort ändern

Aktualisieren Sie Ihr Passwort für erhöhte Sicherheit

{/* Current Password Field */}
{ e.target.style.borderColor = '#1a1325'; e.target.style.boxShadow = '0 0 0 3px rgba(26, 19, 37, 0.1)'; }} onBlur={(e) => { e.target.style.borderColor = '#e8e8e8'; e.target.style.boxShadow = 'none'; }} />
{/* New Password Field */}
{ e.target.style.borderColor = '#1a1325'; e.target.style.boxShadow = '0 0 0 3px rgba(26, 19, 37, 0.1)'; }} onBlur={(e) => { e.target.style.borderColor = '#e8e8e8'; e.target.style.boxShadow = 'none'; }} />
Das Passwort muss mindestens 6 Zeichen lang sein.
{/* Confirm Password Field */}
{ e.target.style.borderColor = '#1a1325'; e.target.style.boxShadow = '0 0 0 3px rgba(26, 19, 37, 0.1)'; }} onBlur={(e) => { e.target.style.borderColor = '#e8e8e8'; e.target.style.boxShadow = 'none'; }} />
)} {/* Availability Tab */} {activeTab === 'availability' && ( <>

Meine Verfügbarkeit

Legen Sie Ihre persönliche Verfügbarkeit für Schichtpläne fest

📅

Verfügbarkeit verwalten

Hier können Sie Ihre persönliche Verfügbarkeit für Schichtpläne festlegen. Legen Sie für jeden Tag und jede Schicht fest, ob Sie bevorzugt, möglicherweise oder nicht verfügbar sind.

)}
); }; export default Settings;