-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
81 lines (72 loc) · 3.07 KB
/
main.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
const todoForm = document.querySelector('form');
const todoInput = document.getElementById('todo-input');
const todoListUL = document.getElementById('todo-list');
let allTodos = getTodos();
updateTodoList();
todoForm.addEventListener('submit', function(e) {
e.preventDefault();
addTodos();
})
function addTodos() {
const todoText = todoInput.value.trim();
if(todoText.length > 0) {
const todoObject = { text: todoText, completed: false }
allTodos.push(todoObject);
updateTodoList();
saveTodos();
todoInput.value = "";
}
}
function updateTodoList() {
todoListUL.innerHTML = "";
allTodos.forEach((todo, todoIndex)=> {
todoItem = createTodoItem(todo, todoIndex);
todoListUL.append(todoItem);
})
}
function createTodoItem(todo, todoIndex) {
const todoId = `todo-${todoIndex}`;
const todoLI = document.createElement('li');
const todoText = todo.text;
todoLI.className = "todo";
todoLI.innerHTML = `
<input type="checkbox" id="${todoId}">
<label class="custom-checkbox" for="${todoId}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="transparent" width="24" height="24" class="size-6">
<path fill-rule="evenodd" d="M19.916 4.626a.75.75 0 0 1 .208 1.04l-9 13.5a.75.75 0 0 1-1.154.114l-6-6a.75.75 0 0 1 1.06-1.06l5.353 5.353 8.493-12.74a.75.75 0 0 1 1.04-.207Z" clip-rule="evenodd" />
</svg>
</label>
<label for="${todoId}" class="todo-text">
${todoText}
</label>
<button class="del-btn">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="#4A4D57" height="24" width="24" class="size-6">
<path fill-rule="evenodd" d="M16.5 4.478v.227a48.816 48.816 0 0 1 3.878.512.75.75 0 1 1-.256 1.478l-.209-.035-1.005 13.07a3 3 0 0 1-2.991 2.77H8.084a3 3 0 0 1-2.991-2.77L4.087 6.66l-.209.035a.75.75 0 0 1-.256-1.478A48.567 48.567 0 0 1 7.5 4.705v-.227c0-1.564 1.213-2.9 2.816-2.951a52.662 52.662 0 0 1 3.369 0c1.603.051 2.815 1.387 2.815 2.951Zm-6.136-1.452a51.196 51.196 0 0 1 3.273 0C14.39 3.05 15 3.684 15 4.478v.113a49.488 49.488 0 0 0-6 0v-.113c0-.794.609-1.428 1.364-1.452Zm-.355 5.945a.75.75 0 1 0-1.5.058l.347 9a.75.75 0 1 0 1.499-.058l-.346-9Zm5.48.058a.75.75 0 1 0-1.498-.058l-.347 9a.75.75 0 0 0 1.5.058l.345-9Z" clip-rule="evenodd" />
</svg>
</button>
`
const delBtn = todoLI.querySelector('.del-btn');
delBtn.addEventListener('click', ()=> {
deleteTodoItem(todoIndex);
})
const checkbox = todoLI.querySelector("input");
checkbox.addEventListener("change", ()=> {
allTodos[todoIndex].completed = checkbox.checked;
saveTodos();
})
checkbox.checked = todo.completed;
return todoLI;
}
function deleteTodoItem(todoIndex) {
allTodos = allTodos.filter((_, i)=> i !== todoIndex);
saveTodos();
updateTodoList();
}
function saveTodos() {
const todosJson = JSON.stringify(allTodos);
localStorage.setItem("todos", todosJson);
}
function getTodos() {
const todos = localStorage.getItem("todos") || "[]";
return JSON.parse(todos);
}