import { useState, useEffect } from 'react'; import { useRouter } from 'next/router'; import Layout from '../components/Layout'; export default function Settings() { const router = useRouter(); // User state const [user, setUser] = useState(null); // Settings state const [settings, setSettings] = useState({ // Account Settings notifications_email: true, notifications_marketing: false, collection_visibility: 'private', preferred_currency: 'USD', cards_per_page: 50, default_view: 'grid', // Security Settings two_factor_enabled: false, // Display Settings theme: 'system', // light, dark, system language: 'en' }); // Password change state const [passwordForm, setPasswordForm] = useState({ current_password: '', new_password: '', confirm_password: '' }); // UI state const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [changingPassword, setChangingPassword] = useState(false); const [message, setMessage] = useState({ type: '', text: '' }); const [activeSection, setActiveSection] = useState('account'); const loadSettings = async () => { try { const token = localStorage.getItem('auth_token'); if (!token) { router.push('/login'); return; } const response = await fetch('/api/user/settings', { headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { const data = await response.json(); setUser(data.user); setSettings(data.settings); } else if (response.status === 401) { router.push('/login'); } } catch (error) { console.error('Error loading settings:', error); setMessage({ type: 'error', text: 'Failed to load settings' }); } finally { setLoading(false); } } useEffect(() => { // eslint-disable-next-line react-hooks/set-state-in-effect -- load settings on mount loadSettings(); // eslint-disable-next-line react-hooks/exhaustive-deps -- mount-only settings load }, []); ; const handleSettingChange = (key, value) => { setSettings(prev => ({ ...prev, [key]: value })); }; const saveSettings = async () => { setSaving(true); setMessage({ type: '', text: '' }); try { const token = localStorage.getItem('auth_token'); const response = await fetch('/api/user/settings', { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify(settings) }); if (response.ok) { setMessage({ type: 'success', text: 'Settings saved successfully!' }); } else { const error = await response.json(); setMessage({ type: 'error', text: error.message || 'Failed to save settings' }); } } catch (error) { console.error('Error saving settings:', error); setMessage({ type: 'error', text: 'Failed to save settings' }); } finally { setSaving(false); } }; const handlePasswordChange = async (e) => { e.preventDefault(); if (passwordForm.new_password !== passwordForm.confirm_password) { setMessage({ type: 'error', text: 'New passwords do not match' }); return; } if (passwordForm.new_password.length < 8) { setMessage({ type: 'error', text: 'Password must be at least 8 characters long' }); return; } setChangingPassword(true); setMessage({ type: '', text: '' }); try { const token = localStorage.getItem('auth_token'); const response = await fetch('/api/user/password', { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify({ current_password: passwordForm.current_password, new_password: passwordForm.new_password }) }); if (response.ok) { setPasswordForm({ current_password: '', new_password: '', confirm_password: '' }); setMessage({ type: 'success', text: 'Password changed successfully!' }); } else { const error = await response.json(); setMessage({ type: 'error', text: error.message || 'Failed to change password' }); } } catch (error) { console.error('Error changing password:', error); setMessage({ type: 'error', text: 'Failed to change password' }); } finally { setChangingPassword(false); } }; const handleDeleteAccount = async () => { const confirmed = window.confirm( 'Are you sure you want to delete your account? This action cannot be undone and will permanently delete all your cards, lists, and decks.' ); if (!confirmed) return; const doubleConfirmed = window.confirm( 'This is your final warning. Deleting your account will permanently remove all your data. Type "DELETE" to confirm.' ); if (!doubleConfirmed) return; try { const token = localStorage.getItem('auth_token'); const response = await fetch('/api/user/delete', { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }); if (response.ok) { localStorage.removeItem('auth_token'); router.push('/login?message=Account deleted successfully'); } else { const error = await response.json(); setMessage({ type: 'error', text: error.message || 'Failed to delete account' }); } } catch (error) { console.error('Error deleting account:', error); setMessage({ type: 'error', text: 'Failed to delete account' }); } }; const sections = [ { id: 'account', name: 'Account', icon: '👤' }, { id: 'security', name: 'Security', icon: '🔒' }, { id: 'preferences', name: 'Preferences', icon: '⚙️' }, { id: 'notifications', name: 'Notifications', icon: '🔔' }, { id: 'display', name: 'Display', icon: '🎨' } ]; const currencyOptions = [ { value: 'USD', label: 'US Dollar ($)' }, { value: 'EUR', label: 'Euro (€)' }, { value: 'GBP', label: 'British Pound (£)' }, { value: 'CAD', label: 'Canadian Dollar (C$)' }, { value: 'JPY', label: 'Japanese Yen (¥)' } ]; const cardsPerPageOptions = [ { value: 25, label: '25 cards' }, { value: 50, label: '50 cards' }, { value: 100, label: '100 cards' } ]; const viewOptions = [ { value: 'grid', label: 'Grid View' }, { value: 'list', label: 'List View' } ]; const themeOptions = [ { value: 'light', label: 'Light' }, { value: 'dark', label: 'Dark' }, { value: 'system', label: 'System' } ]; if (loading) { return (
); } return (
{/* Header */}

Settings

Manage your account preferences and security settings

{/* Message */} {message.text && (
{message.text}
)}
{/* Sidebar Navigation */}
{/* Settings Content */}
{/* Account Settings */} {activeSection === 'account' && (

Account Settings

{/* Email */}
Verified

Contact support to change your email address

{/* Collection Visibility */}

Controls who can see your new lists by default

{/* Preferred Currency */}

Currency used for displaying card values

)} {/* Security Settings */} {activeSection === 'security' && (

Security Settings

{/* Change Password */}

Change Password

setPasswordForm(prev => ({ ...prev, current_password: e.target.value }))} className="input-field w-full" required />
setPasswordForm(prev => ({ ...prev, new_password: e.target.value }))} className="input-field w-full" minLength={8} required />
setPasswordForm(prev => ({ ...prev, confirm_password: e.target.value }))} className="input-field w-full" minLength={8} required />
{/* Two-Factor Authentication */}

Two-Factor Authentication

Enable 2FA

Add an extra layer of security to your account

{settings.two_factor_enabled && (

Two-factor authentication is enabled. Use your authenticator app to log in.

)}
{/* Danger Zone */}

Danger Zone

Delete Account

Permanently delete your account and all associated data

)} {/* Preferences */} {activeSection === 'preferences' && (

Preferences

{/* Cards Per Page */}
{/* Default View */}
)} {/* Notifications */} {activeSection === 'notifications' && (

Notification Settings

{/* Email Notifications */}

Email Notifications

Receive notifications about your lists and cards

{/* Marketing Emails */}

Marketing Emails

Receive updates about new features and promotions

)} {/* Display Settings */} {activeSection === 'display' && (

Display Settings

{/* Theme */}

System will follow your device's theme preference

{/* Language */}

Interface language (coming soon)

)} {/* Save Button */}
); }