Spaces:
Running
Running
File size: 17,324 Bytes
60e97a6 2fb7010 60e97a6 2fb7010 60e97a6 2fb7010 60e97a6 2fb7010 60e97a6 2fb7010 60e97a6 2fb7010 60e97a6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | 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!');
});
}); |