forked from boolean-uk/js-dom-greengrocers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (160 loc) · 4.32 KB
/
index.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
const state = {
items: [
{
id: "001-beetroot",
name: "beetroot",
quantity: 0,
price: 0.35,
total: 0
},
{
id: "002-carrot",
name: "carrot",
quantity: 0,
price: 0.35,
total: 0
},
{
id: "003-apple",
name: "apple",
quantity: 0,
price: 0.35,
total: 0
},
{
id: "004-apricot",
name: "apricot",
quantity: 0,
price: 0.35,
total: 0
},
{
id: "005-avocado",
name: "avocado",
quantity: 0,
price: 0.35,
total: 0
},
{
id: "006-bananas",
name: "bananas",
quantity: 0,
price: 0.35,
total: 0
},
{
id: "007-bell-pepper",
name: "bell pepper",
quantity: 0,
price: 0.35,
total: 0
},
{
id: "008-berry",
name: "berry",
quantity: 0,
price: 0.35,
total: 0
},
{
id: "009-blueberry",
name: "blueberry",
quantity: 0,
price: 0.35,
total: 0
},
{
id: "010-eggplant",
name: "eggplant",
quantity: 0,
price: 0.35,
total: 0
}
],
cart: []
};
const storeList = document.querySelector('ul')
const cartList = document.querySelector('.cart--item-list')
const cartPrice = document.querySelector('.total-number')
function renderShopList() {
for (i = 0; i < state.items.length; i++) {
let item = state.items[i]
//creates list element and img element
const shopItem = document.createElement('li')
shopItem.setAttribute('id', state.items[i].id)
const itemImage = document.createElement('img')
// uses string interpolation within the loop to retrieve all images
itemImage.setAttribute('src', `/assets/icons/${state.items[i].id}.svg`)
itemImage.setAttribute('alt', '')
itemImage.setAttribute('height', '100')
itemImage.style.objectFit = 'cover'
// adds a purchase button as a child so it does not count as its own element
const purchaseButton = document.createElement('button')
purchaseButton.innerText = "Add to Cart"
// assigns an id to each button to give them individual uses
purchaseButton.setAttribute('id', state.items[i].id)
// listens for a click event
purchaseButton.addEventListener('click', () => {
item.quantity++ // adds 1 to the quantity
item.total = item.quantity * item.price
if(state.cart.includes(item)) { // checks to see if the item already exists within the array
renderCart()
return
}
state.cart.push(item)
renderCart()
})
shopItem.append(itemImage, purchaseButton)
storeList.append(shopItem)
}
}
function renderCart() {
cartList.innerHTML = ''
for (i = 0; i < state.cart.length; i++) {
let item = state.cart[i]
console.log(item)
const cartItem = document.createElement('li')
const itemImage = document.createElement('img')
itemImage.setAttribute('src', `/assets/icons/${state.cart[i].id}.svg`)
itemImage.setAttribute('alt', '')
itemImage.setAttribute('height', '22')
itemImage.style.objectFit = 'cover'
const itemName = document.createElement('p')
itemName.innerText = state.cart[i].name
const removeButton = document.createElement('button')
removeButton.innerText = '-'
removeButton.addEventListener('click', () => {
item.quantity --
item.total = item.quantity * item.price
for (k = 0; k < state.cart.length; k++) // loops through the whole array to spot 0 values and remove them
if(state.cart[k].quantity === 0) {
state.cart.splice(k, 1) // splices items from the array
renderCart()
return
}
renderCart()
})
const quantityCounter = document.createElement('span')
quantityCounter.innerText = item.quantity
const addButton = document.createElement('button')
addButton.innerText = '+'
addButton.addEventListener('click', () => {
item.quantity ++
item.total = item.quantity * item.price
renderCart()
})
cartItem.append(itemImage, itemName, removeButton, quantityCounter, addButton)
cartList.append(cartItem)}
updatePrice()
}
function updatePrice() {
let price = 0
console.log(state.cart)
for (let i = 0; i < state.cart.length; i++) {
console.log(state.cart[i])
price += state.cart[i].total
console.log(price)
}
cartPrice.innerText = `£${price.toFixed(2)}`
}
renderShopList(state)