-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
463 lines (393 loc) · 11.6 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
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
const wrapper = document.querySelector(".wrapper");
const backBtn = document.querySelector(".back-btn");
const menuBtn = document.querySelector(".menu-btn");
const toggleScreen = () => {
wrapper.classList.toggle("show-category")
};
menuBtn.addEventListener("click", toggleScreen);
backBtn.addEventListener("click", toggleScreen);
const addTaskBtn = document.querySelector(".add-task-btn");
const addTaskForm = document.querySelector(".add-task")
const blackBackDrop = document.querySelector(".black-backdrop");
const toggleAddTaskForm = () => {
addTaskForm.classList.toggle("active");
blackBackDrop.classList.toggle("active");
addTaskBtn.classList.toggle("active");
}
addTaskBtn.addEventListener("click", toggleAddTaskForm);
blackBackDrop.addEventListener("click", toggleAddTaskForm);
//adding categories and tasks
let categories = [
{
title: "Personal",
img: "boy.png",
},
{
title: "Work",
img: "briefcase.png",
},
{
title: "Shopping",
img: "shopping.png"
},
{
title: "Health",
img: "healthcare.png",
},
{
title: "Fitness",
img: "dumbell.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,
},
// can add more if we want
];
let selectedCategory = categories[0];
const categoriesContainer = document.querySelector(".categories");
const categoryTitle = document.querySelector(".category-title");
const totalCategoryTasks = document.querySelector(".category-tasks");
const categoryImg = document.querySelector("#category-img");
const totalTasks = document.querySelector(".total-tasks");
const calculateTotal = () => {
const categoryTasks = tasks.filter(
(task) => task.category.toLowerCase() === selectedCategory.title.toLowerCase()
);
totalCategoryTasks.innerHTML= `${categoryTasks.length} Tasks`;
totalTasks.innerHTML = tasks.length;
};
const renderCategories = () => {
categoriesContainer.innerHTML = "";
categories.forEach((category) => {
//get all tasks of current category
const categoryTasks = tasks.filter((task) => task.category.toLowerCase() === category.title.toLowerCase()
);
//create a div to render category
const div = document.createElement("div");
div.classList.add("category")
div.addEventListener("click", ()=>{
wrapper.classList.add("show-category");
selectedCategory = category;
categoryTitle.innerHTML = category.title;
categoryImg.src = `images/${category.img}`;
calculateTotal();
//re render tasks when category changes
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 tasksContainer = document.querySelector(".tasks")
const renderTasks = () => {
tasksContainer.innerHTML = "";
const categoryTasks = tasks.filter(
(task) => task.category.toLowerCase() === selectedCategory.title.toLowerCase()
);
//if no task for selected category
if(categoryTasks.length === 0){
tasksContainer.innerHTML = `
<p> class = "no-task">No tasks for this category</p>
`;
}else{
//if there are tasks for selected category the render them lmao
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;
//add completion functionality on clicking the checkbox
checkbox.addEventListener("change", ()=>{
const index = tasks.findIndex((t)=>t.id===task.id);
//change the true or false or vice versa
tasks[index].completed = !tasks[index].completed;
//save in local storage
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"
/>
</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);
//deleting tasks function
const deleteBtn = div.querySelector(".delete");
deleteBtn.addEventListener("click", () => {
const index = tasks.findIndex((t) => t.id === task.id);
tasks.splice(index, 1);
saveLocal();
renderTasks();
});
renderCategories();
calculateTotal();
});
}
};
//save and get tasks from local storage
const saveLocal = () => {
localStorage.setItem("tasks", JSON.stringify(tasks));
};
const getLocal = () => {
const localTasks = JSON.parse(localStorage.getItem("tasks"));
//if tasks found
if(localTasks) {
tasks = localTasks;
}
};
//add task button function
//render all categories in select
const categorySelect = document.getElementById("category-select");
const cancelBtn = document.querySelector(".cancel-btn");
const addBtn = document.querySelector(".add-btn");
const taskInput = document.querySelector("#task-input");
cancelBtn.addEventListener("click", toggleAddTaskForm);
addBtn.addEventListener("click", () => {
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,
};
tasks.push(newTask);
taskInput.value = "";
saveLocal();
toggleAddTaskForm();
renderTasks();
}
});
categories.forEach((category) => {
const option = document.createElement("option");
option.value = category.title.toLowerCase();
option.textContent = category.title;
categorySelect.appendChild(option);
});
//these are already stored tasks
getLocal();
calculateTotal();
renderCategories();
renderTasks();