forked from opensource-coding/Todo-List-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
409 lines (392 loc) · 10.8 KB
/
script.js
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
let categories = [
{
title: "Personal",
img: "boy.png",
},
{
title: "Work",
img: "briefcase.png",
},
{
title: "Shopping",
img: "shopping.png",
},
{
title: "Coding",
img: "web-design.png",
},
{
title: "Health",
img: "healthcare.png",
},
{
title: "Fitness",
img: "dumbbell.png",
},
{
title: "Education",
img: "education.png",
},
{
title: "Finance",
img: "saving.png",
},
];
let tasks = [
{
id: 1,
task: "Go to market",
category: "Shopping",
completed: false,
},
{
id: 2,
task: "Read a chapter of a book",
category: "Personal",
completed: false,
},
{
id: 3,
task: "Prepare presentation for meeting",
category: "Work",
completed: false,
},
{
id: 4,
task: "Complete coding challenge",
category: "Coding",
completed: false,
},
{
id: 5,
task: "Take a 30-minute walk",
category: "Health",
completed: false,
},
{
id: 6,
task: "Do a 20-minute HIIT workout",
category: "Fitness",
completed: false,
},
{
id: 7,
task: "Watch an educational video online",
category: "Education",
completed: false,
},
{
id: 8,
task: "Review monthly budget",
category: "Finance",
completed: false,
},
{
id: 9,
task: "Buy groceries for the week",
category: "Shopping",
completed: false,
},
{
id: 10,
task: "Write in a journal",
category: "Personal",
completed: false,
},
{
id: 11,
task: "Send follow-up emails",
category: "Work",
completed: false,
},
{
id: 12,
task: "Work on a coding side project",
category: "Coding",
completed: false,
},
{
id: 13,
task: "Try a new healthy recipe",
category: "Health",
completed: false,
},
{
id: 14,
task: "Attend a yoga class",
category: "Fitness",
completed: false,
},
{
id: 15,
task: "Read an article about a new topic",
category: "Education",
completed: false,
},
{
id: 16,
task: "Set up automatic bill payments",
category: "Finance",
completed: false,
},
// Additional tasks for each category
{
id: 17,
task: "Buy new clothes",
category: "Shopping",
completed: false,
},
{
id: 18,
task: "Meditate for 10 minutes",
category: "Personal",
completed: false,
},
{
id: 19,
task: "Prepare agenda for team meeting",
category: "Work",
completed: false,
},
{
id: 20,
task: "Debug a software issue",
category: "Coding",
completed: false,
},
{
id: 21,
task: "Try a new recipe for lunch",
category: "Health",
completed: false,
},
{
id: 22,
task: "Go for a run",
category: "Fitness",
completed: false,
},
{
id: 23,
task: "Learn a new language online",
category: "Education",
completed: false,
},
{
id: 24,
task: "Read about history",
category: "Education",
completed: false,
},
{
id: 25,
task: "Review investment portfolio",
category: "Finance",
completed: false,
},
// Add more tasks for each category as desired
];
// Define functions
const saveLocal = () => {
localStorage.setItem("tasks", JSON.stringify(tasks));
};
const getLocal = () => {
const tasksLocal = JSON.parse(localStorage.getItem("tasks"));
if (tasksLocal) {
tasks = tasksLocal;
}
};
const toggleScreen = () => {
screenWrapper.classList.toggle("show-category");
};
const updateTotals = () => {
const categoryTasks = tasks.filter(
(task) =>
task.category.toLowerCase() === selectedCategory.title.toLowerCase()
);
numTasks.innerHTML = `${categoryTasks.length} Tasks`;
totalTasks.innerHTML = tasks.length;
};
const renderCategories = () => {
categoriesContainer.innerHTML = "";
categories.forEach((category) => {
const categoryTasks = tasks.filter(
(task) => task.category.toLowerCase() === category.title.toLowerCase()
);
const div = document.createElement("div");
div.classList.add("category");
div.addEventListener("click", () => {
screenWrapper.classList.toggle("show-category");
selectedCategory = category;
updateTotals();
categoryTitle.innerHTML = category.title;
categoryImg.src = `images/${category.img}`;
renderTasks();
});
div.innerHTML = `
<div class="left">
<img src="images/${category.img}"
alt="${category.title}"
/>
<div class="content">
<h1>${category.title}</h1>
<p>${categoryTasks.length} Tasks</p>
</div>
</div>
<div class="options">
<div class="toggle-btn">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M12 6.75a.75.75 0 110-1.5.75.75 0 010 1.5zM12 12.75a.75.75 0 110-1.5.75.75 0 010 1.5zM12 18.75a.75.75 0 110-1.5.75.75 0 010 1.5z"
/>
</svg>
</div>
</div>
`;
categoriesContainer.appendChild(div);
});
};
const renderTasks = () => {
tasksContainer.innerHTML = "";
const categoryTasks = tasks.filter(
(task) =>
task.category.toLowerCase() === selectedCategory.title.toLowerCase()
);
if (categoryTasks.length === 0) {
tasksContainer.innerHTML = `<p class="no-tasks">No tasks added for this category</p>`;
} else {
categoryTasks.forEach((task) => {
const div = document.createElement("div");
div.classList.add("task-wrapper");
const label = document.createElement("label");
label.classList.add("task");
label.setAttribute("for", task.id);
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.id = task.id;
checkbox.checked = task.completed;
checkbox.addEventListener("change", () => {
const index = tasks.findIndex((t) => t.id === task.id);
tasks[index].completed = !tasks[index].completed;
saveLocal();
});
div.innerHTML = `
<div class="delete">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0"
/>
</svg>
</div>
`;
label.innerHTML = `
<span class="checkmark"
><svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M4.5 12.75l6 6 9-13.5"
/>
</svg>
</span>
<p>${task.task}</p>
`;
label.prepend(checkbox);
div.prepend(label);
tasksContainer.appendChild(div);
const deleteBtn = div.querySelector(".delete");
deleteBtn.addEventListener("click", () => {
const index = tasks.findIndex((t) => t.id === task.id);
tasks.splice(index, 1);
saveLocal();
renderTasks();
});
});
renderCategories();
updateTotals();
}
};
const toggleAddTaskForm = () => {
addTaskWrapper.classList.toggle("active");
blackBackdrop.classList.toggle("active");
addTaskBtn.classList.toggle("active");
};
const addTask = (e) => {
e.preventDefault();
const task = taskInput.value;
const category = categorySelect.value;
if (task === "") {
alert("Please enter a task");
} else {
const newTask = {
id: tasks.length + 1,
task,
category,
completed: false,
};
taskInput.value = "";
tasks.push(newTask);
saveLocal();
toggleAddTaskForm();
renderTasks();
}
};
// Initialize variables and DOM elements
let selectedCategory = categories[0];
const categoriesContainer = document.querySelector(".categories");
const screenWrapper = document.querySelector(".wrapper");
const menuBtn = document.querySelector(".menu-btn");
const backBtn = document.querySelector(".back-btn");
const tasksContainer = document.querySelector(".tasks");
const numTasks = document.getElementById("num-tasks");
const categoryTitle = document.getElementById("category-title");
const categoryImg = document.getElementById("category-img");
const categorySelect = document.getElementById("category-select");
const addTaskWrapper = document.querySelector(".add-task");
const addTaskBtn = document.querySelector(".add-task-btn");
const taskInput = document.getElementById("task-input");
const blackBackdrop = document.querySelector(".black-backdrop");
const addBtn = document.querySelector(".add-btn");
const cancelBtn = document.querySelector(".cancel-btn");
const totalTasks = document.getElementById("total-tasks");
// Attach event listeners
menuBtn.addEventListener("click", toggleScreen);
backBtn.addEventListener("click", toggleScreen);
addTaskBtn.addEventListener("click", toggleAddTaskForm);
blackBackdrop.addEventListener("click", toggleAddTaskForm);
addBtn.addEventListener("click", addTask);
cancelBtn.addEventListener("click", toggleAddTaskForm);
// Render initial state
getLocal();
renderTasks();
categories.forEach((category) => {
const option = document.createElement("option");
option.value = category.title.toLowerCase();
option.textContent = category.title;
categorySelect.appendChild(option);
});