This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto-do-list.js
123 lines (91 loc) · 3.26 KB
/
to-do-list.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
const toDoLists = JSON.parse(localStorage.getItem("toDoLists")) ?? [];
const toDoListContainerDiv = document.querySelector("div.js-to-do-lists-grid");
renderToDoLists();
const addToDoListButton = document.querySelector(
"button.js-create-to-do-list-button",
);
addToDoListButton.addEventListener("click", () => {
toDoLists.push([]);
renderToDoLists();
});
function renderToDoLists() {
localStorage.setItem("toDoLists", JSON.stringify(toDoLists));
toDoListContainerDiv.innerHTML = "";
toDoLists.forEach((toDoList, index) => {
toDoListContainerDiv.innerHTML += `
<div class="to-do-list">
<div class="close-to-do-list-icon-wrapper">
<div class="close-to-do-list-tooltip">Delete To-Do List</div>
<img src="images/close-icon.png" alt="Close Icon" class="close-to-do-list-icon js-close-to-do-list-icon" />
</div>
<div class="to-do-list-input-grid">
<input class="name-input js-name-input-${index}" placeholder="To-Do Name"/>
<input class="date-input js-date-input-${index}" type="date" />
<button class="create-to-do-button js-create-to-do-button">Create</button>
</div>
<div class="to-do-list-item-grid">
${toDoList
.map(
// You would expect "checked='false'" to work here, but apparently it doesn't.
(item) => `
<div class="to-do-list-item-name">${item.name}</div>
<div class="to-do-list-item-date">${item.date}</div>
<input type="checkbox" ${item.completed ? "checked='true'" : ""
} class="js-completed-${index}" />
<button class="delete-to-do-list-item-button js-delete-to-do-list-item-button-${index}">Delete</button>
`,
)
.join("")}
</div>
</div>
`;
});
const closeButtons = document.querySelectorAll(
"img.js-close-to-do-list-icon",
);
closeButtons.forEach((closeButton, index) =>
closeButton.addEventListener("click", () => {
toDoLists.splice(index, 1);
renderToDoLists();
}),
);
// The buttons' events have to be registered separately to avoid the event listeners not working.
toDoLists.forEach((_toDoList, index) => {
const deleteItemButtons = document.querySelectorAll(
`button.js-delete-to-do-list-item-button-${index}`,
);
deleteItemButtons.forEach((button, buttonIndex) =>
button.addEventListener("click", () => {
toDoLists[index].splice(buttonIndex, 1);
renderToDoLists();
}),
);
const createElementItemButtons = document.querySelectorAll(
"button.js-create-to-do-button",
);
createElementItemButtons.forEach((createElementButton) =>
createElementButton.addEventListener("click", () => {
const nameInput = document.querySelector(
`input.js-name-input-${index}`,
);
const dateInput = document.querySelector(
`input.js-date-input-${index}`,
);
if (nameInput.value && dateInput.value) {
toDoLists[index].push({ name: nameInput.value, date: dateInput.value });
renderToDoLists();
}
}),
);
const completedElements = document.querySelectorAll(
`input.js-completed-${index}`,
);
completedElements.forEach((element, elementIndex) =>
element.addEventListener("click", () => {
toDoLists[index][elementIndex].completed =
!toDoLists[index][elementIndex].completed;
renderToDoLists();
}),
);
});
}