-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
168 lines (141 loc) · 4.45 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
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
// IGNORE THIS(CUSTOM-ALERT)=================
iziToast.settings({
timeout: 1500,
});
// ==========================================
// NAVIGATION ===============================
const dropdownBtn = document.querySelectorAll(".dd_btn");
const backBtn = document.querySelectorAll(".dd .back_btn");
backBtn.forEach((btn) => {
btn.addEventListener("click", () => {
dropdownBtn.forEach((btn) => {
btn.classList.remove("active");
});
});
});
dropdownBtn.forEach((btn) => {
btn.addEventListener("click", () => {
if (btn.classList.contains("active")) {
btn.classList.remove("active");
} else {
btn.classList.add("active");
}
});
});
// ===========================================
// TOGGLE MENU
const menu = document.querySelector(".nav_menu");
function toggleMenu() {
if (menu.classList.contains("show") == true) {
menu.classList.remove("show");
dropdownBtn.forEach((btn) => {
btn.classList.remove("active");
});
} else {
menu.classList.add("show");
}
}
// ==========================================
// TOGGLE CART
const cart = document.querySelector(".cart")
function toggleCart() {
if (cart.classList.contains("show") == true) {
cart.classList.remove("show");
} else {
cart.classList.add("show");
}
}
// ==========================================
// MAKING CART WORK
if (document.readyState == 'loading') {
document.addEventListener('DOMContentLoaded', ready);
}
else {
ready();
}
function ready() {
updateTotal()
// REMOVE
const removeCartbtns = document.querySelectorAll('.cart-remove');
removeCartbtns.forEach(btn => {
btn.addEventListener('click', removeCartItem);
})
// QUANTITYCHANGE
const quantityInputs = document.querySelectorAll('.cart-quantity');
quantityInputs.forEach(count => {
count.addEventListener('change', quantityChanged);
})
const addtoCartbtns = document.querySelectorAll('.atc-btn');
addtoCartbtns.forEach(btn => {
btn.addEventListener('click', addtoCartClicked);
})
// BUY
// document.querySelector('.btn-buy').addEventListener('click',buy)
}
// ADD TO CART CLICKED ==================================
function addtoCartClicked(e) {
const parentEl = e.target.parentElement;
const title = parentEl.querySelector('.product-title').innerText;
const price = parentEl.querySelector('.price').innerText;
const img = parentEl.querySelector('.product-img').src;
addtoCart(title, price, img)
updateTotal();
}
function addtoCart(title, price, img) {
const cartItems = document.querySelector('.cart-items');
const itemNames = cartItems.querySelectorAll('.cart-product-title');
for (const element of itemNames) {
if (element.innerText == title) {
// alert("Product already present in the Cart");
iziToast.error({
message: 'This Product is already present in the Cart.',
});
return;
}
}
const cartBox = document.createElement('div');
cartBox.classList.add('cart-item');
cartBox.innerHTML = ` <img src="${img}" 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" min="1" class="cart-quantity">
</div>
<i class="ri-delete-bin-7-fill cart-remove"></i>`
cartItems.append(cartBox);
cartBox.querySelector('.cart-remove').addEventListener('click', removeCartItem);
cartBox.querySelector('.cart-quantity').addEventListener('click', quantityChanged);
// alert('Product added to the Cart')
iziToast.success({
title: 'Success!',
message: 'Added to the Cart.',
});
}
//REMOVE CART ITEM
function removeCartItem(e) {
e.target.parentElement.remove();
updateTotal();
}
// QUANTITY CHANGED
function quantityChanged(e) {
const input = e.target;
if (isNaN(input.value) || input.value <= 0) {
input.value = 1;
}
updateTotal();
}
// UPDATE AMOUNT
function updateTotal() {
const cartBoxes = document.querySelectorAll('.cart-item');
const totalPrice = document.querySelector('.total-price')
let total = 0
cartBoxes.forEach(item => {
const price = item.querySelector('.cart-price').textContent.slice(1)
const quantity = item.querySelector('.cart-quantity').value
total += price * quantity;
})
total = total.toFixed(2);
totalPrice.innerHTML = `$${total}`
}