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 } from 'lucide-react'; import { Button, Card, Avatar, Input, Select } from '../../../../components/ui'; import { Tabs, TabsList, TabsTrigger, TabsContent } from '../../../../components/ui/Tabs'; import { PageHeader } from '../../../../components/layout'; import { useToast } from '../../../../hooks/ui/useToast'; import { useAuthUser, useAuthStore, useAuthActions } from '../../../../stores/auth.store'; import { useAuthProfile, useUpdateProfile, useChangePassword } from '../../../../api/hooks/auth'; import { useCurrentTenant } from '../../../../stores'; import { subscriptionService } from '../../../../api'; // 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 { addToast } = useToast(); const user = useAuthUser(); const token = useAuthStore((state) => state.token); 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 [subscriptionStatus, setSubscriptionStatus] = useState(null); 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]); // Load subscription status React.useEffect(() => { const loadSubscriptionStatus = async () => { const tenantId = currentTenant?.id || user?.tenant_id; if (tenantId) { try { const status = await subscriptionService.getSubscriptionStatus(tenantId); setSubscriptionStatus(status); } catch (error) { console.error('Failed to load subscription status:', error); } } }; loadSubscriptionStatus(); }, [currentTenant, user]); 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); addToast(t('profile.save_changes'), { type: 'success' }); } catch (error) { addToast(t('common.error'), { type: '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: '' }); addToast(t('profile.password.change_success'), { type: 'success' }); } catch (error) { addToast(t('profile.password.change_error'), { type: '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 response = await fetch('/api/v1/users/me/export', { headers: { 'Authorization': `Bearer ${token}` } }); if (!response.ok) { throw new Error('Failed to export data'); } const blob = await response.blob(); 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); addToast(t('profile.privacy.export_success'), { type: 'success' }); } catch (err) { addToast(t('profile.privacy.export_error'), { type: 'error' }); } finally { setIsExporting(false); } }; const handleAccountDeletion = async () => { if (deleteConfirmEmail.toLowerCase() !== user?.email?.toLowerCase()) { addToast(t('common.error'), { type: 'error' }); return; } if (!deletePassword) { addToast(t('common.error'), { type: 'error' }); return; } setIsDeleting(true); try { const response = await fetch('/api/v1/users/me/delete/request', { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ confirm_email: deleteConfirmEmail, password: deletePassword, reason: deleteReason }) }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.detail || 'Failed to delete account'); } addToast(t('common.success'), { type: 'success' }); setTimeout(() => { logout(); navigate('/'); }, 2000); } catch (err: any) { addToast(err.message || t('common.error'), { type: '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 */}

{t('profile.personal_info')}

} /> } /> } /> } />
{!isEditing ? ( ) : ( <> )}
{/* Password Change Form */} {showPasswordForm && (

{t('profile.password.title')}

} /> } /> } />
)}
{/* Tab 2: Notifications */} {}} hasChanges={false} /> {/* Tab 3: Privacy & Data */}
{/* GDPR Rights Information */}

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

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

{/* Cookie Preferences */}

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

Gestiona tus preferencias de cookies

{/* Data Export */}

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

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

{/* Account Deletion */}

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

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

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

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

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

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

{subscriptionStatus && subscriptionStatus.status === 'active' && (

Suscripción Activa Detectada

Tienes una suscripción activa que se cancelará

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