-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
484 lines (423 loc) · 13 KB
/
script.js
File metadata and controls
484 lines (423 loc) · 13 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
const API_URL = "http://localhost:3000";
let currentUser = null;
let darkMode = true; // Start with dark mode by default
let currentFilter = "all";
let selectedPriority = "medium"; // Default priority
document.addEventListener("DOMContentLoaded", () => {
// Initialize theme based on local storage or default
const savedTheme = localStorage.getItem("theme");
if (savedTheme === "light") {
darkMode = false;
document.body.classList.remove("dark-mode");
} else {
document.body.classList.add("dark-mode");
}
// Set up theme toggle
document
.getElementById("theme-toggle")
.addEventListener("click", toggleTheme);
// Set up user logo click for menu
document
.getElementById("user-logo")
.addEventListener("click", toggleUserMenu);
// Close user menu when clicking elsewhere
document.addEventListener("click", (e) => {
const userMenu = document.getElementById("user-menu");
const userLogo = document.getElementById("user-logo");
if (
userMenu.style.display === "block" &&
e.target !== userLogo &&
!userMenu.contains(e.target)
) {
userMenu.style.display = "none";
}
});
// Set up priority dropdown
document
.getElementById("priority-btn")
.addEventListener("click", togglePriorityMenu);
// Close priority menu when clicking elsewhere
document.addEventListener("click", (e) => {
const priorityMenu = document.getElementById("priority-menu");
const priorityBtn = document.getElementById("priority-btn");
if (
priorityMenu.style.display === "block" &&
e.target !== priorityBtn &&
!priorityBtn.contains(e.target) &&
!priorityMenu.contains(e.target)
) {
priorityMenu.style.display = "none";
}
});
// Set up priority options
document.querySelectorAll(".priority-option").forEach((option) => {
option.addEventListener("click", () => {
selectedPriority = option.getAttribute("data-priority");
document.getElementById(
"priority-btn"
).innerHTML = `${capitalizeFirstLetter(
selectedPriority
)} Priority <span class="dropdown-arrow">▼</span>`;
togglePriorityMenu();
});
});
// Set up filter tabs
document.querySelectorAll(".filter-tab").forEach((tab) => {
tab.addEventListener("click", () => {
document
.querySelectorAll(".filter-tab")
.forEach((t) => t.classList.remove("active"));
tab.classList.add("active");
currentFilter = tab.getAttribute("data-filter");
filterTodos();
});
});
// Set up clear all button
document
.getElementById("clear-all-btn")
.addEventListener("click", clearAllTasks);
// Set up event listeners for auth
document
.getElementById("signup-password")
.addEventListener("keypress", (e) => {
if (e.key === "Enter") signup();
});
document
.getElementById("signin-password")
.addEventListener("keypress", (e) => {
if (e.key === "Enter") signin();
});
document.getElementById("input").addEventListener("keypress", (e) => {
if (e.key === "Enter") addTodo();
});
// Check if user is already logged in
checkAuthStatus();
});
function toggleTheme() {
darkMode = !darkMode;
if (darkMode) {
document.body.classList.add("dark-mode");
localStorage.setItem("theme", "dark");
} else {
document.body.classList.remove("dark-mode");
localStorage.setItem("theme", "light");
}
}
function toggleUserMenu() {
const userMenu = document.getElementById("user-menu");
userMenu.style.display =
userMenu.style.display === "block" ? "none" : "block";
}
function togglePriorityMenu() {
const priorityMenu = document.getElementById("priority-menu");
priorityMenu.style.display =
priorityMenu.style.display === "block" ? "none" : "block";
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function checkAuthStatus() {
const token = localStorage.getItem("token");
const username = localStorage.getItem("username");
if (token && username) {
currentUser = username;
document.getElementById("user-name").textContent = username;
showTodoApp();
} else {
moveToSignin();
}
}
function moveToSignup() {
showSection("signup-container");
}
function moveToSignin() {
showSection("signin-container");
}
function showTodoApp() {
showSection("todos-container");
document.body.classList.add("user-logged-in");
getTodos();
}
function showSection(id) {
document
.querySelectorAll(".section")
.forEach((div) => (div.style.display = "none"));
document.getElementById(id).style.display = "block";
}
async function signup() {
const username = document.getElementById("signup-username").value.trim();
const password = document.getElementById("signup-password").value;
if (!username || !password) return alert("All fields are required");
try {
const res = await axios.post(`${API_URL}/signup`, { username, password });
alert(res.data.message);
if (res.data.message.includes("signedup")) moveToSignin();
} catch (err) {
alert(err.response?.data?.message || "Signup error");
}
}
async function signin() {
const username = document.getElementById("signin-username").value.trim();
const password = document.getElementById("signin-password").value;
if (!username || !password) return alert("All fields are required");
try {
const res = await axios.post(`${API_URL}/signin`, { username, password });
if (res.data.token) {
localStorage.setItem("token", res.data.token);
localStorage.setItem("username", username);
currentUser = username;
document.getElementById("user-name").textContent = username;
showTodoApp();
} else {
alert(res.data.message);
}
} catch (err) {
alert(err.response?.data?.message || "Signin error");
}
}
function logout() {
localStorage.removeItem("token");
localStorage.removeItem("username");
currentUser = null;
document.body.classList.remove("user-logged-in");
// Hide user menu
document.getElementById("user-menu").style.display = "none";
moveToSignin();
}
async function getTodos() {
try {
const token = localStorage.getItem("token");
const res = await axios.get(`${API_URL}/todos`, {
headers: { Authorization: token },
});
// Store todos in memory for filtering
window.todos = res.data.map((todo) => ({
...todo,
completed: todo.done,
priority: todo.priority || "medium",
}));
// Apply current filter
filterTodos();
// Update stats
updateStats();
} catch (err) {
console.error("Error fetching todos:", err);
if (err.response?.status === 401) {
// Token expired or invalid
logout();
alert("Session expired. Please sign in again.");
} else {
alert("Error fetching tasks");
}
}
}
function filterTodos() {
if (!window.todos) return;
const list = document.getElementById("todos-list");
list.innerHTML = "";
let filteredTodos = [...window.todos];
// Apply filters
switch (currentFilter) {
case "completed":
filteredTodos = filteredTodos.filter((todo) => todo.completed);
break;
case "pending":
filteredTodos = filteredTodos.filter((todo) => !todo.completed);
break;
case "high":
filteredTodos = filteredTodos.filter((todo) => todo.priority === "high");
break;
case "medium":
filteredTodos = filteredTodos.filter(
(todo) => todo.priority === "medium"
);
break;
case "low":
filteredTodos = filteredTodos.filter((todo) => todo.priority === "low");
break;
// "all" case - no filtering needed
}
if (filteredTodos.length) {
filteredTodos.forEach((todo) => {
const el = createTodoElement(todo);
list.appendChild(el);
});
} else {
// Show empty state
list.innerHTML = `
<div class="empty-state">
<div class="empty-icon">📋</div>
<p>No tasks found</p>
</div>
`;
}
}
function updateStats() {
if (!window.todos) return;
const totalCount = window.todos.length;
const completedCount = window.todos.filter((todo) => todo.completed).length;
const pendingCount = totalCount - completedCount;
document.getElementById("total-count").textContent = totalCount;
document.getElementById("completed-count").textContent = completedCount;
document.getElementById("pending-count").textContent = pendingCount;
}
async function addTodo() {
const input = document.getElementById("input");
const title = input.value.trim();
if (!title) return alert("Please enter a task");
try {
const token = localStorage.getItem("token");
await axios.post(
`${API_URL}/todos`,
{
title,
priority: selectedPriority,
},
{
headers: { Authorization: token },
}
);
input.value = "";
getTodos();
} catch (err) {
console.error("Error adding todo:", err);
alert("Failed to add task");
}
}
async function updateTodo(id, updates) {
try {
const token = localStorage.getItem("token");
await axios.put(`${API_URL}/todos/${id}`, updates, {
headers: { Authorization: token },
});
getTodos();
} catch (err) {
console.error("Error updating todo:", err);
alert("Failed to update task");
}
}
async function deleteTodo(id) {
if (!confirm("Are you sure you want to delete this task?")) return;
try {
const token = localStorage.getItem("token");
await axios.delete(`${API_URL}/todos/${id}`, {
headers: { Authorization: token },
});
// Find and animate the element
const el = document.querySelector(`.todo-item[data-id='${id}']`);
if (el) {
el.classList.add("fade-out");
setTimeout(() => {
// Update local data
window.todos = window.todos.filter((todo) => todo.id !== id);
updateStats();
filterTodos();
}, 300);
}
} catch (err) {
console.error("Error deleting todo:", err);
alert("Failed to delete task");
}
}
async function toggleTodoCompleted(id, completed) {
try {
const token = localStorage.getItem("token");
await axios.put(
`${API_URL}/todos/${id}/done`,
{},
{
headers: { Authorization: token },
}
);
// Update local data first for smoother UX
const todoIndex = window.todos.findIndex((todo) => todo.id === id);
if (todoIndex !== -1) {
window.todos[todoIndex].completed = completed;
updateStats();
}
// Then refresh from server to ensure data consistency
getTodos();
} catch (err) {
console.error("Error toggling todo status:", err);
alert("Failed to update task status");
}
}
function createTodoElement(todo) {
const div = document.createElement("div");
div.className = `todo-item priority-${todo.priority || "medium"}`;
div.setAttribute("data-id", todo.id);
if (todo.completed) div.classList.add("done");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = todo.completed;
checkbox.className = "todo-checkbox";
checkbox.onchange = () => toggleTodoCompleted(todo.id, checkbox.checked);
const input = document.createElement("input");
input.type = "text";
input.value = todo.title;
input.className = "todo-text";
input.readOnly = true;
const priorityBadge = document.createElement("span");
priorityBadge.className = `priority-badge priority-${todo.priority}`;
priorityBadge.textContent = `${capitalizeFirstLetter(todo.priority)}`;
const actionsDiv = document.createElement("div");
actionsDiv.className = "todo-item-actions";
const editBtn = document.createElement("button");
editBtn.textContent = "Edit";
editBtn.className = "edit-btn";
editBtn.onclick = () => {
if (input.readOnly) {
input.readOnly = false;
editBtn.textContent = "Save";
input.focus();
} else {
const newTitle = input.value.trim();
if (newTitle) {
updateTodo(todo.id, { title: newTitle });
} else {
alert("Task cannot be empty");
input.value = todo.title;
}
input.readOnly = true;
editBtn.textContent = "Edit";
}
};
const deleteBtn = document.createElement("button");
deleteBtn.textContent = "Delete";
deleteBtn.className = "delete-btn";
deleteBtn.onclick = () => deleteTodo(todo.id);
actionsDiv.append(editBtn, deleteBtn);
div.append(checkbox, input, priorityBadge, actionsDiv);
return div;
}
async function clearAllTasks() {
if (!window.todos || window.todos.length === 0) {
alert("No tasks to clear");
return;
}
if (
!confirm(
"Are you sure you want to delete all tasks? This action cannot be undone."
)
) {
return;
}
try {
const token = localStorage.getItem("token");
await axios.delete(`${API_URL}/todos/all`, {
headers: { Authorization: token },
});
window.todos = [];
updateStats();
filterTodos();
} catch (err) {
console.error("Error clearing all todos:", err);
alert("Failed to clear tasks");
}
}
// Initialize the priority button text
document.addEventListener("DOMContentLoaded", () => {
document.getElementById(
"priority-btn"
).innerHTML = `Medium Priority <span class="dropdown-arrow">▼</span>`;
});