-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.ts
183 lines (149 loc) · 4.77 KB
/
todo.ts
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
let todos = getSavedTodos();
type Todo = {
id: string;
text: string;
completed: boolean;
};
type Filters = {
searchText: string;
hideCompleted: boolean;
};
const filters: Filters = {
searchText: "",
hideCompleted: false,
};
renderTodos(todos, filters);
const searchTextInput =
document.querySelector<HTMLInputElement>("#search-text");
if (searchTextInput) {
searchTextInput.addEventListener("input", (e: Event) => {
filters.searchText = (e.target as HTMLInputElement).value;
renderTodos(todos, filters);
});
}
const newTodoForm = document.querySelector<HTMLFormElement>("#new-todo");
if (newTodoForm) {
newTodoForm.addEventListener("submit", (e: Event) => {
e.preventDefault();
const id = generateUniqueId();
if (e.target instanceof HTMLFormElement) {
const textInput = e.target.elements.namedItem("text") as HTMLInputElement;
if (textInput) {
todos.push({
id: id,
text: textInput.value,
completed: false,
});
saveTodos(todos);
renderTodos(todos, filters);
textInput.value = "";
}
}
});
}
const hideCompleted =
document.querySelector<HTMLInputElement>("#hide-completed");
if (hideCompleted) {
hideCompleted.addEventListener("change", (e: Event) => {
const inputElement = e.target as HTMLInputElement;
if (inputElement) {
filters.hideCompleted = inputElement.checked;
renderTodos(todos, filters);
}
});
}
// Fetch existing todos from localStorage
function getSavedTodos() {
const todosJSON = localStorage.getItem("todos");
try {
return todosJSON ? JSON.parse(todosJSON) : [];
} catch (e) {
return [];
}
}
// Save todos to localStorage
function saveTodos(todos: Todo[]): void {
localStorage.setItem("todos", JSON.stringify(todos));
}
//Remove todo from the list
function removeTodo(id: string) {
const todoIndex = todos.findIndex((todo: Todo) => todo.id === id);
if (todoIndex > -1) {
todos.splice(todoIndex, 1);
}
}
// Render application todos based on filters
function renderTodos(todos: Todo[], filters: Filters) {
const filteredTodos = todos.filter((todo) => {
const searchTextMatch = todo.text
.toLowerCase()
.includes(filters.searchText.toLowerCase());
const hideCompletedMatch = !filters.hideCompleted || !todo.completed;
return searchTextMatch && hideCompletedMatch;
});
const incompleteTodos = filteredTodos.filter((todo) => !todo.completed);
const todosContainer = document.querySelector<HTMLDivElement>("#todos");
if (todosContainer) {
todosContainer.innerHTML = "";
todosContainer.appendChild(generateSummaryDOM(incompleteTodos));
filteredTodos.forEach((todo) =>
todosContainer.appendChild(generateTodoDOM(todo))
);
}
}
function toggleTodo(id: string) {
const todo = todos.find((todo: Todo) => todo.id === id);
if (todo) {
todo.completed = !todo.completed;
}
}
// Get the DOM elements for an individual note
function generateTodoDOM(todo: Todo) {
const todoEl = document.createElement("div");
todoEl.classList.add("todo-app__item");
const checkbox = document.createElement("input");
checkbox.classList.add("todo-app__item-status");
const checkboxContainer = document.createElement("label");
checkboxContainer.classList.add("todo-app__custom-container");
const customCheckbox = document.createElement("span");
customCheckbox.classList.add("todo-app__custom-checkbox");
const todoText = document.createElement("p");
todoText.classList.add("todo-app__item-link");
const removeButton = document.createElement("button");
removeButton.classList.add("todo-app__button-remove");
// Setup todo checkbox
checkbox.setAttribute("type", "checkbox");
checkbox.checked = todo.completed;
checkboxContainer.appendChild(checkbox);
todoEl.appendChild(checkboxContainer);
checkboxContainer.appendChild(customCheckbox);
checkbox.addEventListener("change", () => {
toggleTodo(todo.id);
saveTodos(todos);
renderTodos(todos, filters);
});
// Setup the todo text
todoText.textContent = todo.text;
checkboxContainer.appendChild(todoText);
// Setup the remove button
todoEl.appendChild(removeButton);
removeButton.addEventListener("click", () => {
removeTodo(todo.id);
saveTodos(todos);
renderTodos(todos, filters);
});
return todoEl;
}
// Get the DOM elements for list summary
function generateSummaryDOM(incompleteTodos: Todo[]) {
const summary = document.createElement("h2");
summary.classList.add("todo-app__summary");
summary.textContent = `You have ${incompleteTodos.length} todos left`;
return summary;
}
function generateUniqueId() {
const randomHex = Math.random().toString(16).slice(2);
const timestampHex = Date.now().toString(16);
const uniqueId = randomHex + timestampHex;
return uniqueId;
}