Spaces:
Running
Running
debug
Browse files- static/js/script.js +43 -38
- templates/index.html +2 -1
static/js/script.js
CHANGED
@@ -1,17 +1,18 @@
|
|
1 |
-
// --- Existing Constants (
|
2 |
const registerSection = document.getElementById('register-section');
|
3 |
const loginSection = document.getElementById('login-section');
|
4 |
const welcomeSection = document.getElementById('welcome-section');
|
5 |
// ... other element constants ...
|
6 |
const notificationsDiv = document.getElementById('notifications');
|
7 |
-
const API_URL = '/api';
|
8 |
-
const themeToggleButton = document.getElementById('theme-toggle'); //
|
9 |
|
10 |
-
// --- Existing State Variables
|
11 |
let webSocket = null;
|
12 |
-
let authToken = localStorage.getItem('authToken');
|
13 |
|
14 |
// --- UI Control (showSection, setStatus - keep) ---
|
|
|
15 |
function showSection(sectionId) {
|
16 |
registerSection.style.display = 'none';
|
17 |
loginSection.style.display = 'none';
|
@@ -26,7 +27,9 @@ function setStatus(element, message, isSuccess = false) {
|
|
26 |
element.style.display = message ? 'block' : 'none';
|
27 |
}
|
28 |
|
|
|
29 |
// --- API Calls (apiRequest - keep) ---
|
|
|
30 |
async function apiRequest(endpoint, method = 'GET', body = null, token = null) {
|
31 |
const headers = { 'Content-Type': 'application/json' };
|
32 |
if (token) { headers['Authorization'] = `Bearer ${token}`; }
|
@@ -43,7 +46,8 @@ async function apiRequest(endpoint, method = 'GET', body = null, token = null) {
|
|
43 |
}
|
44 |
|
45 |
|
46 |
-
// --- WebSocket Handling (connect/disconnectWebSocket - keep) ---
|
|
|
47 |
function connectWebSocket(token) {
|
48 |
if (!token) { console.error("No token for WebSocket."); return; }
|
49 |
if (webSocket && webSocket.readyState === WebSocket.OPEN) { console.log("WS already open."); return; }
|
@@ -77,7 +81,7 @@ function connectWebSocket(token) {
|
|
77 |
function disconnectWebSocket() {
|
78 |
if (webSocket) { console.log("Closing WS."); webSocket.close(); webSocket = null; }
|
79 |
}
|
80 |
-
function displayNotificationError(message) {
|
81 |
const p = document.createElement('p');
|
82 |
p.textContent = message;
|
83 |
p.style.color = 'orange';
|
@@ -87,6 +91,7 @@ function displayNotificationError(message) { // Helper for WS errors
|
|
87 |
|
88 |
|
89 |
// --- Authentication Logic (handleLogin, handleRegister, showWelcomePage, handleLogout - keep) ---
|
|
|
90 |
async function handleLogin(email, password) {
|
91 |
setStatus(loginStatus, "Logging in...");
|
92 |
try {
|
@@ -122,29 +127,22 @@ function handleLogout() {
|
|
122 |
localStorage.removeItem('authToken');
|
123 |
disconnectWebSocket();
|
124 |
setStatus(loginStatus, "");
|
125 |
-
// Reset notification display on logout
|
126 |
notificationsDiv.innerHTML = '<p><em>Notifications will appear here...</em></p>';
|
127 |
showSection('login-section');
|
128 |
}
|
129 |
|
130 |
-
// --- Theme
|
131 |
-
function applyTheme(
|
132 |
-
if (
|
133 |
document.body.classList.add('dark-mode');
|
|
|
134 |
} else {
|
135 |
document.body.classList.remove('dark-mode');
|
|
|
136 |
}
|
137 |
}
|
138 |
|
139 |
-
|
140 |
-
const isDarkMode = document.body.classList.toggle('dark-mode');
|
141 |
-
const newTheme = isDarkMode ? 'dark' : 'light';
|
142 |
-
localStorage.setItem('theme', newTheme); // Save preference
|
143 |
-
console.log("Theme toggled to:", newTheme);
|
144 |
-
}
|
145 |
-
|
146 |
-
|
147 |
-
// --- Event Listeners ---
|
148 |
registerForm.addEventListener('submit', (e) => {
|
149 |
e.preventDefault();
|
150 |
// ... (validation and call handleRegister - keep) ...
|
@@ -155,7 +153,6 @@ registerForm.addEventListener('submit', (e) => {
|
|
155 |
if (password.length < 8) { setStatus(registerStatus, "Password must be >= 8 characters."); return; }
|
156 |
handleRegister(email, password);
|
157 |
});
|
158 |
-
|
159 |
loginForm.addEventListener('submit', (e) => {
|
160 |
e.preventDefault();
|
161 |
// ... (call handleLogin - keep) ...
|
@@ -163,30 +160,38 @@ loginForm.addEventListener('submit', (e) => {
|
|
163 |
const password = document.getElementById('login-password').value;
|
164 |
handleLogin(email, password);
|
165 |
});
|
166 |
-
|
167 |
logoutButton.addEventListener('click', handleLogout);
|
168 |
-
|
169 |
-
themeToggleButton.addEventListener('click', toggleTheme); // Add listener
|
170 |
|
171 |
|
172 |
// --- Initial Page Load Logic ---
|
173 |
document.addEventListener('DOMContentLoaded', () => {
|
174 |
-
//
|
175 |
-
const
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
} else {
|
180 |
-
// Optional: Detect system preference if no theme stored
|
181 |
-
// const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
182 |
-
// applyTheme(prefersDark ? 'dark' : 'light');
|
183 |
-
// console.log("Applied system theme preference.");
|
184 |
-
applyTheme('light'); // Default to light if nothing else
|
185 |
-
console.log("Applied default light theme.");
|
186 |
-
}
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
-
//
|
190 |
if (authToken) {
|
191 |
console.log("Token found, showing welcome page.");
|
192 |
showWelcomePage();
|
|
|
1 |
+
// --- Existing Constants (remove theme button) ---
|
2 |
const registerSection = document.getElementById('register-section');
|
3 |
const loginSection = document.getElementById('login-section');
|
4 |
const welcomeSection = document.getElementById('welcome-section');
|
5 |
// ... other element constants ...
|
6 |
const notificationsDiv = document.getElementById('notifications');
|
7 |
+
const API_URL = '/api';
|
8 |
+
// const themeToggleButton = document.getElementById('theme-toggle'); // REMOVE
|
9 |
|
10 |
+
// --- Existing State Variables ---
|
11 |
let webSocket = null;
|
12 |
+
let authToken = localStorage.getItem('authToken');
|
13 |
|
14 |
// --- UI Control (showSection, setStatus - keep) ---
|
15 |
+
// ... showSection and setStatus functions remain the same ...
|
16 |
function showSection(sectionId) {
|
17 |
registerSection.style.display = 'none';
|
18 |
loginSection.style.display = 'none';
|
|
|
27 |
element.style.display = message ? 'block' : 'none';
|
28 |
}
|
29 |
|
30 |
+
|
31 |
// --- API Calls (apiRequest - keep) ---
|
32 |
+
// ... apiRequest function remains the same ...
|
33 |
async function apiRequest(endpoint, method = 'GET', body = null, token = null) {
|
34 |
const headers = { 'Content-Type': 'application/json' };
|
35 |
if (token) { headers['Authorization'] = `Bearer ${token}`; }
|
|
|
46 |
}
|
47 |
|
48 |
|
49 |
+
// --- WebSocket Handling (connect/disconnectWebSocket, displayNotificationError - keep) ---
|
50 |
+
// ... connectWebSocket, disconnectWebSocket, displayNotificationError functions remain the same ...
|
51 |
function connectWebSocket(token) {
|
52 |
if (!token) { console.error("No token for WebSocket."); return; }
|
53 |
if (webSocket && webSocket.readyState === WebSocket.OPEN) { console.log("WS already open."); return; }
|
|
|
81 |
function disconnectWebSocket() {
|
82 |
if (webSocket) { console.log("Closing WS."); webSocket.close(); webSocket = null; }
|
83 |
}
|
84 |
+
function displayNotificationError(message) {
|
85 |
const p = document.createElement('p');
|
86 |
p.textContent = message;
|
87 |
p.style.color = 'orange';
|
|
|
91 |
|
92 |
|
93 |
// --- Authentication Logic (handleLogin, handleRegister, showWelcomePage, handleLogout - keep) ---
|
94 |
+
// ... handleLogin, handleRegister, showWelcomePage, handleLogout functions remain the same ...
|
95 |
async function handleLogin(email, password) {
|
96 |
setStatus(loginStatus, "Logging in...");
|
97 |
try {
|
|
|
127 |
localStorage.removeItem('authToken');
|
128 |
disconnectWebSocket();
|
129 |
setStatus(loginStatus, "");
|
|
|
130 |
notificationsDiv.innerHTML = '<p><em>Notifications will appear here...</em></p>';
|
131 |
showSection('login-section');
|
132 |
}
|
133 |
|
134 |
+
// --- Theme Handling ---
|
135 |
+
function applyTheme(isDarkMode) {
|
136 |
+
if (isDarkMode) {
|
137 |
document.body.classList.add('dark-mode');
|
138 |
+
console.log("Applied dark theme.");
|
139 |
} else {
|
140 |
document.body.classList.remove('dark-mode');
|
141 |
+
console.log("Applied light theme.");
|
142 |
}
|
143 |
}
|
144 |
|
145 |
+
// --- Event Listeners (Forms & Logout) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
registerForm.addEventListener('submit', (e) => {
|
147 |
e.preventDefault();
|
148 |
// ... (validation and call handleRegister - keep) ...
|
|
|
153 |
if (password.length < 8) { setStatus(registerStatus, "Password must be >= 8 characters."); return; }
|
154 |
handleRegister(email, password);
|
155 |
});
|
|
|
156 |
loginForm.addEventListener('submit', (e) => {
|
157 |
e.preventDefault();
|
158 |
// ... (call handleLogin - keep) ...
|
|
|
160 |
const password = document.getElementById('login-password').value;
|
161 |
handleLogin(email, password);
|
162 |
});
|
|
|
163 |
logoutButton.addEventListener('click', handleLogout);
|
164 |
+
// themeToggleButton.addEventListener('click', toggleTheme); // REMOVE
|
|
|
165 |
|
166 |
|
167 |
// --- Initial Page Load Logic ---
|
168 |
document.addEventListener('DOMContentLoaded', () => {
|
169 |
+
// System Theme Detection
|
170 |
+
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
|
171 |
+
|
172 |
+
// Apply theme based on current system preference
|
173 |
+
applyTheme(prefersDarkScheme.matches);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
+
// Listen for changes in system theme preference
|
176 |
+
try {
|
177 |
+
// Newer browsers
|
178 |
+
prefersDarkScheme.addEventListener('change', (e) => {
|
179 |
+
console.log("System theme preference changed.");
|
180 |
+
applyTheme(e.matches);
|
181 |
+
});
|
182 |
+
} catch (e1) {
|
183 |
+
try {
|
184 |
+
// Older browsers (legacy method)
|
185 |
+
prefersDarkScheme.addListener((e) => {
|
186 |
+
console.log("System theme preference changed (legacy listener).");
|
187 |
+
applyTheme(e.matches);
|
188 |
+
});
|
189 |
+
} catch (e2) {
|
190 |
+
console.error("Browser doesn't support dynamic theme changes via matchMedia listeners.");
|
191 |
+
}
|
192 |
+
}
|
193 |
|
194 |
+
// Check auth token after setting theme
|
195 |
if (authToken) {
|
196 |
console.log("Token found, showing welcome page.");
|
197 |
showWelcomePage();
|
templates/index.html
CHANGED
@@ -10,7 +10,8 @@
|
|
10 |
<div class="container">
|
11 |
<div class="header">
|
12 |
<h1>Auth & Notification App</h1>
|
13 |
-
|
|
|
14 |
</div>
|
15 |
|
16 |
<!-- Registration Section -->
|
|
|
10 |
<div class="container">
|
11 |
<div class="header">
|
12 |
<h1>Auth & Notification App</h1>
|
13 |
+
<!-- REMOVE THE BUTTON HERE -->
|
14 |
+
<!-- <button id="theme-toggle">Toggle Theme</button> -->
|
15 |
</div>
|
16 |
|
17 |
<!-- Registration Section -->
|