-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (64 loc) · 2.13 KB
/
index.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
const appendBtn = document.getElementById('add-button');
const taskInput = document.getElementById('task-description');
const tasksWrapper = document.querySelector('.tasks-wrapper');
let tasks;
!localStorage.tasks ? tasks = [] : tasks = JSON.parse(localStorage.getItem('tasks'));
let taskItems = [];
function Task(description) {
this.description = description;
this.completed = false;
};
const createTemplate = (task, index) => {
return `
<div class="task-item ${task.completed ? 'checked' : ''}">
<div class="description">${task.description}</div>
<div class="buttons">
<input onclick="finishTask(${index})" class="finish-btn" type="checkbox" ${task.completed ? 'checked' : ''}>
<button onclick="deleteTask(${index})" class="delete-btn">Delete</button>
</div>
</div>
`
};
const filterTasks = () => {
const activeTasks = tasks.length && tasks.filter(item => item.completed == false);
const finishedTasks = tasks.length && tasks.filter(item => item.completed == true);
tasks = [...activeTasks,...finishedTasks];
}
const fillHtmlList = () => {
tasksWrapper.innerHTML = "";
if (tasks.length > 0) {
filterTasks();
tasks.forEach((item, index) => {
tasksWrapper.innerHTML += createTemplate(item, index);
});
taskItems = document.querySelectorAll('.task-item');
}
};
fillHtmlList();
const updateLocal = () => {
localStorage.setItem('tasks', JSON.stringify(tasks));
};
const finishTask = (index) => {
tasks[index].completed = !tasks[index].completed;
if (tasks[index].completed) {
taskItems[index].classList.add('checked');
} else {
taskItems[index].classList.remove('checked');
}
updateLocal();
fillHtmlList();
};
appendBtn.addEventListener('click', () => {
tasks.push(new Task(taskInput.value));
updateLocal();
fillHtmlList();
taskInput.value = '';
});
const deleteTask = (index) => {
taskItems[index].classList.add('delition');
setTimeout(() => {
tasks.splice(index, 1);
updateLocal();
fillHtmlList();
}, 500);
};