Skip to content

Commit d2b1237

Browse files
committed
Added LocalStorage feature & Reload Functionality
1 parent dcc3736 commit d2b1237

File tree

1 file changed

+73
-33
lines changed

1 file changed

+73
-33
lines changed

todo-list-crud/app.js

+73-33
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,10 @@ form.addEventListener("submit", (event) => {
1919
const id = new Date().getTime().toString(); // to get unique value for id, date() can be used.
2020
if (toDoValue) {
2121
if (!editFlag) { // to add item to the list dynamically
22-
let element = document.createElement("article"); // creating new article to add to list
23-
element.classList.add("grocery-item"); // add class
24-
const attribute = document.createAttribute("data-id"); // creating attribute
25-
attribute.value = id;
26-
element.setAttributeNode(attribute); // set that attribute for the created element
27-
element.innerHTML = `<p class="title">${toDoValue}</p>
28-
<div class="btn-container">
29-
<button type="button" class="edit-btn">
30-
<i class="fas fa-edit"></i>
31-
</button>
32-
<button type="button" class="delete-btn">
33-
<i class="fas fa-trash"></i>
34-
</button>
35-
</div>`;
36-
list.appendChild(element); // add whole element to the toDo list
22+
createListItems(id, toDoValue);
3723
container.classList.add("show-container"); // since visibility is false
24+
addToLocalStorage(id, toDoValue); // CRUD
3825
setAlert("Item/Task added to the list.", "success");
39-
// edit and delete button elements
40-
const editBtn = element.querySelector(".edit-btn");
41-
editBtn.addEventListener("click", editItem);
42-
const deleteBtn = element.querySelector(".delete-btn");
43-
deleteBtn.addEventListener("click", deleteItem);
44-
// CRUD
45-
addToLocalStorage(id, toDoValue);
4626
setToDefault();
4727
}
4828
else { // to edit items in the list
@@ -67,7 +47,18 @@ clearBtn.addEventListener("click", () => {
6747
container.classList.remove("show-container");
6848
setAlert("Items have been removed from the list.", "danger");
6949
setToDefault(); // for edit functionality
70-
// localStorage.removeItem("list");
50+
localStorage.removeItem("list"); // removes all items from local storage
51+
});
52+
53+
// on page load / reload
54+
window.addEventListener("DOMContentLoaded", () => {
55+
let data = localStorage.getItem("list") ? JSON.parse(localStorage.getItem("list")) : [];
56+
if (data.length > 0) {
57+
for (item of data) {
58+
createListItems(item.id, item.value);
59+
}
60+
container.classList.add("show-container");
61+
}
7162
});
7263

7364
// ****** FUNCTIONS **********
@@ -107,24 +98,73 @@ deleteItem = (event) => {
10798

10899
// ****** LOCAL STORAGE **********
109100
addToLocalStorage = (id, value) => {
110-
// console.log("add to local storage", id);
101+
const toDo = { id, value };
102+
// if localStorage is empty, create empty array, otherwise assign existing data to array
103+
let data = localStorage.getItem("list") ? JSON.parse(localStorage.getItem("list")) : [];
104+
data.push(toDo);
105+
localStorage.setItem("list", JSON.stringify(data));
111106
}
112107
removeFromLocalStorage = (id) => {
113-
// console.log("removeFromLocalStorage", id);
108+
let data = localStorage.getItem("list") ? JSON.parse(localStorage.getItem("list")) : [];
109+
data = data.filter(d => d.id !== id); // filtering data that has to be removed.
110+
localStorage.setItem("list", JSON.stringify(data)); // keeping rest of the data in local storage
114111
}
115112
editLocalStorage = (id, value) => {
116-
// console.log("edited local storage", id);
113+
let data = localStorage.getItem("list") ? JSON.parse(localStorage.getItem("list")) : [];
114+
// map() method can be used to edit the specific value w.r.t. id
115+
data = data.map(d => {
116+
if (d.id === id) {
117+
d.value = value;
118+
}
119+
return d;
120+
});
121+
localStorage.setItem("list", JSON.stringify(data));
117122
}
118-
// ****** SETUP ITEMS **********
119-
120123

124+
// ****** SETUP ITEMS **********
125+
createListItems = (id, toDoValue) => {
126+
let element = document.createElement("article"); // creating new article to add to list
127+
element.classList.add("grocery-item"); // add class
128+
const attribute = document.createAttribute("data-id"); // creating attribute
129+
attribute.value = id;
130+
element.setAttributeNode(attribute); // set that attribute for the created element
131+
element.innerHTML = `<p class="title">${toDoValue}</p>
132+
<div class="btn-container">
133+
<button type="button" class="edit-btn">
134+
<i class="fas fa-edit"></i>
135+
</button>
136+
<button type="button" class="delete-btn">
137+
<i class="fas fa-trash"></i>
138+
</button>
139+
</div>`;
140+
list.appendChild(element); // add whole element to the toDo list
141+
// edit and delete button elements
142+
const editBtn = element.querySelector(".edit-btn");
143+
editBtn.addEventListener("click", editItem);
144+
const deleteBtn = element.querySelector(".delete-btn");
145+
deleteBtn.addEventListener("click", deleteItem);
146+
}
121147

122148
/*
123149
Learnings:
124150
1. changing content and classlist dynamically
125-
2. setTimeOut() method
126-
3. createElement, createAttribute, setAttributeNode
127-
4. appendChild(), removeChild()
128-
5. children property of array
129-
6. previousElementSibling
151+
2. setTimeOut() method calls a function or evaluates an expression after a specified number of milliseconds.
152+
153+
----- DOM ELEMENTS -----
154+
3. createElement() method creates an Element Node with the specified name.
155+
4. createAttribute() method creates an attribute with the specified name, and returns the attribute as an Attr object.
156+
5. setAttributeNode() method adds the specified attribute node to an element.
157+
6. appendChild() method appends a node as the last child of a node
158+
7. removeChild() method removes a specified child node of the specified element.
159+
8. children property returns a collection of an element's child elements, as an HTMLCollection object.
160+
9. previousElementSibling property returns the previous element of the specified element, in the same tree level.
161+
162+
----- LOCAL STORAGE -----
163+
10. The localStorage property allows to save key/value pairs in a web browser.
164+
11. localStorage.setItem() method is used to Add key and value to local storage,
165+
12. localStorage.getItem() method is used to Retrieve a value by the key from local storage,
166+
13. localStorage.removeItem() method is used to Remove an item by key from local storage.
167+
168+
14. JSON.stringify() method used is to convert a data array to a string since json takes strings only
169+
15. JSON.parse() method is used to to convert the content back into an object
130170
*/

0 commit comments

Comments
 (0)