-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
125 lines (98 loc) · 3.48 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
import { getDateRepresentation, saveTodosIntoLocalStorage, getTodosFromLocalStorage } from "./utils.js";
const addTodoInput = document.querySelector("[data-add-todo-input]");
const addTodoBtn = document.querySelector("[data-add-todo-btn]");
const searchTodoInput = document.querySelector("[data-search-todo-input]");
const todosContainer = document.querySelector("[data-todo-container]");
const todoTemplate = document.querySelector("[data-todo-template]");
let todoList = getTodosFromLocalStorage();
let filteredTodoList = [];
addTodoBtn.addEventListener("click", () => {
if (addTodoInput.value.trim()) {
const newTodo = {
id: Date.now(),
text: addTodoInput.value.trim(),
completed: false,
createdAt: getDateRepresentation(new Date()),
}
todoList.push(newTodo);
addTodoInput.value = "";
saveTodosIntoLocalStorage(todoList);
renderTodos();
}
})
addTodoInput.addEventListener("input", () => {
if (searchTodoInput.value.trim()) {
searchTodoInput.value = "";
renderTodos();
}
})
searchTodoInput.addEventListener("input", (e) => {
const searchValue = e.target.value.trim();
filterAndRenderFilteredTodos(searchValue);
})
const filterAndRenderFilteredTodos = (searchValue) => {
filteredTodoList = todoList.filter((t) => t.text.includes(searchValue));
renderFilteredTodos();
}
const createTodoLayout = (todo) => {
const todoElement = document.importNode(todoTemplate.content, true);
const checkbox = todoElement.querySelector("[data-todo-checkbox]");
checkbox.checked = todo.completed;
const todoText = todoElement.querySelector("[data-todo-text]");
todoText.textContent = todo.text;
const todoCreatedDate = todoElement.querySelector("[data-todo-date]");
todoCreatedDate.textContent = todo.createdAt;
const removeTodoBtn = todoElement.querySelector("[remove-todo-btn]");
removeTodoBtn.disabled = !todo.completed;
checkbox.addEventListener("change", (e) => {
todoList = todoList.map((t) => {
if (t.id === todo.id) {
t.completed = e.target.checked;
}
return t;
})
saveTodosIntoLocalStorage(todoList);
if (searchTodoInput.value.trim()) {
filterAndRenderFilteredTodos(searchTodoInput.value.trim());
} else {
renderTodos();
}
})
removeTodoBtn.addEventListener("click", () => {
todoList = todoList.filter((t) => {
if (t.id !== todo.id) {
return t;
}
})
saveTodosIntoLocalStorage(todoList);
if (searchTodoInput.value.trim()) {
filterAndRenderFilteredTodos(searchTodoInput.value.trim());
} else {
renderTodos();
}
});
return todoElement;
}
const renderTodos = () => {
todosContainer.innerHTML = "";
if (todoList.length === 0) {
todosContainer.innerHTML = `<h3>No todos...</h3>`;
return;
}
todoList.forEach((todo) => {
const todoElement = createTodoLayout(todo);
todosContainer.append(todoElement);
})
}
const renderFilteredTodos = () => {
todosContainer.innerHTML = "";
if (filteredTodoList.length === 0) {
todosContainer.innerHTML = `<h3>No todos found...</h3>`;
return;
}
filteredTodoList.forEach((todo) => {
const todoElement = createTodoLayout(todo);
todosContainer.append(todoElement);
})
}
renderTodos();