-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
149 lines (131 loc) · 4.35 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const grocWarn = document.querySelector('.hide-groc-warn'),
quantWarn = document.querySelector('.hide-quant-warn'),
grocBox = document.querySelector('.groc-input-box'),
quantBox = document.querySelector('.quant-input-box'),
addBtn = document.querySelector('.add-btn'),
reminder = document.querySelector('.reminder'),
addItem = document.querySelector('.list-of-items'),
info = document.querySelector('.info'),
done = document.querySelector('.done'),
addCart = document.querySelector('.list-of-cart'),
deleteBtn = document.querySelector('.delete-btn'),
clearAll = document.querySelector('.delete-all')
listDisplay = document.querySelector('.list-display');
setupPage();
function setupPage() {
const exist = localStorage.getItem('itemsList');
if (exist) {
const savedGrocery = JSON.parse(localStorage.getItem('itemsList'));
savedGrocery.forEach((item) => {
addToList(item);
})
}
}
function removeOldItems() {
addItem.innerHTML = '';
addCart.innerHTML = '';
}
//add button event
addBtn.addEventListener('click', inputField);
function inputField() {
const grocery = grocBox.value;
const quantity = quantBox.value;
const value = `${grocery} (${quantity})`;
grocBox.value = '';
quantBox.value = '';
if (grocery === '') {
groceryWarn();
}
if (quantity === '') {
quantityWarn();
}
else {
grocWarn.classList.add('hide-groc-warn');
quantWarn.classList.add('hide-quant-warn');
const item = createItem(value, 'groceryItem');
addToList(item);
saveToLocalStorage(item);
}
}
//warnings if invalid input
function groceryWarn() {
grocWarn.classList.remove('hide-groc-warn');
}
function quantityWarn() {
quantWarn.classList.remove('hide-quant-warn');
}
function createItem(value, listItem) {
return {
title: listItem,
grocery: value,
id: Date.now()
}
}
//add items to grocery list
function addToList(item) {
const listElements = document.createElement('li');
listElements.classList.add('grocery-list-item');
if (item.title === 'groceryItem') {
listElements.innerHTML = `<p id='${item.id}'>${item.grocery}</p>
<span class="material-symbols-outlined done">
done_outline
</span>`;
addItem.appendChild(listElements);
}
else if (item.title === 'cartItem') {
listElements.innerHTML = `<p id='${item.id}'>${item.grocery}</p>
<span class="material-symbols-outlined delete-btn">delete</span> `;
addCart.appendChild(listElements);
}
}
//add event listner to UL of grocery list
addItem.addEventListener('click', function (e) {
const element = e.target;
if (element.classList[1] === 'done') {
grocId = Number(element.parentElement.children[0].id);
element.parentElement.remove();
const inCart = updateTitleStorage(grocId, 'cartItem');
addToList(inCart);
}
})
//add event listener to ul of cart list
addCart.addEventListener('click', function (e) {
const targetElement = e.target;
if (targetElement.classList[1] === 'delete-btn') {
const grocId = targetElement.parentElement.children[0].id;
targetElement.parentElement.remove();
removeFromStorage(grocId);
}
})
function removeFromStorage(itemId) {
const savedItems = JSON.parse(localStorage.getItem('itemsList'));
const newItemsList = savedItems.filter((item) => {
return item.id != itemId;
});
localStorage.setItem('itemsList', JSON.stringify(newItemsList));
}
function updateTitleStorage(itemId, newTitle) {
let updatedItem;
const savedGroc = JSON.parse(localStorage.getItem('itemsList'));
savedGroc.forEach((item) => {
if (item.id == itemId) {
item.title = newTitle;
updatedItem = item;
}
});
localStorage.setItem('itemsList', JSON.stringify(savedGroc));
return updatedItem;
}
function saveToLocalStorage(item) {
const savedItems = JSON.parse(localStorage.getItem('itemsList')) || [];
savedItems.push(item);
localStorage.setItem('itemsList', JSON.stringify(savedItems));
}
//delete all items event listener
clearAll.addEventListener('click', function () {
const canDelete = confirm('are you sure you want to delete grocery');
if (canDelete) {
localStorage.clear();
removeOldItems();
}
})