-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
148 lines (121 loc) · 4.03 KB
/
script.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
/* Declaration of variables from the span class and ADD TO CART class*/
const cart = document.querySelector("span");
const product = document.querySelectorAll(".cart");
const proContainer = document.querySelector(".cart-products");
/* We create a javascript object, i.e JSON to convert an object to string and back to an objct */
let products = [
{
proName: "Black shoe",
tagName: "black",
price: 15,
inCart: 0
},
{
proName: "Yellow Shoe",
tagName: "Yellow",
price: 20,
inCart: 0
},
{
proName: "White shoe",
tagName: "White",
price: 25,
inCart: 0
},
{
proName: "Pink shoe",
tagName: "Pink",
price: 30,
inCart: 0
}
]
/* Create a for... loop to loop through the Array */
for(let i = 0; i < product.length; i++) {
product[i].addEventListener("click", function() {
addNumberToCart(products[i]);
})
}
/* We create a function that increment the number at the cart and we call it at the addEventListener function */
function addNumberToCart(product) {
let number = localStorage.getItem("cartNumber");
number = parseInt(number);
if(number) {
localStorage.setItem("cartNumber", number + 1);
cart.textContent = number + 1;
} else {
localStorage.setItem("cartNumber", 1);
cart.textContent = 1;
}
setProduct(product);
costTotal(product);
}
/* We create a function that can track the exact product on the Cart */
function setProduct(product) {
let cartItem = localStorage.getItem("product");
cartItem = JSON.parse(cartItem);
if(cartItem != null) {
if(cartItem[product.tagName] == undefined) {
cartItem = {
...cartItem,
[product.tagName]:product
}
}
cartItem[product.tagName].inCart += 1;
} else {
product.inCart = 1;
cartItem = {
[product.tagName]:product
}
}
localStorage.setItem("product", JSON.stringify(cartItem));
}
/* We ceate a function to calculate the total product cost */
function costTotal(product) {
let costTotal = localStorage.getItem("costTotal");
if(costTotal != null) {
costTotal = parseInt(costTotal);
localStorage.setItem("costTotal", costTotal + product.price);
} else {
localStorage.setItem("costTotal", product.price);
}
}
/* We create a function onloadScreen */
function onloadScreen() {
let number = localStorage.getItem("cartNumber");
number = parseInt(number);
if(number) {
cart.textContent = number;
}
}
// function to display product on the Cart page //
function displayProduct() {
let productItems = localStorage.getItem("product");
productItems = JSON.parse(productItems);
if (productItems && proContainer) {
proContainer.innerHTML = "";
Object.values(productItems).map(item => {
proContainer.innerHTML += `
<div class="product">
<i class='bx bx-x-circle'></i>
<img src="./img/${item.tagName}.jpg">
<span>${item.proName}</span>
</div>
<div class="price">$${item.price}.00</div>
<div class="quantity">
<i class='bx bx-minus'></i>
<span>${item.inCart}</span>
<i class='bx bx-plus'></i>
</div>
<div class="total">$${item.inCart * item.price}.00</div>`
})
let TOTAL = localStorage.getItem("costTotal");
proContainer.innerHTML +=`<div class"totalcost">
<h4 class="total">
TOTAL
</h4>
<h4 class="cost">$${TOTAL}.00</h4>
</div>`
}
}
displayProduct()
onloadScreen();