-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (75 loc) · 2.65 KB
/
index.js
File metadata and controls
96 lines (75 loc) · 2.65 KB
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
const db = new Dexie('ShoppingApp')
db.version(1).stores({ items: '++id,name,price,isPurchased' })
const itemForm = document.getElementById('itemForm')
const itemsDiv = document.getElementById('itemsDiv')
const totalPriceDiv = document.getElementById('totalPriceDiv')
const populateItemsDiv = async () => {
const allItems = await db.items.reverse().toArray()
itemsDiv.innerHTML = allItems.map(item => `
<div class="item ${item.isPurchased && 'purchased'}">
<input
type="checkbox"
class="checkbox"
onchange="toggleItemStatus(event, ${item.id})"
${item.isPurchased && 'checked'}
/>
<div class="itemInfo">
<p>${item.name}</p>
<p>$${item.price} x ${item.quantity}</p>
</div>
<button class="editItemButton" onclick="editItem(event, ${item.id})">
Edit item
</button>
<button class="updateItemButton" onclick="updateItem(event, ${item.id})">
Update item
</button>
<button onclick="removeItem(${item.id})" class="deleteButton">
X
</button>
</div>
`).join('')
const arrayOfPrices = allItems.map(item => item.price * item.quantity)
const totalPrice = arrayOfPrices.reduce((a, b) => a + b, 0)
totalPriceDiv.innerText = 'Total price: $' + totalPrice
}
window.onload = populateItemsDiv
itemForm.onsubmit = async (event) => {
event.preventDefault()
const name = document.getElementById('nameInput').value
const quantity = document.getElementById('quantityInput').value
const price = document.getElementById('priceInput').value
await db.items.add({ name, quantity, price})
await populateItemsDiv()
itemForm.reset()
}
const toggleItemStatus = async (event, id) => {
await db.items.update(id, { isPurchased: !!event.target.checked })
await populateItemsDiv()
}
const removeItem = async id => {
await db.items.delete(id)
await populateItemsDiv()
}
// delete all items
const clearAllItems = document.getElementById('clearItemsButton')
clearAllItems.onclick = async () => {
await db.items.clear()
.then(async (data) => {
console.log(data)
populateItemsDiv()
})
.catch((err) => {
console.log(err)
if (err) {
alert("Failed to delete all items")
}
})
}
const editItem = () => {
nameInput.focus()
}
const updateItem = async (event, id) => {
await db.items.update(id, {name: nameInput.value, price: priceInput.value, quantity: quantityInput.value})
await populateItemsDiv()
itemForm.reset()
}