-
Notifications
You must be signed in to change notification settings - Fork 1
/
taskManager.js
122 lines (93 loc) · 3.52 KB
/
taskManager.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
// Add a Delete button with the class delete-button
const createTaskHtml = (id, name, description, assignedTo, dueDate, status) => `
<br>
<li class="list-group-item task-style task-bg" data-task-id=${id}>
<div class="d-flex w-100 mt-2 justify-content-between align-items-center">
<h5>${name}</h5>
<span class="badge ${status === 'TODO' ? 'badge-danger' : 'badge-success'}">${status}</span>
</div>
<div class="d-flex w-100 mb-3 justify-content-between">
<small>Assigned To: ${assignedTo}</small>
<small>Due: ${dueDate}</small>
</div>
<p>${description}</p>
<div class="d-flex w-100 justify-content-end btns">
<button class="btn-background btn btn-outline-success done-button mr-1 ${status === 'TODO' ? 'visible' : 'invisible'}">Mark As Done</button>
<button class="btn-background btn btn-outline-danger delete-button">Delete</button>
</div>
</li>
<br>
`;
class TaskManager {
constructor(currentId = 0) {
this.tasks = [];
this.currentId = currentId;
}
addTask(name, description, assignedTo, dueDate) {
const task = {
id: this.currentId++,
name: name,
description: description,
assignedTo: assignedTo,
dueDate: dueDate,
status: 'TODO'
};
this.tasks.push(task);
}
// Create the deleteTask method
deleteTask(taskId) {
// Create an empty array and store it in a new variable, newTasks
const newTasks = [];
// Loop over the tasks
for (let i = 0; i < this.tasks.length; i++) {
// Get the current task in the loop
const task = this.tasks[i];
// Check if the task id is not the task id passed in as a parameter
if (task.id !== taskId) {
// Push the task to the newTasks array
newTasks.push(task);
}
}
// Set this.tasks to newTasks
this.tasks = newTasks;
}
getTaskById(taskId) {
let foundTask;
for (let i = 0; i < this.tasks.length; i++) {
const task = this.tasks[i];
if (task.id === taskId) {
foundTask = task;
}
}
return foundTask;
}
render() {
const tasksHtmlList = [];
for (let i = 0; i < this.tasks.length; i++) {
const task = this.tasks[i];
const date = new Date(task.dueDate);
const formattedDate = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
const taskHtml = createTaskHtml(task.id, task.name, task.description, task.assignedTo, formattedDate, task.status);
tasksHtmlList.push(taskHtml);
}
const tasksHtml = tasksHtmlList.join('\n');
const tasksList = document.querySelector('#tasksList');
tasksList.innerHTML = tasksHtml;
}
save() {
const tasksJson = JSON.stringify(this.tasks);
localStorage.setItem('tasks', tasksJson);
const currentId = String(this.currentId);
localStorage.setItem('currentId', currentId);
}
load() {
if (localStorage.getItem('tasks')) {
const tasksJson = localStorage.getItem('tasks');
this.tasks = JSON.parse(tasksJson);
}
if (localStorage.getItem('currentId')) {
const currentId = localStorage.getItem('currentId');
this.currentId = Number(currentId);
}
}
}