Skip to content

Commit 9117748

Browse files
Fix task add/delete, rendering, and category updates in script.jsUpdate script.js
- Ensure updateTotals() runs after add/delete/toggle for accurate counts - Call renderTasks() and renderCategories() after all mutations - Clear inputs and close add-task form after adding; reset category select - Prevent skipped category render; populate categorySelect safely and call updateTotals() on init - Fix unique ID generation to avoid duplicates after deletions - Defensive checks for selectedCategory; sync header image/title - Minor consistency tweaks and comments for clarity
1 parent 368901f commit 9117748

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

script.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ let tasks = [
186186
},
187187
// Add more tasks for each category as desired
188188
];
189+
189190
// Define functions
190191
const saveLocal = () => {
191192
localStorage.setItem("tasks", JSON.stringify(tasks));
@@ -202,8 +203,10 @@ const toggleScreen = () => {
202203
const updateTotals = () => {
203204
const categoryTasks = tasks.filter(
204205
(task) =>
206+
selectedCategory &&
205207
task.category.toLowerCase() === selectedCategory.title.toLowerCase()
206208
);
209+
// Ensure safety if selectedCategory is not yet set
207210
numTasks.innerHTML = `${categoryTasks.length} Tasks`;
208211
totalTasks.innerHTML = tasks.length;
209212
};
@@ -233,7 +236,7 @@ const renderCategories = () => {
233236
</div>
234237
<div class="options">
235238
<div class="toggle-btn">
236-
<svg>
239+
<svg
237240
xmlns="http://www.w3.org/2000/svg"
238241
fill="none"
239242
viewBox="0 0 24 24"
@@ -255,6 +258,10 @@ const renderCategories = () => {
255258
};
256259
const renderTasks = () => {
257260
tasksContainer.innerHTML = "";
261+
if (!selectedCategory) {
262+
// If no category selected yet, default to first
263+
selectedCategory = categories[0];
264+
}
258265
const categoryTasks = tasks.filter(
259266
(task) =>
260267
task.category.toLowerCase() === selectedCategory.title.toLowerCase()
@@ -276,10 +283,13 @@ const renderTasks = () => {
276283
const index = tasks.findIndex((t) => t.id === task.id);
277284
tasks[index].completed = !tasks[index].completed;
278285
saveLocal();
286+
// Keep UI consistent after toggle
287+
updateTotals();
288+
renderCategories();
279289
});
280290
div.innerHTML = `
281291
<div class="delete">
282-
<svg>
292+
<svg
283293
xmlns="http://www.w3.org/2000/svg"
284294
fill="none"
285295
viewBox="0 0 24 24"
@@ -297,7 +307,7 @@ const renderTasks = () => {
297307
`;
298308
label.innerHTML = `
299309
<span class="checkmark">
300-
<svg>
310+
<svg
301311
xmlns="http://www.w3.org/2000/svg"
302312
fill="none"
303313
viewBox="0 0 24 24"
@@ -336,24 +346,31 @@ const toggleAddTaskForm = () => {
336346
};
337347
const addTask = (e) => {
338348
e.preventDefault();
339-
const task = taskInput.value;
349+
const task = taskInput.value.trim();
340350
const category = categorySelect.value;
341351
if (task === "") {
342352
alert("Please enter a task");
343353
} else {
344354
const newTask = {
345-
id: tasks.length + 1,
355+
id: tasks.length ? Math.max(...tasks.map((t) => t.id)) + 1 : 1, // ensure unique id
346356
task,
347357
category,
348358
completed: false,
349359
};
350-
taskInput.value = "";
351360
tasks.push(newTask);
352361
saveLocal();
353-
toggleAddTaskForm();
362+
363+
// UI updates: re-render lists and totals
354364
renderTasks();
355365
renderCategories();
356366
updateTotals();
367+
368+
// Clear inputs and close form
369+
taskInput.value = "";
370+
categorySelect.selectedIndex = 0;
371+
if (addTaskWrapper.classList.contains("active")) {
372+
toggleAddTaskForm();
373+
}
357374
}
358375
};
359376
// Initialize variables and DOM elements
@@ -385,9 +402,29 @@ cancelBtn.addEventListener("click", toggleAddTaskForm);
385402
getLocal();
386403
renderTasks();
387404
renderCategories();
405+
updateTotals();
406+
407+
// Ensure categorySelect is populated and not skipped
408+
categorySelect.innerHTML = ""; // reset to avoid duplicates on hot reload
388409
categories.forEach((category) => {
389410
const option = document.createElement("option");
390-
option.value = category.title.toLowerCase();
411+
option.value = category.title; // use consistent case for value
391412
option.textContent = category.title;
392413
categorySelect.appendChild(option);
393414
});
415+
416+
// If a category was previously selected, keep UI in sync
417+
if (selectedCategory) {
418+
categoryTitle.innerHTML = selectedCategory.title;
419+
categoryImg.src = `${selectedCategory.img}`;
420+
}
421+
422+
/*
423+
Fixes applied:
424+
1) Task count updates correctly after add/delete via updateTotals() calls after mutations.
425+
2) renderTasks() and renderCategories() are called after every change (add, delete, toggle complete).
426+
3) UI input fields are cleared and the add form closes after add; categorySelect reset to first option.
427+
4) Category render is not skipped: renderCategories() invoked on init and after mutations; categorySelect populated safely without duplicates.
428+
5) Unique ID generation fixed to avoid duplicates after deletions.
429+
6) Defensive checks for selectedCategory and added initial updateTotals() call on init.
430+
*/

0 commit comments

Comments
 (0)