-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
213 lines (177 loc) · 6.1 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
let itemName = document.getElementById('itemName')
let metrics = document.getElementById('metrics')
let quantity = document.getElementById('quantity')
let section = document.getElementsByTagName('section')[0]
let addItemBtn = document.getElementById('addItemBtn')
let totalItems = document.getElementById('info')
let deleteAllBtn = document.getElementById('deleteAllBtn')
let itemsArray = (localStorage.getItem('items')) ? JSON.parse(localStorage.getItem('items')) : []
let grocerylist = document.getElementById('grocerylist')
import grocery_list from "./itemsList"
let emptyDiv = document.createElement('div')
let emptyTasks =()=>{
if(itemsArray.length === 0){
emptyDiv.innerHTML = 'No Items left, Add new Item!!!'
emptyDiv.style.fontSize = '2rem'
emptyDiv.style.marginTop = '2rem'
emptyDiv.style.textAlign = 'center'
section.appendChild(emptyDiv)
}else{
emptyDiv.style.display = 'none'
}
}
const micBtn = document.getElementById('micBtn');
micBtn.classList.add('micBtn-off')
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.lang = 'en-US';
micBtn.addEventListener('click', () => {
micBtn.classList.remove('micBtn-off')
micBtn.classList.add('micBtn-on')
recognition.start();
});
recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript;
itemName.value = transcript;
};
recognition.onend = () => {
micBtn.classList.remove('micBtn-on')
micBtn.classList.add('micBtn-off')
};
recognition.onerror = (event) => {
console.error('Speech recognition error:', event.error);
micBtn.classList.add('micBtn-off')
};
grocery_list.forEach((item) => {
let option = document.createElement('option')
option.innerHTML = item
grocerylist.appendChild(option)
})
let totalItemsCount = (item) => {
return item.length
}
console.log(itemsArray.length)
let itemsPurchasedCount = (item) => {
let cart = 0
item.forEach((i) => {
let item_ob = i
if (item_ob.item_status === 'purchased') {
cart += 1
}
})
return cart
}
let itemsCounter = (totalItemsCount, itemsPurchasedCount) => {
totalItems.innerHTML = `Total number of items Purchased ${itemsPurchasedCount} / ${totalItemsCount}`
}
let updateCounts = () => {
itemsCounter(totalItemsCount(itemsArray), itemsPurchasedCount(itemsArray));
};
let saveItem = (item) => {
emptyTasks()
let card = document.createElement('div')
card.classList.add('card')
let itemDetail = document.createElement('div')
itemDetail.classList.add('item-detail')
itemDetail.innerHTML = `${item.item_name} ${item.quantity}${item.metrics}`
card.appendChild(itemDetail)
let statusBox = document.createElement('div')
statusBox.classList.add('status-box')
card.appendChild(statusBox)
section.appendChild(card)
let purchased = document.createElement('div')
purchased.classList.add('purchased')
statusBox.appendChild(purchased)
let notPurchased = document.createElement('div')
notPurchased.classList.add('not-purchased')
statusBox.appendChild(notPurchased)
let deleteItem = document.createElement('div')
deleteItem.classList.add('delete')
statusBox.appendChild(deleteItem)
purchased.addEventListener('click', () => {
item.item_status = 'purchased';
updateItemInArray(item);
localStorage.setItem('items', JSON.stringify(itemsArray))
card.style.background = '#8ac926'
itemDetail.style.textDecoration = 'line-through'
updateCounts();
})
notPurchased.addEventListener('click', () => {
item.item_status = 'not purchased'
updateItemInArray(item);
localStorage.setItem('items', JSON.stringify(itemsArray))
card.style.background = '#FCC72C'
itemDetail.style.textDecoration = 'none'
updateCounts();
})
deleteItem.addEventListener('click', () => {
let itemConfirmation = confirm('Do you want to delete this Item?')
if(itemConfirmation){
section.removeChild(card)
let res = itemsArray.indexOf(item)
if (res > -1) {
itemsArray.splice(res, 1); // Remove the element
}
localStorage.setItem('items', JSON.stringify(itemsArray))
location.reload()
}
})
let task_object = item
if (task_object.item_status === 'not purchased') {
card.style.background = '#FCC72C'
itemDetail.style.textDecoration = 'none'
updateCounts();
} else {
itemDetail.style.textDecoration = 'line-through'
card.style.background = '#8ac926'
updateCounts();
}
}
let updateItemInArray = (updatedItem) => {
let index = itemsArray.findIndex(item => item.id === updatedItem.id);
if (index !== -1) {
itemsArray[index] = updatedItem;
}
};
itemsArray.forEach(saveItem)
let addItem = () => {
if (itemName.value === '') {
alert('Item Name should not be Empty!!')
}else if(quantity.value === ''){
alert('Quantity Should not be empty')
}else if(metrics.value === ''){
alert('Weight of an Item Should not be empty')
}else{
let item = {
id: Date.now(),
item_name: itemName.value,
quantity: quantity.value,
metrics: metrics.value,
item_status: 'not purchased'
}
itemsArray.push(item)
localStorage.setItem('items', JSON.stringify(itemsArray))
saveItem(item)
updateCounts();
itemName.value = ''
quantity.value = ''
metrics.value = ''
}
}
let deleteAll = () => {
if (itemsArray.length === 0) {
alert('No Tasks to Delete')
} else {
let confirmation = confirm('Do you want do Delete all Items?')
if(confirmation){
localStorage.clear()
itemsArray.length = 0;
while (section.firstChild) {
section.removeChild(section.firstChild);
}
location.reload()
}
}
}
emptyTasks()
deleteAllBtn.addEventListener('click', deleteAll)
addItemBtn.addEventListener('click', addItem)