-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
289 lines (212 loc) · 6.72 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
const products = [
{
id: 1,
imgSrc: 'assets/img/product1.jpg',
title: 'Nike Shoes1',
price: '$79.5',
},
{
id: 2,
imgSrc: 'assets/img/product2.jpg',
title: 'Nike Shoes2',
price: '$79.5',
},
{
id: 3,
imgSrc: 'assets/img/product3.jpg',
title: 'Nike Shoes3',
price: '$79.5',
},
{
id: 4,
imgSrc: 'assets/img/product4.jpg',
title: 'Nike Shoes4',
price: '$79.5',
},
{
id: 5,
imgSrc: 'assets/img/product5.jpg',
title: 'Nike Shoes5',
price: '$79.5',
},
{
id: 6,
imgSrc: 'assets/img/product6.jpg',
title: 'Nike Shoes6',
price: '$79.5',
},
{
id: 7,
imgSrc: 'assets/img/product7.jpg',
title: 'Nike Shoes7',
price: '$79.5',
},
{
id: 8,
imgSrc: 'assets/img/product8.jpg',
title: 'Nike Shoes8',
price: '$79.5',
},
];
// open & close cart
const cartIcon = document.querySelector("#cart-icon");
cartIcon.addEventListener("click", () => {
cart.classList.add("active");
});
const closeCart = document.querySelector("#cart-close");
closeCart.addEventListener("click", () => {
cart.classList.remove("active");
});
// add and bost your products
const shopContent = document.querySelector(".shop-content");
document.addEventListener('DOMContentLoaded', function () {
products.forEach(product => {
const productElement = document.createElement('div');
productElement.innerHTML = `
<div class="product-box">
<img src="${product.imgSrc}" alt="${product.title}" class="product-img">
<h2 class="product-title">${product.title}</h2>
<span class="product-price">${product.price}</span>
<i class=' bx bx-shopping-bag add-cart'></i>
</div>
`;
shopContent.appendChild(productElement);
});
});
const cart = document.querySelector(".cart");
// Start when the document is ready
if (document.readyState == "loading") {
document.addEventListener('DOMContentLoaded', start);
} else {
start();
}
// ===== == ===== START ====
function start() {
addEvents();
}
// ================UPDATE & RERENDER
function update() {
addEvents();
updateTotal();
}
//========== ADD EVENTS ====
function addEvents() {
// Remove items from cart
let cartRemove_btns = document.querySelectorAll(".cart-remove");
// console.log(cartRemove_btns);
cartRemove_btns.forEach((btn) => {
btn.addEventListener("click", handle_remoeCartItem)
});
// Change item quantity
let cartQuantity_inputs = document.querySelectorAll('.cart-quantity');
cartQuantity_inputs.forEach(input => {
input.addEventListener("change", handle_changeItemQuantity);
console.log(input.addEventListener("change", handle_changeItemQuantity));
})
// console.log(cartQuantity_inputs);
// Add item to cart
let addCart_btns = document.querySelectorAll(".add-cart");
addCart_btns.forEach(btn => {
btn.addEventListener("click", handle_addCartItem)
})
var buy_btn=document.querySelector(".btn-buy")
buy_btn.addEventListener("click",handle_buyOrder)
}
// ============handle_event function=========
let itemAdded=[]
function handle_addCartItem() {
let product = this.parentElement;
}
function handle_addCartItem() {
let product = this.parentElement;
let title = product.querySelector(".product-title").innerHTML;
let price = product.querySelector(".product-price").innerHTML;
let imgsrc = product.querySelector(".product-img").src;
// console.log(title, price, imgsrc);
let newToAdd = { title, price, imgsrc };
//handle item is as exited
// handle item is already exist
// handle item is already exist
if (itemAdded.find((el) => el.title == newToAdd.title)) {
alert("This Item Is Already Exist!");
return;
}
else {
itemAdded.push(newToAdd);
}
// Add product to cart imgsrc
let cartBoxElement = CartBoxComponent(title, price, imgsrc);
let newNode = document.createElement('div');
newNode.innerHTML = cartBoxElement;
const cartcontent = cart.querySelector(".cart-content");
cartcontent.appendChild(newNode);
update()
}
function handle_remoeCartItem() {
this.parentElement.remove();
// ظظظ ؟؟
itemAdded =itemAdded.filter(
(el) => el.title !=
this.parentElement.querySelector(".cart-product-title").innerHTML
)
update()
}
function handle_changeItemQuantity() {
if (isNaN(this.value) || this.value < 1) {
this.value = 1;
}
this.value = Math.floor(this.value); // to keep it integer
update();
}
function handle_buyOrder( ) {
console.log("fdf");
if (itemAdded.length<=0) {
// alert("no order ")
// Create an "li" node:
const node = document.createElement("li");
// Create a text node:
const textnode = document.createTextNode("yor card is empty");
node.appendChild(textnode);
const dog= document.querySelector('.total').appendChild(node);//.appendChild="<h1>dddd</h1>"
console.log(dog);
//????
return;
}
var cartcontent=cart.querySelector(".cart-content")
console.log(cartcontent);
cartcontent.innerHTML=" handle_buyOrder";
alert("you orderd succesefuly ")
itemAdded
update();
}
// == == UPDATE & RERENDER FUNCTIONS
function updateTotal() {
let cartBoxes = document.querySelectorAll(".cart-box");
const totalElement = cart.querySelector(".total-price");
let total = 0;
cartBoxes.forEach((cartBox) => {
let priceElement = cartBox.querySelector(".cart-price");
let price = parseFloat(priceElement.innerHTML.replace("$", ""));
let quantity = cartBox.querySelector(".cart-quantity").value;
total += price * quantity;
console.log(total);
});
//keep 2 digits after the decimal point 12,22222 >> 12,12
total = total.toFixed(2)
if (total == 0) {
// ram >>>> pay now must be <a> or botton
totalElement.innerHTML = "$" + total + " pay now";
} else {
totalElement.innerHTML = "$" + total;
}
}
// html Commponant
// let cartBoxElement = CartBoxComponent(title,price,imgSrc)
function CartBoxComponent(title, price, imgSrc) {
return '<div class="cart-content"> <div class="cart-box">' +
'<img src="' + imgSrc + '" alt="" class="cart-img"> <div class="detail-box">'
+ ' <div class="cart-product-title">' + title + '</div>'
+ ' <div class="cart-price">' + price + '</div>'
+ '<input type="number" value="1" class="cart-quantity">'
+ '</div><!-- REMOVE CART --><i class="bx bxs-trash-alt cart-remove"></i></div></div>';
}