-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
205 lines (157 loc) · 6.16 KB
/
script.js
File metadata and controls
205 lines (157 loc) · 6.16 KB
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
const decreaseButtons = document.querySelectorAll('.decrease');
const increaseButtons = document.querySelectorAll('.increase');
const addToCartButtons = document.querySelectorAll('.add-to-cart> button');
const cartButton = document.querySelector('#cart');
cartButton.addEventListener('click', () => {
showCart();
});
document.querySelector('#close-cart').addEventListener('click', closeCart);
document.querySelector('.overlay').addEventListener('click', closeCart);
decreaseButtons.forEach((button) => {
button.addEventListener('click', (event) => {
const productId = event.target.parentNode.parentNode.parentNode.parentNode.id;
changeQuantity(productId, 'decrease');
});
});
increaseButtons.forEach((button) => {
button.addEventListener('click', (event) => {
const productId = event.target.parentNode.parentNode.parentNode.parentNode.id;
changeQuantity(productId, 'increase');
});
});
addToCartButtons.forEach((button) => {
button.addEventListener('click', (event) => {
const productId = event.target.parentNode.parentNode.parentNode.id;
addToCart(productId);
});
});
//decrease or increase quantity of choc0late when clicked on add or sub button
function changeQuantity(id, action) {
var quantity = document.querySelector(`#${id} .change-quantity input`).value;
if (action == "decrease") {
if (quantity > 1) {
quantity--;
}
} else if (action == "increase") {
quantity++;
}
document.querySelector(`#${id} .change-quantity input`).value = quantity;
}
//adding product to pack
function addToCart(id) {
const name = document.querySelector(`#${id} .product-desc h3`).innerText;
const price = document.querySelector(`#${id} .product-desc p`).innerText;
const quantity = document.querySelector(`#${id} .change-quantity input`).value;
document.querySelector(`#${id} .change-quantity input`).value = 1;
if (!checkCart(quantity)) {
return;
}
const productData = {
id: id,
quantity: quantity,
price: price,
name: name,
}
const cart = JSON.parse(localStorage.getItem('cart')) || [];
const existingProduct = cart.find((product) => {
return product.id == id;
});
if (existingProduct) {
existingProduct.quantity = parseInt(existingProduct.quantity) + parseInt(quantity);
} else {
cart.push(productData);
}
localStorage.setItem('cart', JSON.stringify(cart));
updateCartSize();
notification("Product added to cart");
}
function updateCartSize() {
const cart = JSON.parse(localStorage.getItem('cart')) || [];
let totalQuantity = 0;
cart.forEach((product) => {
totalQuantity += parseInt(product.quantity);
});
document.querySelector('#cart p').innerText = totalQuantity;
}
//checking at every any point the quantity of every product in pack added can not be more than 8
function checkCart(currentProductQuantity) {
const cart = JSON.parse(localStorage.getItem('cart')) || [];
let totalQuantity = 0;
cart.forEach((product) => {
totalQuantity += parseInt(product.quantity);
});
if (totalQuantity + parseInt(currentProductQuantity) > 8) {
notification('You can not add more than 8 products to cart');
return false;
}
return true;
}
//show cart items in a modal
function showCart() {
const cart = JSON.parse(localStorage.getItem('cart')) || [];
const productContainer = document.querySelector('.cart-container .product-container');
let total = 0;
productContainer.innerHTML = '';
cart.forEach((product) => {
productContainer.innerHTML += `
<div class="cart-product"">
<h3>${product.name}</h3>
<p>${product.quantity}</p>
<p>${product.price}</p>
<button class="remove-from-cart" data-productid="${product.id}" ><i class="fa-solid fa-trash"></i></button>
</div>
`;
total += parseInt(product.price.substring(1)) * parseInt(product.quantity);
});
const totalContainer = document.querySelector('.cart-container .checkout h3');
totalContainer.innerText = `Total: ₹${total}`;
const cartContainer = document.querySelector('.cart-container');
cartContainer.style.display = "flex";
document.querySelector('body').style.overflow = "hidden";
//add event listener to remove button of every product in pack
const removeButtons = document.querySelectorAll('.remove-from-cart i');
removeButtons.forEach((button) => {
button.addEventListener('click', (event) => {
const productId = event.target.parentNode.dataset.productid;
removeFromCart(productId, event.target.parentNode.parentNode);
});
});
}
function closeCart(){
const cartContainer = document.querySelector('.cart-container');
cartContainer.style.display = "none";
document.querySelector('body').style.overflow = "auto";
}
//remove product from cart
function removeFromCart(id, productContainer) {
const cart = JSON.parse(localStorage.getItem('cart')) || [];
const updatedCart = cart.filter((product) => {
return product.id != id;
});
localStorage.setItem('cart', JSON.stringify(updatedCart));
productContainer.remove();
let total = 0;
updatedCart.forEach((product) => {
total += parseInt(product.price.substring(1)) * parseInt(product.quantity);
});
const totalContainer = document.querySelector('.cart-container .checkout h3');
totalContainer.innerText = `Total: ₹${total}`;
updateCartSize();
notification("Product removed from cart");
}
function notification(msg) {
const notification = document.querySelector('#notify');
notification.innerHTML = `<p>${msg}</p>`;
notification.style.right = "0%";
setTimeout(() => {
notification.style.right = "-100%";
}, 3000);
}
updateCartSize();
const nav = document.querySelector('nav');
nav.addEventListener('mouseenter', function(){
cursor.style.transform = "translate(-50%, -50%) scale(2)";
})
nav.addEventListener('mouseleave', function(){
cursor.style.transform = "translate(-50%, -50%) scale(1)";
})