Skip to content

Commit 69d7cfe

Browse files
committed
14-todo complete
1 parent 850e8d8 commit 69d7cfe

File tree

1,652 files changed

+130709
-44
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,652 files changed

+130709
-44
lines changed

14-todo/final/app.js

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// select items
1+
// ****** select items **********
2+
23
const form = document.querySelector(".grocery-form");
34
const alert = document.querySelector(".alert");
45
const grocery = document.getElementById("grocery");
@@ -9,16 +10,23 @@ const clearBtn = document.querySelector(".clear-btn");
910
// edit option
1011
let editElement;
1112
let editFlag = false;
13+
14+
// ****** event listeners **********
15+
1216
// submit form
1317
form.addEventListener("submit", addItem);
1418
// clear list
1519
clearBtn.addEventListener("click", clearItems);
20+
// display items onload
21+
window.addEventListener("DOMContentLoaded", setupItems);
22+
23+
// ****** functions **********
1624

1725
// add item
1826
function addItem(e) {
1927
e.preventDefault();
2028
const value = grocery.value;
21-
if (value !== "" && editFlag === false) {
29+
if (value !== "" && !editFlag) {
2230
const element = document.createElement("article");
2331
element.classList.add("grocery-item");
2432
element.innerHTML = `<p class="title">${value}</p>
@@ -48,11 +56,13 @@ function addItem(e) {
4856
// show container
4957
container.classList.add("show-container");
5058
// set local storage
51-
setLocalStorage();
59+
addToLocalStorage(value);
5260
} else if (value !== "" && editFlag) {
5361
editElement.innerHTML = value;
5462
displayAlert("value changed", "success");
5563
setBackToDefault();
64+
// add to local storage
65+
addToLocalStorage(value);
5666
} else {
5767
displayAlert("please enter value", "danger");
5868
}
@@ -79,6 +89,7 @@ function clearItems() {
7989
container.classList.remove("show-container");
8090
displayAlert("empty list", "danger");
8191
setBackToDefault();
92+
localStorage.removeItem("list");
8293
}
8394

8495
// delete item
@@ -92,6 +103,8 @@ function deleteItem(e) {
92103
displayAlert("item removed", "danger");
93104

94105
setBackToDefault();
106+
// remove from local storage
107+
removeFromLocalStorage(element.firstElementChild.textContent);
95108
}
96109
// edit item
97110
function editItem(e) {
@@ -102,15 +115,74 @@ function editItem(e) {
102115
editFlag = true;
103116
//
104117
submitBtn.textContent = "edit";
118+
// remove from local storage
119+
removeFromLocalStorage(editElement.innerHTML);
105120
}
106-
// set backt to default
121+
// set backt to defaults
107122
function setBackToDefault() {
108123
grocery.value = "";
109124
editFlag = false;
110125
submitBtn.textContent = "submit";
111126
}
112127

113-
function setLocalStorage() {
114-
const items = document.querySelectorAll(".grocery-item");
128+
// ****** local storage **********
129+
130+
// add to local storage
131+
function addToLocalStorage(grocery) {
132+
let items = getLocalStorage();
133+
items.push(grocery);
115134
localStorage.setItem("list", JSON.stringify(items));
116135
}
136+
137+
function getLocalStorage() {
138+
return localStorage.getItem("list")
139+
? JSON.parse(localStorage.getItem("list"))
140+
: [];
141+
}
142+
143+
function removeFromLocalStorage(grocery) {
144+
let items = getLocalStorage();
145+
items = items.filter(function (item) {
146+
return item !== grocery;
147+
});
148+
localStorage.setItem("list", JSON.stringify(items));
149+
}
150+
151+
// SETUP LOCALSTORAGE.REMOVEITEM('LIST');
152+
153+
// ****** setup items **********
154+
155+
function setupItems() {
156+
let items = getLocalStorage();
157+
if (items.length > 0) {
158+
items.forEach(function (item) {
159+
createListItem(item);
160+
});
161+
container.classList.add("show-container");
162+
}
163+
}
164+
165+
function createListItem(value) {
166+
const element = document.createElement("article");
167+
element.classList.add("grocery-item");
168+
element.innerHTML = `<p class="title">${value}</p>
169+
<div class="btn-container">
170+
<!-- edit btn -->
171+
<button type="button" class="edit-btn">
172+
<i class="fas fa-edit"></i>
173+
</button>
174+
<!-- delete btn -->
175+
<button type="button" class="delete-btn">
176+
<i class="fas fa-trash"></i>
177+
</button>
178+
</div>
179+
`;
180+
// add event listeners to both buttons;
181+
const deleteBtn = element.querySelector(".delete-btn");
182+
deleteBtn.addEventListener("click", deleteItem);
183+
const editBtn = element.querySelector(".edit-btn");
184+
editBtn.addEventListener("click", editItem);
185+
186+
// append child
187+
list.appendChild(element);
188+
}

14-todo/final/index.html

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<title>Grocery List</title>
6+
<title>Grocery Bud</title>
77
<!-- font awesome -->
88
<link
99
rel="stylesheet"
@@ -25,26 +25,23 @@ <h3>grocery bud</h3>
2525
</form>
2626
<!-- list -->
2727
<div class="grocery-container">
28-
<div class="grocery-list"></div>
28+
<div class="grocery-list">
29+
<!-- <article class="grocery-item">
30+
<p class="title">item</p>
31+
<div class="btn-container">
32+
<button type="button" class="edit-btn">
33+
<i class="fas fa-edit"></i>
34+
</button>
35+
<button type="button" class="delete-btn">
36+
<i class="fas fa-trash"></i>
37+
</button>
38+
</div>
39+
</article> -->
40+
</div>
2941
<button class="clear-btn">clear items</button>
3042
</div>
3143
</section>
3244
<!-- javascript -->
3345
<script src="app.js"></script>
3446
</body>
3547
</html>
36-
<!-- single item -->
37-
<!-- <article class="grocery-item">
38-
<p class="title">item</p>
39-
<div class="btn-container"> -->
40-
<!-- edit btn -->
41-
<!-- <button type="button" class="edit-btn">
42-
<i class="fas fa-edit"></i>
43-
</button> -->
44-
<!-- delete btn -->
45-
<!-- <button type="button" class="delete-btn">
46-
<i class="fas fa-trash"></i>
47-
</button>
48-
</div>
49-
</article> -->
50-
<!-- end of single item -->

14-todo/final/styles.css

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ p {
163163
.section-center {
164164
width: 90vw;
165165
margin: 0 auto;
166-
max-width: 40rem;
166+
max-width: 35rem;
167167
margin-top: 8rem;
168168
}
169169
@media screen and (min-width: 992px) {
@@ -191,6 +191,25 @@ Grocery List
191191
.section-center:hover {
192192
box-shadow: var(--dark-shadow);
193193
}
194+
.alert {
195+
margin-bottom: 1rem;
196+
height: 1.25rem;
197+
display: grid;
198+
align-items: center;
199+
text-align: center;
200+
font-size: 0.7rem;
201+
border-radius: 0.25rem;
202+
letter-spacing: var(--spacing);
203+
text-transform: capitalize;
204+
}
205+
.alert-danger {
206+
color: #721c24;
207+
background: #f8d7da;
208+
}
209+
.alert-success {
210+
color: #155724;
211+
background: #d4edda;
212+
}
194213
.grocery-form h3 {
195214
color: var(--clr-primary-1);
196215
margin-bottom: 1.5rem;
@@ -229,31 +248,13 @@ Grocery List
229248
cursor: pointer;
230249
content: var(--clr-primary-5);
231250
transition: var(--transition);
232-
font-size: 1rem;
251+
font-size: 0.85rem;
233252
}
234253
.submit-btn:hover {
235254
background: var(--clr-primary-5);
236255
color: var(--clr-white);
237256
}
238-
.alert {
239-
margin-bottom: 1rem;
240-
height: 1.25rem;
241-
display: grid;
242-
align-items: center;
243-
text-align: center;
244-
font-size: 0.7rem;
245-
border-radius: 0.25rem;
246-
letter-spacing: var(--spacing);
247-
text-transform: capitalize;
248-
}
249-
.alert-danger {
250-
color: #721c24;
251-
background: #f8d7da;
252-
}
253-
.alert-success {
254-
color: #155724;
255-
background: #d4edda;
256-
}
257+
257258
.grocery-container {
258259
margin-top: 2rem;
259260
transition: var(--transition);

14-todo/setup/app.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// ****** SELECT ITEMS **********
2+
3+
// edit option
4+
5+
// ****** EVENT LISTENERS **********
6+
7+
// ****** FUNCTIONS **********
8+
9+
// ****** LOCAL STORAGE **********
10+
11+
// ****** SETUP ITEMS **********
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Font Awesome Free License
2+
-------------------------
3+
4+
Font Awesome Free is free, open source, and GPL friendly. You can use it for
5+
commercial projects, open source projects, or really almost whatever you want.
6+
Full Font Awesome Free license: https://fontawesome.com/license/free.
7+
8+
# Icons: CC BY 4.0 License (https://creativecommons.org/licenses/by/4.0/)
9+
In the Font Awesome Free download, the CC BY 4.0 license applies to all icons
10+
packaged as SVG and JS file types.
11+
12+
# Fonts: SIL OFL 1.1 License (https://scripts.sil.org/OFL)
13+
In the Font Awesome Free download, the SIL OFL license applies to all icons
14+
packaged as web and desktop font files.
15+
16+
# Code: MIT License (https://opensource.org/licenses/MIT)
17+
In the Font Awesome Free download, the MIT license applies to all non-font and
18+
non-icon files.
19+
20+
# Attribution
21+
Attribution is required by MIT, SIL OFL, and CC BY licenses. Downloaded Font
22+
Awesome Free files already contain embedded comments with sufficient
23+
attribution, so you shouldn't need to do anything additional when using these
24+
files normally.
25+
26+
We've kept attribution comments terse, so we ask that you do not actively work
27+
to remove them from files, especially code. They're a great way for folks to
28+
learn about Font Awesome.
29+
30+
# Brand Icons
31+
All brand icons are trademarks of their respective owners. The use of these
32+
trademarks does not indicate endorsement of the trademark holder by Font
33+
Awesome, nor vice versa. **Please do not use brand logos for any purpose except
34+
to represent the company, product, or service to which they refer.**

0 commit comments

Comments
 (0)