-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
108 lines (96 loc) · 3.98 KB
/
app.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
//const taxRate = 0.18;
// const shippingPrice = 25.99;
// const freeShippingPrice = 3000;
//localStorage vs. sessionStorage
window.addEventListener("load", () => {
// localStorage.setItem("taxRate", taxRate);
// localStorage.setItem("shippingPrice", shippingPrice);
// localStorage.setItem("freeShippingPrice", freeShippingPrice);
///total cart calc.
calculateCartPrice();
});
const navbarList = document.querySelector(".nav__list");
const productList = document.querySelector("div.main__product-painel");
//capturing
navbarList.addEventListener("click", (e) => {
if (e.target.getAttribute("class") == "nav__list--btn" || "fa-regular fa-trash-can") {
//e.target.parentElement.firstElementChild.innerText = "My Cart";
//e.target.previousElementSibling.innerText = "My Cart";
//e.target vs. e.currentTarget
//foreach ==> array, nodeList
productList.innerText = "No Product!";
e.currentTarget.firstElementChild.innerText = "My Cart";
calculateCartPrice();
}
});
//capturing
productList.addEventListener("click", (e)=>{
//minus
if(e.target.className == "fa-solid fa-minus"){
if(e.target.nextElementSibling.innerText > 1){
e.target.nextElementSibling.innerText --;
calculateProductPrice(e.target);
}
else{
if(confirm(`${e.target.closest(".main__product-info").querySelector("h2").innerText} will be removed!`)){
e.target.closest(".main__product").remove();
calculateCartPrice();
}
}
}
//plus
else if(e.target.classList.contains("fa-plus")){
e.target.previousElementSibling.innerText ++;
calculateProductPrice(e.target);
}
//remove
else if(e.target.id == "remove-product"){
if(confirm(`${e.target.closest(".main__product-info").querySelector("h2").innerText} will be removed!`)){
e.target.closest(".main__product").remove();
calculateCartPrice();
}
}
else{
alert("other element clicked");
}
//calculateCartPrice();
});
//target == minus || plus btn
const calculateProductPrice = (btn) =>{
//product line calculations
const infoDiv = btn.closest(".main__product-info");
//console.log(infoDiv);
const price = infoDiv.querySelector(".main__product-price strong").innerText;
//console.log(price);
const quantity = infoDiv.querySelector("#quantity").innerText;
console.log(quantity);
infoDiv.querySelector(".main__product-line-price").innerText = (price * quantity).toFixed(2);
// console.log(typeof (price * quantity).toFixed(2));
calculateCartPrice();
}
const calculateCartPrice = () =>{
//cart total calculations
const productPriceDivs = productList.querySelectorAll(".main__product-line-price");
let subtotal = 0;
//reduce calculation
productPriceDivs.forEach(price=>{
subtotal += parseFloat(price.innerText);
});
console.log(subtotal);
const taxPrice = parseFloat(subtotal * localStorage.getItem("taxRate"));
console.log(taxPrice);
const shippingPrice = subtotal > 0 && subtotal < localStorage.getItem("freeShippingPrice") ? parseFloat(localStorage.getItem("shippingPrice")) : 0;
const totalPrice = (subtotal + shippingPrice + taxPrice).toFixed(2);
console.log(totalPrice);
document.querySelector(".main__total h2").innerText = subtotal.toFixed(2);
document.querySelector("#cart-shipping span:nth-child(2)").innerText = shippingPrice.toFixed(2);
document.querySelector("#cart-tax span:nth-child(2)").innerText = taxPrice.toFixed(2);
document.querySelector("#cart-total").lastElementChild.innerText = totalPrice;
if(productList.querySelectorAll(".main__product").length == 0){
productList.innerText = "No Product!";
navbarList.firstElementChild.innerText = "My Cart";
}
else{
navbarList.firstElementChild.innerText = `My Cart (${productList.querySelectorAll(".main__product").length} Products)`;
}
}