class SettingsManager {
constructor() {
this.settingsData = {
profile: {
firstName: 'John',
lastName: 'Doe',
email: 'john.doe@example.com',
timezone: 'UTC-05:00'
},
notifications: {
email: true,
push: true,
sound: false,
frequency: 'immediate'
},
security: {
twoFactor: false,
passwordChanged: '2023-11-01',
activeSessions: 2
},
preferences: {
theme: 'light',
language: 'en',
dashboardView: 'cards'
},
account: {
username: '',
email: '',
password: '',
confirmPassword: '',
role: 'user',
hasApp: false
}
};
}
loadSection(section) {
let content = '';
const data = this.settingsData[section];
switch(section) {
case 'profile':
content = `
Profile Settings
`;
break;
case 'notifications':
content = `
Notification Settings
`;
break;
case 'security':
content = `
Security Settings
`;
break;
case 'preferences':
content = `
User Preferences
`;
break;
case 'account':
content = `
Create New Account
`;
break;
}
document.getElementById('settingsContent').innerHTML = content;
if (section === 'account') {
const appDownload = document.getElementById('appDownload');
appDownload.addEventListener('send-download-link', (e) => {
const { email } = this.settingsData.account;
this.sendDownloadEmail(email, e.detail.iosUrl, e.detail.androidUrl);
});
}
feather.replace();
}
saveSection(section, formData) {
this.settingsData[section] = { ...this.settingsData[section], ...formData };
console.log(`Saved ${section} settings:`, this.settingsData[section]);
if (section === 'account') {
this.settingsData.account.hasApp = false;
// In a real app, you would send this to your backend API
// and trigger the welcome email with download links
}
}
async sendDownloadEmail(email, iosUrl, androidUrl) {
// In a real app, this would call your backend API to send the email
console.log(`Sending download links to ${email}`);
console.log(`iOS: ${iosUrl}`);
console.log(`Android: ${androidUrl}`);
// Simulate API call
try {
// This is where you would make the actual API call
// await fetch('/api/send-download-email', {
// method: 'POST',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify({ email, iosUrl, androidUrl })
// });
alert(`Download links have been sent to ${email}`);
this.settingsData.account.hasApp = true;
} catch (error) {
console.error('Failed to send email:', error);
alert('Failed to send download links. Please try again later.');
}
}
}
// Initialize settings manager and set up event listeners
document.addEventListener('DOMContentLoaded', () => {
const settingsManager = new SettingsManager();
// Load default section
settingsManager.loadSection('profile');
// Set active button
document.querySelector('[data-section="profile"]').classList.add('bg-gray-200', 'text-gray-900');
// Handle section button clicks
document.querySelectorAll('[data-section]').forEach(button => {
button.addEventListener('click', (e) => {
const section = e.currentTarget.dataset.section;
// Update active button
document.querySelectorAll('[data-section]').forEach(btn => {
btn.classList.remove('bg-gray-200', 'text-gray-900');
btn.classList.add('text-gray-600', 'hover:bg-gray-100');
});
e.currentTarget.classList.add('bg-gray-200', 'text-gray-900');
e.currentTarget.classList.remove('text-gray-600', 'hover:bg-gray-100');
// Load section
settingsManager.loadSection(section);
});
});
// Handle form submissions
document.addEventListener('submit', (e) => {
e.preventDefault();
const formId = e.target.id;
const formData = Object.fromEntries(new FormData(e.target).entries());
if (formId === 'profileForm') {
settingsManager.saveSection('profile', formData);
} else if (formId === 'notificationsForm') {
settingsManager.saveSection('notifications', formData);
} else if (formId === 'securityForm') {
settingsManager.saveSection('security', formData);
} else if (formId === 'preferencesForm') {
settingsManager.saveSection('preferences', formData);
} else if (formId === 'accountForm') {
settingsManager.saveSection('account', formData);
// Reset form after creation
e.target.reset();
}
// Show success message
alert('Settings saved successfully!');
});
});