-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
333 lines (279 loc) · 9.06 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//-----------variables
let cartButt = document.querySelector('.cart-btn');
let closeCartButt = document.querySelector('.close-cart');
let clearCartButt = document.querySelector('.clear-cart');
let cartMenu = document.querySelector(".cart");
let cartOpacity = document.querySelector(".cart-overlay");
//actual cart
let cartItems = document.querySelector(".cart-items");
let cartTotal = document.querySelector(".cart-total");
let cartContent = document.querySelector(".cart-content");
//products
let productsMenu = document.querySelector(".products-center");
//cart
let cart = [];
let buttons = [];
//------------CLASSES
//getting products from json
class Products{
async getProducts(){
try {
let result = await fetch('products.json');
//getting data in json format
let data = await result.json();
//cleaning up json data:
let products = data.items;
products = products.map(item =>{
let {title, price} = item.fields;
let {id} = item.sys;
let image = item.fields.image.fields.file.url;
let description = item.fields.description;
return {title, price, id, image, description};
})
return products;
} catch (error) {
console.log(error);
}
}
}
// display products on website (insted of coding HTML)
class Creating{
displayProducts(products){
let result ='';
products.forEach(product => {
result +=`
<article class="product">
<div class="img-container">
<img src= ${product.image}
alt="produkt ${product.title}"
class="product-img">
<!-- <a href="productpage.html">
<button class="bag-btn"
data-id= ${product.id}>
<p>Läs mer</p>
</button>
</a> -->
</div>
<div class="products-items">
<div class="products-items-row2">
<h3> ${product.title} </h3>
<h4> ${product.price} SEK</h4>
</div>
<button class="grid-btn" data-id= ${product.id}>
<p>KÖP</p>
</button>
</div>
</article>
`;
});
productsMenu.innerHTML = result;
}
getButtons(){
//creating nodes to arrays so I can loop through them and use array-methods:
let buttonsCart = [...document.querySelectorAll(".grid-btn")];
buttons = buttonsCart;
//alternative buttons:
// let buttonsMenu = [...document.querySelectorAll(".grid-btn")];
// allMenuButtons = buttonsMenu;
//*getting the ID:s and checking if they are in the cart
buttonsCart.forEach(button => {
let id = button.dataset.id;
let inCart = cart.find(item => item.id === id);
//adding to cart, disabling double-adding
if(inCart){
button.innerText = "I varukorgen";
button.disabled = true;
button.style.backgroundColor ="#707070";
button.style.color ="#ececec"
}
//button on image:
button.addEventListener("click", (event) => {
event.target.innerText ="I varukorgen";
event.target.disabled = true;
//product from products
let cartItem = {...Storage.getProduct(id), amount:1};
//add product to the cart (in array)
cart = [...cart,cartItem];
//save cart-array in local storage
Storage.saveCart(cart);
//set cart values
this.setCartValue(cart);
//display cart item
this.addCartItem(cartItem);
//show cart
this.showCart();
});
})
};
setCartValue(cart){
let temporaryTotal = 0;
let itemsTotal = 0;
cart.map(item =>{
//amount of items multiplied by their prices
temporaryTotal += item.price * item.amount;
itemsTotal += item.amount;
});
//showing amount in cart in navigations wooo!:
cartItems.innerText = itemsTotal;
///fixing decimals
cartTotal.innerText = parseFloat(temporaryTotal.toFixed(2));
}
//for each cart item:
addCartItem(item){
let cartdiv = document.createElement("div");
cartdiv.classList.add("cart-item-new");
cartdiv.innerHTML = `
<div class="cart-item" data-id=${item.id}>
<img src=${item.image}
alt="product" />
<div>
<h4>${item.title}</h4>
<h5>${item.price}SEK</h5>
<span class="remove-item" data-id=${item.id} >Remove</span>
</div>
<div>
<i class="fas fa-chevron-up" data-id=${item.id}></i>
<p class="item-amount">${item.amount}</p>
<i class="fas fa-chevron-down"data-id=${item.id} ></i>
</div>
</div>
`
cartContent.appendChild(cartdiv);
}
setupCart(){
cart = Storage.getCartStart();
this.setCartValue(cart);
this.populateCart(cart);
//activate buttons (close, open)
cartButt.addEventListener("click", this.showCart);
//hide cart
closeCartButt.addEventListener("click",this.hideCart);
};
showCart(){
cartOpacity.classList.add("transparentBcg");
cartMenu.classList.add("showCart");
};
hideCart(){
cartOpacity.classList.remove("transparentBcg");
cartMenu.classList.remove("showCart");
}
//method for cart array
populateCart(cart){
//adding cart items to the cart
cart.forEach(item => this.addCartItem(item));
}
cartLogic(){
//clear cart (whole cart)
clearCartButt.addEventListener("click", () => {
this.clearCart();
});
//cart functionality (remove , add, take away ONE ITEM)
cartContent.addEventListener("click",event => {
if(event.target.classList.contains("remove-item")){
let removeItem = event.target;
let id = removeItem.dataset.id;
let removingDom = removeItem.parentElement.parentElement;
removingDom.classList.add("hide-item");
this.removeItem(id);
}
else if (event.target.classList.contains("fa-chevron-up")){
let addAmount = event.target;
let addId = addAmount.dataset.id;
//push new amount into local storage:
let temporaryItem = cart.find(item => item.id === addId);
temporaryItem.amount = temporaryItem.amount + 1;
Storage.saveCart(cart);
this.setCartValue(cart);
addAmount.nextElementSibling.innerText = temporaryItem.amount;
}
else if (event.target.classList.contains("fa-chevron-down")){
let reduceAmount = event.target;
let reduceId = reduceAmount.dataset.id;
//reduce new amount:
let temporaryItem = cart.find(item => item.id ===reduceId);
temporaryItem.amount = temporaryItem.amount -1;
if(temporaryItem.amount > 0){
Storage.saveCart(cart);
this.setCartValue(cart);
reduceAmount.previousElementSibling.innerText = temporaryItem.amount;
}
else{
console.log(reduceAmount.parentElement.parentElement.classList)
console.log(temporaryItem.id);
let classID = (reduceAmount.parentElement.parentElement.getAttribute("data-id"));
console.log(classID);
if (temporaryItem.id === classID){
reduceAmount.parentElement.parentElement.classList.add("hide-item")
}
this.removeItem(temporaryItem.id);
}
}
});
}
clearCart(){
//get all the ID:s from the cart
let cartItemsAllClear = cart.map(item => item.id);
//loop over the array and remove
cartItemsAllClear.forEach(id => this.removeItem(id));
while(cartContent.children.length>0){
cartContent.removeChild(cartContent.children[0]);
}
this.hideCart();
}
removeItem(id){
cart = cart.filter(item => item.id !== id);
this.setCartValue(cart);
Storage.saveCart(cart);
let button = this.getSingleCartButton(id);
button.disabled = false;
button.innerText = `KÖP`;
button.style.backgroundColor ="#ED1C24";
button.style.color ="#ececec";
}
getSingleCartButton(id){
return buttons.find(button => button.dataset.id === id);
};
};
//*----------------local storage
//all products
class Storage{
static saveProducts(products){
localStorage.setItem("products", JSON.stringify(products));
}
//take product from products
static getProduct(id){
let products = JSON.parse(localStorage.getItem('products'));
return products.find(product => product.id ===id);
}
//cart array in local storage
static saveCart(cart){
localStorage.setItem("cart", JSON.stringify(cart));
}
static getCartStart(){
//saving the cart for future visits or, for first time visitors, an empty array:
return localStorage.getItem('cart')?JSON.parse(localStorage.getItem('cart')):[];
}
}
//closing cart with escape button
document.addEventListener('keydown', function(event){
if(event.key === "Escape"){
cartOpacity.classList.remove("transparentBcg");
cartMenu.classList.remove("showCart");
}
});
//------function eventlistener(loading page)
document.addEventListener("DOMContentLoaded",() =>{
let creating = new Creating();
let products = new Products();
//setting up
creating.setupCart();
//get all products and fix local storage
//*by using .then we add things ONLY after adding products
products.getProducts().then(products => {
creating.displayProducts(products);
Storage.saveProducts(products);
}).then(()=>{
creating.getButtons();
creating.cartLogic();
});
});