-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
218 lines (185 loc) · 7.14 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
214
215
216
217
218
let carts = document.querySelectorAll('.add-cart');
let products = [
{
name: "Black Hoodie",
tag: "blackhoodie",
price: 20,
inCart: 0
},
{
name: "Navy Hoodie",
tag: "navyhoodie",
price: 15,
inCart: 0
},
{
name: "Purple Hoodie",
tag: "purplehoodie",
price: 25,
inCart: 0
},
{
name: "Cyan Hoodie",
tag: "cyanhoodie",
price: 15,
inCart: 0
},
{
name: "Yellow Hoodie",
tag: "yellowhoodie",
price: 15,
inCart: 0
}
];
for(let i=0; i< carts.length; i++) {
carts[i].addEventListener('click', () => {
cartNumbers(products[i]);
totalCost(products[i]);
});
}
function onLoadCartNumbers() {
let productNumbers = localStorage.getItem('cartNumbers');
if( productNumbers ) {
document.querySelector('.cart span').textContent = productNumbers;
}
}
function cartNumbers(product, action) {
let productNumbers = localStorage.getItem('cartNumbers');
productNumbers = parseInt(productNumbers);
let cartItems = localStorage.getItem('productsInCart');
cartItems = JSON.parse(cartItems);
if( action ) {
localStorage.setItem("cartNumbers", productNumbers - 1);
document.querySelector('.cart span').textContent = productNumbers - 1;
console.log("action running");
} else if( productNumbers ) {
localStorage.setItem("cartNumbers", productNumbers + 1);
document.querySelector('.cart span').textContent = productNumbers + 1;
} else {
localStorage.setItem("cartNumbers", 1);
document.querySelector('.cart span').textContent = 1;
}
setItems(product);
}
function setItems(product) {
// let productNumbers = localStorage.getItem('cartNumbers');
// productNumbers = parseInt(productNumbers);
let cartItems = localStorage.getItem('productsInCart');
cartItems = JSON.parse(cartItems);
if(cartItems != null) {
let currentProduct = product.tag;
if( cartItems[currentProduct] == undefined ) {
cartItems = {
...cartItems,
[currentProduct]: product
}
}
cartItems[currentProduct].inCart += 1;
} else {
product.inCart = 1;
cartItems = {
[product.tag]: product
};
}
localStorage.setItem('productsInCart', JSON.stringify(cartItems));
}
function totalCost( product, action ) {
let cart = localStorage.getItem("totalCost");
if( action) {
cart = parseInt(cart);
localStorage.setItem("totalCost", cart - product.price);
} else if(cart != null) {
cart = parseInt(cart);
localStorage.setItem("totalCost", cart + product.price);
} else {
localStorage.setItem("totalCost", product.price);
}
}
function displayCart() {
let cartItems = localStorage.getItem('productsInCart');
cartItems = JSON.parse(cartItems);
let cart = localStorage.getItem("totalCost");
cart = parseInt(cart);
let productContainer = document.querySelector('.products');
if( cartItems && productContainer ) {
productContainer.innerHTML = '';
Object.values(cartItems).map( (item, index) => {
productContainer.innerHTML +=
`<div class="product"><ion-icon name="close-circle"></ion-icon><img src="./images/${item.tag}.png" />
<span class="sm-hide">${item.name}</span>
</div>
<div class="price sm-hide">$${item.price},00</div>
<div class="quantity">
<ion-icon class="decrease " name="arrow-dropleft-circle"></ion-icon>
<span>${item.inCart}</span>
<ion-icon class="increase" name="arrow-dropright-circle"></ion-icon>
</div>
<div class="total">$${item.inCart * item.price},00</div>`;
});
productContainer.innerHTML += `
<div class="basketTotalContainer">
<h4 class="basketTotalTitle">Basket Total</h4>
<h4 class="basketTotal">$${cart},00</h4>
</div>`
deleteButtons();
manageQuantity();
}
}
function manageQuantity() {
let decreaseButtons = document.querySelectorAll('.decrease');
let increaseButtons = document.querySelectorAll('.increase');
let currentQuantity = 0;
let currentProduct = '';
let cartItems = localStorage.getItem('productsInCart');
cartItems = JSON.parse(cartItems);
for(let i=0; i < increaseButtons.length; i++) {
decreaseButtons[i].addEventListener('click', () => {
console.log(cartItems);
currentQuantity = decreaseButtons[i].parentElement.querySelector('span').textContent;
console.log(currentQuantity);
currentProduct = decreaseButtons[i].parentElement.previousElementSibling.previousElementSibling.querySelector('span').textContent.toLocaleLowerCase().replace(/ /g,'').trim();
console.log(currentProduct);
if( cartItems[currentProduct].inCart > 1 ) {
cartItems[currentProduct].inCart -= 1;
cartNumbers(cartItems[currentProduct], "decrease");
totalCost(cartItems[currentProduct], "decrease");
localStorage.setItem('productsInCart', JSON.stringify(cartItems));
displayCart();
}
});
increaseButtons[i].addEventListener('click', () => {
console.log(cartItems);
currentQuantity = increaseButtons[i].parentElement.querySelector('span').textContent;
console.log(currentQuantity);
currentProduct = increaseButtons[i].parentElement.previousElementSibling.previousElementSibling.querySelector('span').textContent.toLocaleLowerCase().replace(/ /g,'').trim();
console.log(currentProduct);
cartItems[currentProduct].inCart += 1;
cartNumbers(cartItems[currentProduct]);
totalCost(cartItems[currentProduct]);
localStorage.setItem('productsInCart', JSON.stringify(cartItems));
displayCart();
});
}
}
function deleteButtons() {
let deleteButtons = document.querySelectorAll('.product ion-icon');
let productNumbers = localStorage.getItem('cartNumbers');
let cartCost = localStorage.getItem("totalCost");
let cartItems = localStorage.getItem('productsInCart');
cartItems = JSON.parse(cartItems);
let productName;
console.log(cartItems);
for(let i=0; i < deleteButtons.length; i++) {
deleteButtons[i].addEventListener('click', () => {
productName = deleteButtons[i].parentElement.textContent.toLocaleLowerCase().replace(/ /g,'').trim();
localStorage.setItem('cartNumbers', productNumbers - cartItems[productName].inCart);
localStorage.setItem('totalCost', cartCost - ( cartItems[productName].price * cartItems[productName].inCart));
delete cartItems[productName];
localStorage.setItem('productsInCart', JSON.stringify(cartItems));
displayCart();
onLoadCartNumbers();
})
}
}
onLoadCartNumbers();
displayCart();