-
Notifications
You must be signed in to change notification settings - Fork 0
/
kanban.js
105 lines (89 loc) · 2.64 KB
/
kanban.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
export default class Kanban {
static getTasks(columnId) {
const data = read().find((column) => column.columnId === columnId);
if (!data) {
return [];
}
return data.tasks;
}
static insertTask(columnId, content) {
const data = read();
const column = data.find((column) => column.columnId === columnId);
const task = {
taskId: Math.floor(Math.random() * 100000),
content: content,
};
if (!column) {
throw new Error("Column doesn't exists !");
}
column.tasks.push(task);
save(data);
return task;
}
static updateTask(taskId, updatedInformation) {
const data = read();
function findColumnTask() {
for (const column of data) {
const task = column.tasks.find((item) => item.taskId === taskId);
if (task) {
return [task, column];
}
}
}
const [task, currentColumn] = findColumnTask();
const targetColumn = data.find(
(column) => column.columnId === updatedInformation.columnId
);
task.content = updatedInformation.content;
currentColumn.tasks.splice(currentColumn.tasks.indexOf(task), 1);
targetColumn.tasks.push(task);
save(data);
}
static deleteTask(taskId) {
const data = read();
for (const column of data) {
column.tasks = column.tasks.filter((item) => item.taskId !== taskId);
}
save(data);
}
static getAllTasks() {
const data = read();
columnCount();
return [data[0].tasks, data[1].tasks, data[2].tasks];
}
}
function read() {
const data = localStorage.getItem("data");
if (!data) {
return [
{ columnId: 0, tasks: [] },
{ columnId: 1, tasks: [] },
{ columnId: 2, tasks: [] },
];
}
return JSON.parse(data);
}
function save(data) {
localStorage.setItem("data", JSON.stringify(data));
columnCount();
}
function columnCount() {
const data = read();
const todo = document.querySelector("span.todo");
todo.textContent = data[0].tasks.length;
const pending = document.querySelector("span.pending");
pending.textContent = data[1].tasks.length;
const completed = document.querySelector("span.completed");
completed.textContent = data[2].tasks.length;
}
// console.log(Kanban.getAllTasks());
// console.log(Kanban.getTasks(1));
// console.log(Kanban.getTasks(1));
// console.log(Kanban.insertTask(0, "make react project"));
// console.log(Kanban.getTasks(1));
// console.log(Kanban.getAllTasks());
// Kanban.deleteTask(24564);
// console.log(Kanban.getAllTasks());
// console.log(Kanban.getAllTasks());
// // Kanban.updateTask(97522, { columnId: 1, content: "record javascript preview" });
// console.log(Kanban.getAllTasks());