-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTM.html
178 lines (157 loc) · 5.67 KB
/
TM.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/svg+xml" href="./images/logo.png" />
<title>Chores</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1 class="chorehead">Manage Your Chores</h1>
<div id="main-content">
<!-- Add Chores Section -->
<div id="add-chores">
<h2>ADD CHORES</h2>
<input id="title" type="text" placeholder="Title here" />
<textarea id="description" placeholder="Description here"></textarea>
<button id="add-button" onclick="addtodo()">Add Todo</button>
</div>
<!-- Task List Section -->
<div id="todos">
<h2>TASKS</h2>
<button id="toggle-button" onclick="toggleTaskView()">
Show All Tasks
</button>
<div id="task-list"></div>
</div>
</div>
<script>
let showAllTasks = false;
function getToken() {
return localStorage.getItem("token");
}
function toggleTaskView() {
showAllTasks = !showAllTasks;
document.getElementById("toggle-button").innerText = showAllTasks
? "Show Incomplete Tasks"
: "Show All Tasks";
getTasks();
}
async function addtodo() {
const title = document.getElementById("title").value.trim();
const description = document.getElementById("description").value.trim();
// Check if title or description is empty
if (!title || !description) {
alert("Both title and description are required!");
return; // Stop function execution if fields are empty
}
const token = getToken();
try {
const response = await fetch("http://localhost:5500/tasks", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ title, description }),
});
if (response.ok) {
alert("Task added successfully!");
getTasks();
} else {
alert("Failed to add task.");
}
} catch (error) {
console.error(error);
alert("An error occurred. Please try again.");
}
}
async function getTasks() {
try {
const token = getToken();
const response = await fetch("http://localhost:5500/tasks", {
headers: {
Authorization: `Bearer ${token}`,
},
});
if (response.ok) {
const tasks = await response.json();
const tasksToDisplay = showAllTasks
? tasks
: tasks.filter((task) => !task.completed);
displayTasks(tasksToDisplay);
} else {
console.error("Failed to fetch tasks.");
}
} catch (error) {
console.error(error);
alert("An error occurred. Please try again.");
}
}
function displayTasks(tasks) {
const taskList = document.getElementById("task-list");
taskList.innerHTML = "";
tasks.forEach((task) => {
const taskElement = createTaskElement(task);
taskList.appendChild(taskElement);
});
}
function createTaskElement(task) {
const taskDiv = document.createElement("div");
taskDiv.classList.add("task", "custom-task-style");
const titleDiv = document.createElement("div");
titleDiv.classList.add("task-title");
titleDiv.innerHTML = task.title;
const descriptionDiv = document.createElement("div");
descriptionDiv.classList.add("task-description");
descriptionDiv.innerHTML = task.description;
const createdAtDiv = document.createElement("div");
const createdAt = new Date(task.createdAt).toLocaleString();
createdAtDiv.classList.add("task-time");
createdAtDiv.innerHTML = `Created on: ${createdAt}`;
const doneButton = document.createElement("button");
doneButton.classList.add("complete-button");
doneButton.innerHTML = task.completed ? "Completed" : "Mark as Done";
doneButton.addEventListener("click", async () => {
try {
const token = getToken();
const newStatus = !task.completed;
const response = await fetch(
`http://localhost:5500/tasks/${task._id}/complete`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ completed: newStatus }),
}
);
if (response.ok) {
task.completed = newStatus;
doneButton.innerHTML = newStatus ? "Completed" : "Mark as Done";
titleDiv.classList.toggle("completed", newStatus);
descriptionDiv.classList.toggle("completed", newStatus);
} else {
alert("Failed to update task status.");
}
} catch (error) {
console.error(error);
alert("An error occurred. Please try again.");
}
});
taskDiv.appendChild(titleDiv);
taskDiv.appendChild(descriptionDiv);
taskDiv.appendChild(createdAtDiv);
taskDiv.appendChild(doneButton);
if (task.completed) {
titleDiv.classList.add("completed");
descriptionDiv.classList.add("completed");
}
return taskDiv;
}
window.onload = getTasks;
</script>
</body>
</html>