forked from latakshsariyapatidar/TaskManager_UM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
103 lines (89 loc) · 3.47 KB
/
Copy pathscript.js
File metadata and controls
103 lines (89 loc) · 3.47 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
document.addEventListener("DOMContentLoaded", () => {
const createBtn = document.getElementById("create_task");
// CREATE TASK (Only available on index.html)
if (createBtn) {
createBtn.addEventListener("click", function (e) {
e.preventDefault();
const title = document.getElementById("task_input").value;
const description = document.getElementById("task_description").value;
const dueDate = document.getElementById("due_date").value;
const priority =
document.getElementById("priority").value === "imp"
? "Important"
: "Not Important";
if (!title.trim()) {
alert("Please enter a task title");
return;
}
const task = { title, description, dueDate, priority, completed: false };
let tasks = JSON.parse(localStorage.getItem("tasks")) || [];
tasks.push(task);
localStorage.setItem("tasks", JSON.stringify(tasks));
window.location.href = "/look_tasks.html";
});
}
// DISPLAY & DELETE TASKS (Only on look_tasks.html)
const taskContainer = document.querySelector(".task_container");
if (taskContainer) {
let tasks = JSON.parse(localStorage.getItem("tasks")) || [];
if (tasks.length === 0) {
taskContainer.innerHTML = "<p>No tasks yet.</p>";
return;
}
taskContainer.innerHTML = ""; // Clear any pre-existing content
tasks.forEach((task, index) => {
const taskDiv = document.createElement("div");
taskDiv.classList.add("task");
// Check if task is completed
const taskClass = task.completed ? "completed-task" : "";
taskDiv.innerHTML = `
<div class="${taskClass}">
<input type="checkbox" class="completeCheck" data-index="${index}" ${
task.completed ? "checked" : ""
}>
<h1>${task.title}</h1>
<p>${task.description}</p>
<div id="detContainer">
<p>Due Date: ${task.dueDate}</p>
<p>Priority: ${task.priority}</p>
</div>
<button class="deleteBtn" data-index="${index}">Delete</button>
<button class="editBtn" data-index="${index}">Edit</button>
</div>
`;
taskContainer.appendChild(taskDiv);
});
// Add listeners for deletion
document.querySelectorAll(".deleteBtn").forEach((btn) => {
btn.addEventListener("click", function () {
const index = this.getAttribute("data-index");
tasks.splice(index, 1);
localStorage.setItem("tasks", JSON.stringify(tasks));
location.reload();
});
});
// Add listeners for editing
document.querySelectorAll(".editBtn").forEach((btn) => {
btn.addEventListener("click", function () {
const index = this.getAttribute("data-index");
localStorage.setItem("editIndex", index);
window.location.href = "/edit.html";
});
});
document.querySelectorAll(".completeCheck").forEach((checkbox) => {
checkbox.addEventListener("change", function () {
const index = this.getAttribute("data-index");
tasks[index].completed = this.checked;
localStorage.setItem("tasks", JSON.stringify(tasks));
location.reload();
});
});
}
});
document.querySelectorAll(".editBtn").forEach((btn) => {
btn.addEventListener("click", function () {
const index = this.getAttribute("data-index");
localStorage.setItem("editIndex", index); // store index
window.location.href = "/edit.html";
});
});