-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (52 loc) · 1.84 KB
/
app.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
"use strict";
const addItems = document.querySelector(".add-items");
const itemsList = document.querySelector(".plates");
const resetBtn = document.querySelector(`button[type="reset"]`);
const items = JSON.parse(localStorage.getItem("items")) || [];
const addItem = function (e) {
e.preventDefault();
const text = this.querySelector(`input[name=item]`).value;
const item = {
text,
done: false,
};
items.push(item);
updateList(items, itemsList);
localStorage.setItem("items", JSON.stringify(items));
this.reset();
};
// get the mapped values of the items array, map it and return the plate value as a new list item of the parent UL
const updateList = function (plates = [], platesList) {
platesList.innerHTML = plates
.map((plate, i) => {
return `
<li>
<input type="checkbox" data-index="${i}" id="item${i}" ${
plate.done ? "checked" : ""
} />
<label for="item${i}" >${plate.text}</label for="items{i}">
</li>
`;
})
.join("");
};
const toggleDone = function (e) {
const el = e.target;
if (!el.matches("input")) return; //skip targets that aren't input
const index = el.dataset.index;
items[index].done = !items[index].done;
localStorage.setItem("items", JSON.stringify(items)); // set it to local storage
updateList(items, itemsList); //update the list
};
const reset = function () {
console.log("cleared in local storage");
localStorage.clear();
itemsList.innerHTML = `<li>Loading Bole...</li>`;
};
// Event Listeners
addItems.addEventListener("submit", addItem);
// NOTE: Event Delegation is simply giving responsibility to a tag or variable that can handle it, in the event where the main target can't.
itemsList.addEventListener("click", toggleDone);
resetBtn.addEventListener("click", reset);
// populate list upon page load
updateList(items, itemsList);