-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
74 lines (65 loc) · 2.02 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
function getItems(){
db.collection("todo-items").onSnapshot((snapshot) => {
let items = [];
snapshot.docs.forEach((doc) => {
items.push({
id: doc.id,
...doc.data()
})
})
generateItems(items);
})
}
function generateItems(items){
let todoItems = []
items.forEach((item) => {
let todoItem = document.createElement("div");
todoItem.classList.add("todo-item");
let checkContainer = document.createElement("div");
checkContainer.classList.add("check");
let checkMark = document.createElement("div");
checkMark.classList.add("check-mark");
checkMark.innerHTML = '<img src="assets/icon-check.svg">';
checkMark.addEventListener("click", function(){
markCompleted(item.id);
})
checkContainer.appendChild(checkMark);
let todoText = document.createElement("div");
todoText.classList.add("todo-text");
todoText.innerText = item.text;
if(item.status == "completed"){
checkMark.classList.add("checked");
todoText.classList.add("checked");
}
todoItem.appendChild(checkContainer);
todoItem.appendChild(todoText);
todoItems.push(todoItem)
})
document.querySelector(".todo-items").replaceChildren(...todoItems);
}
function addItem(event){
event.preventDefault();
let text = document.getElementById("todo-input");
let newItem = db.collection("todo-items").add({
text: text.value,
status: "active"
})
text.value = "";
}
function markCompleted(id){
let item = db.collection("todo-items").doc(id);
item.get().then(function(doc) {
if (doc.exists) {
if(doc.data().status == "active"){
item.update({
status: "completed"
})
} else {
item.update({
status: "active"
})
}
}
})
}
getItems();