-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
174 lines (156 loc) · 5.31 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
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
const bar = document.getElementById("bar");
const close = document.getElementById("close");
const nav = document.getElementById("navbar");
let iconCart = document.getElementById("cart-car");
let closeCart = document.querySelector(".close");
let listProductHTML = document.querySelector(".listProduct");
let listCartHTML = document.querySelector(".listCart");
let iconCartSpan = document.querySelector(".iconCart span");
let listProducts = [];
let carts = [];
if (bar) {
bar.addEventListener("click", () => {
nav.classList.add("active");
});
}
if (close) {
close.addEventListener("click", () => {
nav.classList.remove("active");
});
}
iconCart.addEventListener("click", () => {
document.body.classList.toggle("showCart");
});
closeCart.addEventListener("click", () => {
document.body.classList.toggle("showCart");
});
const addDataToHTML = () => {
listProductHTML.innerHTML = "";
if (listProducts.length > 0) {
listProducts.forEach((product) => {
let newProduct = document.createElement("div");
newProduct.classList.add("item");
newProduct.dataset.id = product.id;
newProduct.innerHTML = `
<img src="${product.image}" alt="">
<h2>${product.name}</h2>
<div class="price">$${product.price}</div>
<button class="addCart">
Add To Cart
</button>
`;
listProductHTML.appendChild(newProduct);
});
}
};
listProductHTML.addEventListener("click", (event) => {
let positionClick = event.target;
if (positionClick.classList.contains("addCart")) {
let product_id = positionClick.parentElement.dataset.id;
addToCart(product_id);
}
});
const addToCart = (product_id) => {
let positionThisProductInCart = carts.findIndex(
(value) => value.product_id == product_id
);
if (carts.length <= 0 || positionThisProductInCart === -1) {
carts.push({
product_id: product_id,
quantity: 1,
});
} else {
carts[positionThisProductInCart].quantity += 1;
}
addCartToHTML();
addCartToMemory();
};
const addCartToMemory = () => {
localStorage.setItem("cart", JSON.stringify(carts));
};
const addCartToHTML = () => {
listCartHTML.innerHTML = "";
let totalQuantity = 0;
if (carts.length > 0) {
carts.forEach((cart) => {
totalQuantity = totalQuantity + cart.quantity;
let newCart = document.createElement("div");
newCart.classList.add("item");
newCart.dataset.id = cart.product_id;
let positionProduct = listProducts.findIndex(
(value) => value.id == cart.product_id
);
let info = listProducts[positionProduct];
newCart.innerHTML = `
<div class="image">
<img src="${info.image}" alt="">
</div>
<div class="name">
${info.name}
</div>
<div class="totalPrice">
$${info.price * cart.quantity}
</div>
<div class="quantity">
<span class="minus">-</span>
<span>${cart.quantity}</span>
<span class="plus">+</span>
</div>
`;
listCartHTML.appendChild(newCart);
});
}
iconCartSpan.innerText = totalQuantity;
};
listCartHTML.addEventListener("click", (event) => {
let positionClick = event.target;
if (
positionClick.classList.contains("minus") ||
positionClick.classList.contains("plus")
) {
let product_id = positionClick.parentElement.parentElement.dataset.id;
let type = "minus";
if (positionClick.classList.contains("plus")) {
type = "plus";
}
changeQuantity(product_id, type);
}
});
const changeQuantity = (product_id, type) => {
let positionItemInCart = carts.findIndex(
(value) => value.product_id == product_id
);
if (positionItemInCart >= 0) {
switch (type) {
case "plus":
carts[positionItemInCart].quantity =
carts[positionItemInCart].quantity + 1;
break;
default:
let valueChange = carts[positionItemInCart].quantity - 1;
if (valueChange > 0) {
carts[positionItemInCart].quantity = valueChange;
} else {
carts.splice(positionItemInCart, 1);
}
break;
}
}
addCartToMemory();
addCartToHTML();
};
const initApp = () => {
// get data from json
fetch("products.json")
.then((response) => response.json())
.then((data) => {
listProducts = data;
addDataToHTML();
// get cart from memory
if (localStorage.getItem("cart")) {
carts = JSON.parse(localStorage.getItem("cart"));
addCartToHTML();
}
});
};
initApp();