-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (63 loc) · 2.54 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
const taxtRate = 0.18;
const shippingPrice = 20;
const shippingFreePrice = 300;
window.addEventListener("load", () => {
localStorage.setItem("taxtRate", taxtRate);
localStorage.setItem("shippingPrice", shippingPrice);
localStorage.setItem("shippingFreePrice", shippingFreePrice);
// sessionStorage.setItem("taxtRate", taxtRate);
// sessionStorage.setItem("shippingPrice", shippingPrice);
// sessionStorage.setItem("shippingFreePrice", shippingFreePrice);
});
const productsDiv = document.querySelector(".products");
productsDiv.addEventListener("click", (event) => {
if (event.target.className == "fa-solid fa-minus") {
if (event.target.parentElement.querySelector(".quantity").innerText > 1) {
event.target.parentElement.querySelector(".quantity").innerText--;
} else {
if (
confirm(
`${
event.target.parentElement.parentElement.querySelector("h2")
.innerText
} will be deleted!!!`
)
) {
event.target.closest(".product").remove();
}
}
} else if (event.target.className == "fa-solid fa-plus") {
event.target.parentElement.querySelector(".quantity").innerText++;
} else if (event.target.className == "remove-product") {
event.target.closest(".product").remove();
}
calculateProductPrice(event.target);
calculateCartPrice();
});
const calculateProductPrice = (btn) => {
const productInfoDiv = btn.parentElement.parentElement;
const price = Number(
productInfoDiv.querySelector(".product-price strong").innerText
);
const quantity = Number(productInfoDiv.querySelector(".quantity").innerText);
const productTotalDiv = productInfoDiv.querySelector(".price");
productTotalDiv.innerText = (price * quantity).toFixed(2);
};
const calculateCartPrice = () => {
const productsTotalPricesDiv = document.querySelectorAll(".price");
const subtotal = [...productsTotalPricesDiv].reduce(
(acc, price) => acc + Number(price.innerText),
0
);
const taxtPrice = subtotal * localStorage.getItem("taxtRate");
const shippingPrice = parseFloat(
subtotal > 0 && subtotal < localStorage.getItem("shippingFreePrice")
? localStorage.getItem("shippingPrice")
: 0
);
const totalCart = subtotal + taxtPrice + shippingPrice;
document.querySelector("#subtotalCart").innerText = subtotal.toFixed(2);
document.querySelector("#taxRateCart").innerText = taxtPrice.toFixed(2);
document.querySelector("#shippingCart").innerText = shippingPrice.toFixed(2);
document.querySelector("#totalCart").innerText = totalCart.toFixed(2);
};