-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.js
More file actions
75 lines (65 loc) · 1.83 KB
/
task.js
File metadata and controls
75 lines (65 loc) · 1.83 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
export class Task {
constructor(name, description, isCompleted = false, id = Date.now()) {
this.id = id; // allow for loading existing tasks with specific IDs
this.name = name;
this.description = description;
this.isCompleted = isCompleted;
}
toggle() {
this.isCompleted = !this.isCompleted;
}
update(name, desc) {
if (!name && !desc) {
console.log("Enter name or description");
return;
}
if (name) {
this.name = name;
}
if (desc) {
this.description = desc;
}
}
}
export class TaskManager {
constructor() {
// Load tasks from localStorage (or set to an empty array if none exists)
const storedTasks = JSON.parse(localStorage.getItem("tasks")) || [];
this.tasks = storedTasks.map(
(task) => new Task(task.name, task.description, task.isCompleted, task.id)
);
}
addTask(name, description, isCompleted = false) {
let newTask = new Task(name, description, isCompleted);
this.tasks.push(newTask);
this.saveTasks(); // Save tasks to local storage
}
deleteTask(id) {
this.tasks = this.tasks.filter((task) => task.id !== id);
this.saveTasks(); // Save updated tasks to local storage
}
updateTask(id, name, desc) {
let task = this.tasks.find((task) => task.id === id);
if (task) {
task.update(name, desc);
this.saveTasks(); // Save updated tasks to local storage
}
}
toggleTaskCompletion(id) {
const task = this.tasks.find((task) => task.id === id);
if (task) {
task.toggle();
this.saveTasks(); // Save updated tasks to local storage
}
}
getAllTask() {
return this.tasks;
}
getTaskById(id) {
return this.tasks.find((task) => task.id === id);
}
saveTasks() {
// Save the tasks to local storage
localStorage.setItem("tasks", JSON.stringify(this.tasks));
}
}