-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodos-functions.ts
128 lines (105 loc) · 3.57 KB
/
todos-functions.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
"use strict";
// Fetch existing todos from localStorage
export function getSavedTodos(): Todo[] {
const todosJSON = localStorage.getItem("todos");
try {
return todosJSON ? JSON.parse(todosJSON) : [];
} catch (e) {
return [];
}
}
// Save todos to localStorage
export function saveTodos(todos: Todo[]): void {
localStorage.setItem("todos", JSON.stringify(todos));
}
// Remove todo from the list
function removeTodo(id: number): void {
const todoIndex = todos.findIndex((todo) => todo.id === id);
if (todoIndex > -1) {
todos.splice(todoIndex, 1);
}
}
// Render application todos based on filters
export function renderTodos(todos: Todo[], filters: Filters): void {
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("#todos") as HTMLDivElement;
todosContainer.innerHTML = "";
todosContainer.appendChild(generateSummaryDOM(incompleteTodos));
filteredTodos.forEach((todo) =>
todosContainer.appendChild(generateTodoDOM(todo))
);
}
const toggleTodo = (id: number): void => {
const todo = todos.find((todo) => todo.id === id);
if (todo) {
todo.completed = !todo.completed;
}
};
// Define the Todo interface
export interface Todo {
id: number;
text: string;
completed: boolean;
}
// Define the Filters interface
export interface Filters {
searchText: string;
hideCompleted: boolean;
}
// Get the DOM elements for an individual note
const generateTodoDOM = (todo: Todo): HTMLDivElement => {
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
const generateSummaryDOM = (incompleteTodos: Todo[]): HTMLHeadingElement => {
const summary = document.createElement("h2");
summary.classList.add("todo-app__summary");
summary.textContent = `You have ${incompleteTodos.length} todos left`;
return summary;
};
// Mock data for todos and filters (you need to define these variables in your code)
const todos: Todo[] = [];
const filters: Filters = {
searchText: "",
hideCompleted: false,
};