import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useNavigate } from 'react-router-dom'; import { User, Mail, Phone, Lock, Globe, Clock, Camera, Save, X, Bell, Shield, Download, Trash2, AlertCircle, Cookie, ExternalLink, Check } from 'lucide-react'; import { Button, Card, Avatar, Input, Select, SettingSection, SettingRow } from '../../../../components/ui'; import { Tabs, TabsList, TabsTrigger, TabsContent } from '../../../../components/ui/Tabs'; import { PageHeader } from '../../../../components/layout'; import { showToast } from '../../../../utils/toast'; import { useAuthUser, useAuthActions } from '../../../../stores/auth.store'; import { useAuthProfile, useUpdateProfile, useChangePassword } from '../../../../api/hooks/auth'; import { useCurrentTenant } from '../../../../stores'; // Import the communication preferences component import CommunicationPreferences, { type NotificationPreferences } from './CommunicationPreferences'; interface ProfileFormData { first_name: string; last_name: string; email: string; phone: string; language: string; timezone: string; avatar?: string; } interface PasswordData { currentPassword: string; newPassword: string; confirmPassword: string; } const NewProfileSettingsPage: React.FC = () => { const { t } = useTranslation('settings'); const navigate = useNavigate(); const user = useAuthUser(); const { logout } = useAuthActions(); const currentTenant = useCurrentTenant(); const { data: profile, isLoading: profileLoading } = useAuthProfile(); const updateProfileMutation = useUpdateProfile(); const changePasswordMutation = useChangePassword(); const [activeTab, setActiveTab] = useState('personal'); const [isEditing, setIsEditing] = useState(false); const [isLoading, setIsLoading] = useState(false); const [showPasswordForm, setShowPasswordForm] = useState(false); // Export & Delete states const [isExporting, setIsExporting] = useState(false); const [showDeleteModal, setShowDeleteModal] = useState(false); const [deleteConfirmEmail, setDeleteConfirmEmail] = useState(''); const [deletePassword, setDeletePassword] = useState(''); const [deleteReason, setDeleteReason] = useState(''); const [isDeleting, setIsDeleting] = useState(false); const [profileData, setProfileData] = useState({ first_name: '', last_name: '', email: '', phone: '', language: 'es', timezone: 'Europe/Madrid' }); const [passwordData, setPasswordData] = useState({ currentPassword: '', newPassword: '', confirmPassword: '' }); const [errors, setErrors] = useState>({}); // Update profile data when profile is loaded React.useEffect(() => { if (profile) { setProfileData({ first_name: profile.first_name || '', last_name: profile.last_name || '', email: profile.email || '', phone: profile.phone || '', language: profile.language || 'es', timezone: profile.timezone || 'Europe/Madrid', avatar: profile.avatar || '' }); } }, [profile]); const languageOptions = [ { value: 'es', label: 'Español' }, { value: 'eu', label: 'Euskara' }, { value: 'en', label: 'English' } ]; const timezoneOptions = [ { value: 'Europe/Madrid', label: 'Madrid (CET/CEST)' }, { value: 'Atlantic/Canary', label: 'Canarias (WET/WEST)' }, { value: 'Europe/London', label: 'Londres (GMT/BST)' } ]; const validateProfile = (): boolean => { const newErrors: Record = {}; if (!profileData.first_name.trim()) { newErrors.first_name = t('profile.fields.first_name') + ' ' + t('common.required'); } if (!profileData.last_name.trim()) { newErrors.last_name = t('profile.fields.last_name') + ' ' + t('common.required'); } if (!profileData.email.trim()) { newErrors.email = t('profile.fields.email') + ' ' + t('common.required'); } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(profileData.email)) { newErrors.email = t('common.error'); } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const validatePassword = (): boolean => { const newErrors: Record = {}; if (!passwordData.currentPassword) { newErrors.currentPassword = t('profile.password.current_password') + ' ' + t('common.required'); } if (!passwordData.newPassword) { newErrors.newPassword = t('profile.password.new_password') + ' ' + t('common.required'); } else if (passwordData.newPassword.length < 8) { newErrors.newPassword = t('profile.password.password_requirements'); } if (passwordData.newPassword !== passwordData.confirmPassword) { newErrors.confirmPassword = t('common.error'); } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSaveProfile = async () => { if (!validateProfile()) return; setIsLoading(true); try { await updateProfileMutation.mutateAsync(profileData); setIsEditing(false); showToast.success(t('profile.save_changes')); } catch (error) { showToast.error(t('common.error')); } finally { setIsLoading(false); } }; const handleChangePasswordSubmit = async () => { if (!validatePassword()) return; setIsLoading(true); try { await changePasswordMutation.mutateAsync({ current_password: passwordData.currentPassword, new_password: passwordData.newPassword, confirm_password: passwordData.confirmPassword }); setShowPasswordForm(false); setPasswordData({ currentPassword: '', newPassword: '', confirmPassword: '' }); showToast.success(t('profile.password.change_success')); } catch (error) { showToast.error(t('profile.password.change_error')); } finally { setIsLoading(false); } }; const handleInputChange = (field: keyof ProfileFormData) => (e: React.ChangeEvent) => { setProfileData(prev => ({ ...prev, [field]: e.target.value })); if (errors[field]) { setErrors(prev => ({ ...prev, [field]: '' })); } }; const handleSelectChange = (field: keyof ProfileFormData) => (value: string) => { setProfileData(prev => ({ ...prev, [field]: value })); }; const handlePasswordChange = (field: keyof PasswordData) => (e: React.ChangeEvent) => { setPasswordData(prev => ({ ...prev, [field]: e.target.value })); if (errors[field]) { setErrors(prev => ({ ...prev, [field]: '' })); } }; const handleSaveNotificationPreferences = async (preferences: NotificationPreferences) => { try { await updateProfileMutation.mutateAsync({ language: preferences.language, timezone: preferences.timezone, notification_preferences: preferences }); } catch (error) { throw error; } }; const handleDataExport = async () => { setIsExporting(true); try { const { authService } = await import('../../../../api'); const exportData = await authService.exportMyData(); // Convert to blob and download const blob = new Blob([JSON.stringify(exportData, null, 2)], { type: 'application/json' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `my_data_export_${new Date().toISOString().split('T')[0]}.json`; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); showToast.success(t('profile.privacy.export_success')); } catch (err) { showToast.error(t('profile.privacy.export_error')); } finally { setIsExporting(false); } }; const handleAccountDeletion = async () => { if (deleteConfirmEmail.toLowerCase() !== user?.email?.toLowerCase()) { showToast.error(t('common.error')); return; } if (!deletePassword) { showToast.error(t('common.error')); return; } setIsDeleting(true); try { const { authService } = await import('../../../../api'); await authService.deleteAccount(deleteConfirmEmail, deletePassword, deleteReason); showToast.success(t('common.success')); setTimeout(() => { logout(); navigate('/'); }, 2000); } catch (err: any) { showToast.error(err.message || t('common.error')); } finally { setIsDeleting(false); } }; if (profileLoading || !profile) { return (
{t('common.loading')}
); } return (
{/* Profile Header */}

{profileData.first_name} {profileData.last_name}

{profileData.email}

{user?.role && (

{user.role}

)}
{t('profile.online')}
{/* Tabs Navigation */} {t('profile.tabs.personal')} {t('profile.tabs.notifications')} {t('profile.tabs.privacy')} {/* Tab 1: Personal Information */}
{/* Profile Form */} } headerAction={ !isEditing ? ( ) : (
) } >
} required /> } required /> } /> } />
{/* Security Section */} } headerAction={ } > {showPasswordForm && (
} required /> } required /> } required />
)}
{/* Tab 2: Notifications */} {}} hasChanges={false} /> {/* Tab 3: Privacy & Data */}
{/* GDPR Rights Information */}

{t('profile.privacy.gdpr_rights')}

{t('profile.privacy.gdpr_description')}

{/* Cookie Preferences */} } headerAction={ } >
{/* Data Export */} } headerAction={ } >
{/* Account Deletion */} } className="border-red-200 dark:border-red-800" >

{t('profile.privacy.delete_description')}

{t('profile.privacy.delete_warning')}

{/* Delete Account Modal */} {showDeleteModal && (

{t('profile.privacy.delete_account')}?

{t('profile.privacy.delete_warning')}

setDeleteConfirmEmail(e.target.value)} required /> setDeletePassword(e.target.value)} required leftIcon={} />