-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.js
More file actions
204 lines (166 loc) · 6.84 KB
/
settings.js
File metadata and controls
204 lines (166 loc) · 6.84 KB
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
document.addEventListener('DOMContentLoaded', function() {
// Check if user is logged in
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (!currentUser) {
window.location.href = 'signin.html';
return;
}
// Populate interface language dropdown
populateInterfaceLanguages();
// Load user settings
loadUserSettings();
// Event listeners
$('#saveSettingsBtn').on('click', saveSettings);
$('#changeUsernameBtn').on('click', showChangeUsernameModal);
$('#changeEmailBtn').on('click', showChangeEmailModal);
$('#changePasswordBtn').on('click', showChangePasswordModal);
$('#interfaceLanguage').on('change', updateInterfacePreview);
});
/**
* Populate interface language options
*/
function populateInterfaceLanguages() {
const interfaceLanguageSelect = document.getElementById('interfaceLanguage');
const currentLanguage = localStorage.getItem('interfaceLanguage') || 'English';
// Clear existing options
interfaceLanguageSelect.innerHTML = '';
// Add available interface languages
const interfaceLanguages = ['English', 'Spanish', 'French', 'German', 'Chinese', 'Japanese', 'Russian'];
interfaceLanguages.forEach(language => {
const option = document.createElement('option');
option.value = language;
option.textContent = language;
if (language === currentLanguage) {
option.selected = true;
}
interfaceLanguageSelect.appendChild(option);
});
}
/**
* Load user settings from localStorage
*/
function loadUserSettings() {
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (!currentUser) return;
// Set form values
$('#username').val(currentUser.username);
$('#email').val(currentUser.email);
// Set notification preferences (if they exist)
$('#emailNotifications').prop('checked', currentUser.emailNotifications || false);
$('#forumNotifications').prop('checked', currentUser.forumNotifications || false);
// Set interface language
const interfaceLanguage = localStorage.getItem('interfaceLanguage') || 'English';
$('#interfaceLanguage').val(interfaceLanguage);
}
/**
* Save user settings
*/
function saveSettings() {
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (!currentUser) return;
// Update notification settings
currentUser.emailNotifications = $('#emailNotifications').is(':checked');
currentUser.forumNotifications = $('#forumNotifications').is(':checked');
// Save interface language
const interfaceLanguage = $('#interfaceLanguage').val();
localStorage.setItem('interfaceLanguage', interfaceLanguage);
// Update user in localStorage
localStorage.setItem('currentUser', JSON.stringify(currentUser));
// Update users array in localStorage
const users = JSON.parse(localStorage.getItem('users')) || [];
const userIndex = users.findIndex(user => user.username === currentUser.username);
if (userIndex !== -1) {
users[userIndex] = currentUser;
localStorage.setItem('users', JSON.stringify(users));
}
alert('Settings saved successfully!');
}
/**
* Update interface preview when language changes
*/
function updateInterfacePreview() {
// This would show a preview of the interface in the selected language
// For now, we'll just log it
console.log('Interface language changed to: ' + $('#interfaceLanguage').val());
}
// Modal functions for changing username, email, and password
function showChangeUsernameModal() {
// Implementation for username change modal
const newUsername = prompt('Enter new username:');
if (newUsername && newUsername.trim()) {
updateUsername(newUsername.trim());
}
}
function showChangeEmailModal() {
// Implementation for email change modal
const newEmail = prompt('Enter new email:');
if (newEmail && newEmail.trim()) {
updateEmail(newEmail.trim());
}
}
function showChangePasswordModal() {
// Implementation for password change modal
const currentPassword = prompt('Enter current password:');
if (!currentPassword) return;
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (currentPassword !== currentUser.password) {
alert('Current password is incorrect');
return;
}
const newPassword = prompt('Enter new password:');
if (newPassword && newPassword.trim()) {
updatePassword(newPassword.trim());
}
}
// Functions to update user data
function updateUsername(newUsername) {
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
const users = JSON.parse(localStorage.getItem('users')) || [];
// Check if username already exists
if (users.some(user => user.username === newUsername && user.username !== currentUser.username)) {
alert('Username already exists');
return;
}
// Update username
const userIndex = users.findIndex(user => user.username === currentUser.username);
if (userIndex !== -1) {
users[userIndex].username = newUsername;
currentUser.username = newUsername;
localStorage.setItem('users', JSON.stringify(users));
localStorage.setItem('currentUser', JSON.stringify(currentUser));
$('#username').val(newUsername);
alert('Username updated successfully');
}
}
function updateEmail(newEmail) {
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
const users = JSON.parse(localStorage.getItem('users')) || [];
// Check if email already exists
if (users.some(user => user.email === newEmail && user.username !== currentUser.username)) {
alert('Email already exists');
return;
}
// Update email
const userIndex = users.findIndex(user => user.username === currentUser.username);
if (userIndex !== -1) {
users[userIndex].email = newEmail;
currentUser.email = newEmail;
localStorage.setItem('users', JSON.stringify(users));
localStorage.setItem('currentUser', JSON.stringify(currentUser));
$('#email').val(newEmail);
alert('Email updated successfully');
}
}
function updatePassword(newPassword) {
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
const users = JSON.parse(localStorage.getItem('users')) || [];
// Update password
const userIndex = users.findIndex(user => user.username === currentUser.username);
if (userIndex !== -1) {
users[userIndex].password = newPassword;
currentUser.password = newPassword;
localStorage.setItem('users', JSON.stringify(users));
localStorage.setItem('currentUser', JSON.stringify(currentUser));
alert('Password updated successfully');
}
}