bugsquasher-pro / components /settings-manager.js
Zeddfly's picture
Can you please create a link when a user signs up that allows them to download the app to either an android or apple phone. It should get emailed through to them with their user details.
2fb7010 verified
Raw History Blame Contribute Delete
17.3 kB
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 = `
<h2 class="text-xl font-semibold mb-6">Profile Settings</h2>
<form id="profileForm" class="space-y-6">
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<label for="firstName" class="block text-sm font-medium text-gray-700">First Name</label>
<input type="text" id="firstName" name="firstName" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500" value="${data.firstName}">
</div>
<div>
<label for="lastName" class="block text-sm font-medium text-gray-700">Last Name</label>
<input type="text" id="lastName" name="lastName" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500" value="${data.lastName}">
</div>
</div>
<div>
<label for="email" class="block text-sm font-medium text-gray-700">Email</label>
<input type="email" id="email" name="email" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500" value="${data.email}">
</div>
<div>
<label for="timezone" class="block text-sm font-medium text-gray-700">Timezone</label>
<select id="timezone" name="timezone" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
<option value="UTC-12:00" ${data.timezone === 'UTC-12:00' ? 'selected' : ''}>UTC-12:00</option>
<option value="UTC-08:00" ${data.timezone === 'UTC-08:00' ? 'selected' : ''}>UTC-08:00 (Pacific Time)</option>
<option value="UTC-05:00" ${data.timezone === 'UTC-05:00' ? 'selected' : ''}>UTC-05:00 (Eastern Time)</option>
<option value="UTC+00:00" ${data.timezone === 'UTC+00:00' ? 'selected' : ''}>UTC+00:00 (GMT)</option>
<option value="UTC+01:00" ${data.timezone === 'UTC+01:00' ? 'selected' : ''}>UTC+01:00 (CET)</option>
</select>
</div>
<div class="flex justify-end">
<button type="button" class="bg-gray-200 text-gray-800 px-4 py-2 rounded-md mr-3 hover:bg-gray-300">
Cancel
</button>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600">
Save Changes
</button>
</div>
</form>
`;
break;
case 'notifications':
content = `
<h2 class="text-xl font-semibold mb-6">Notification Settings</h2>
<form id="notificationsForm" class="space-y-6">
<div class="space-y-4">
<div class="flex items-center">
<input type="checkbox" id="emailNotifications" name="email" class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded" ${data.email ? 'checked' : ''}>
<label for="emailNotifications" class="ml-2 block text-sm text-gray-700">Email Notifications</label>
</div>
<div class="flex items-center">
<input type="checkbox" id="pushNotifications" name="push" class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded" ${data.push ? 'checked' : ''}>
<label for="pushNotifications" class="ml-2 block text-sm text-gray-700">Push Notifications</label>
</div>
<div class="flex items-center">
<input type="checkbox" id="soundNotifications" name="sound" class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded" ${data.sound ? 'checked' : ''}>
<label for="soundNotifications" class="ml-2 block text-sm text-gray-700">Sound Alerts</label>
</div>
</div>
<div>
<label for="notificationFrequency" class="block text-sm font-medium text-gray-700">Notification Frequency</label>
<select id="notificationFrequency" name="frequency" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
<option value="immediate" ${data.frequency === 'immediate' ? 'selected' : ''}>Immediately</option>
<option value="hourly" ${data.frequency === 'hourly' ? 'selected' : ''}>Hourly Digest</option>
<option value="daily" ${data.frequency === 'daily' ? 'selected' : ''}>Daily Digest</option>
</select>
</div>
<div class="flex justify-end">
<button type="button" class="bg-gray-200 text-gray-800 px-4 py-2 rounded-md mr-3 hover:bg-gray-300">
Cancel
</button>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600">
Save Changes
</button>
</div>
</form>
`;
break;
case 'security':
content = `
<h2 class="text-xl font-semibold mb-6">Security Settings</h2>
<form id="securityForm" class="space-y-6">
<div class="flex items-center">
<input type="checkbox" id="twoFactor" name="twoFactor" class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded" ${data.twoFactor ? 'checked' : ''}>
<label for="twoFactor" class="ml-2 block text-sm text-gray-700">Enable Two-Factor Authentication</label>
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Last Password Change</label>
<p class="mt-1 text-sm text-gray-500">${data.passwordChanged}</p>
</div>
<div>
<label class="block text-sm font-medium text-gray-700">Active Sessions</label>
<p class="mt-1 text-sm text-gray-500">${data.activeSessions} active sessions</p>
<button type="button" class="mt-2 text-sm text-blue-500 hover:text-blue-700">View all sessions</button>
</div>
<div class="pt-4 border-t">
<h3 class="text-lg font-medium text-gray-900 mb-4">Change Password</h3>
<div class="space-y-4">
<div>
<label for="currentPassword" class="block text-sm font-medium text-gray-700">Current Password</label>
<input type="password" id="currentPassword" name="currentPassword" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
</div>
<div>
<label for="newPassword" class="block text-sm font-medium text-gray-700">New Password</label>
<input type="password" id="newPassword" name="newPassword" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
</div>
<div>
<label for="confirmNewPassword" class="block text-sm font-medium text-gray-700">Confirm New Password</label>
<input type="password" id="confirmNewPassword" name="confirmNewPassword" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
</div>
</div>
</div>
<div class="flex justify-end">
<button type="button" class="bg-gray-200 text-gray-800 px-4 py-2 rounded-md mr-3 hover:bg-gray-300">
Cancel
</button>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600">
Save Changes
</button>
</div>
</form>
`;
break;
case 'preferences':
content = `
<h2 class="text-xl font-semibold mb-6">User Preferences</h2>
<form id="preferencesForm" class="space-y-6">
<div>
<label for="theme" class="block text-sm font-medium text-gray-700">Theme</label>
<select id="theme" name="theme" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
<option value="light" ${data.theme === 'light' ? 'selected' : ''}>Light</option>
<option value="dark" ${data.theme === 'dark' ? 'selected' : ''}>Dark</option>
<option value="system" ${data.theme === 'system' ? 'selected' : ''}>System Default</option>
</select>
</div>
<div>
<label for="language" class="block text-sm font-medium text-gray-700">Language</label>
<select id="language" name="language" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
<option value="en" ${data.language === 'en' ? 'selected' : ''}>English</option>
<option value="es" ${data.language === 'es' ? 'selected' : ''}>Spanish</option>
<option value="fr" ${data.language === 'fr' ? 'selected' : ''}>French</option>
<option value="de" ${data.language === 'de' ? 'selected' : ''}>German</option>
</select>
</div>
<div>
<label for="dashboardView" class="block text-sm font-medium text-gray-700">Dashboard View</label>
<select id="dashboardView" name="dashboardView" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
<option value="cards" ${data.dashboardView === 'cards' ? 'selected' : ''}>Cards</option>
<option value="table" ${data.dashboardView === 'table' ? 'selected' : ''}>Table</option>
<option value="list" ${data.dashboardView === 'list' ? 'selected' : ''}>List</option>
</select>
</div>
<div class="flex justify-end">
<button type="button" class="bg-gray-200 text-gray-800 px-4 py-2 rounded-md mr-3 hover:bg-gray-300">
Cancel
</button>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600">
Save Changes
</button>
</div>
</form>
`;
break;
case 'account':
content = `
<h2 class="text-xl font-semibold mb-6">Create New Account</h2>
<form id="accountForm" class="space-y-6">
<div>
<label for="accountUsername" class="block text-sm font-medium text-gray-700">Username</label>
<input type="text" id="accountUsername" name="username" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500" required>
</div>
<div>
<label for="accountEmail" class="block text-sm font-medium text-gray-700">Email</label>
<input type="email" id="accountEmail" name="email" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500" required>
</div>
<div>
<label for="accountPassword" class="block text-sm font-medium text-gray-700">Password</label>
<input type="password" id="accountPassword" name="password" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500" required>
</div>
<div>
<label for="accountConfirmPassword" class="block text-sm font-medium text-gray-700">Confirm Password</label>
<input type="password" id="accountConfirmPassword" name="confirmPassword" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500" required>
</div>
<div>
<label for="accountRole" class="block text-sm font-medium text-gray-700">Role</label>
<select id="accountRole" name="role" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
<option value="user">User</option>
<option value="developer">Developer</option>
<option value="admin">Administrator</option>
</select>
</div>
<div class="flex justify-end">
<button type="button" class="bg-gray-200 text-gray-800 px-4 py-2 rounded-md mr-3 hover:bg-gray-300">
Cancel
</button>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600">
Create Account
</button>
</div>
</form>
<app-download id="appDownload"></app-download>
`;
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!');
});
});