-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (121 loc) · 4.77 KB
/
index.js
File metadata and controls
143 lines (121 loc) · 4.77 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
const CURRENCY_SYMBOL = '₹'; // Currency symbol for formatting
// Utility function to format currency
function formatCurrency(amount) {
return `${CURRENCY_SYMBOL}${amount.toFixed(2)}`;
}
// Initialize cart from localStorage or default to an empty object
let cart = JSON.parse(localStorage.getItem('cart')) || {};
let cartItemCount = Object.values(cart).reduce((acc, item) => acc + item.quantity, 0);
// Update cart count on page load
const cartCountElem = document.getElementById('cartCount');
if (cartCountElem) {
cartCountElem.textContent = cartItemCount;
}
// Show selected section (Products or Cart)
function showSection(sectionId, event) {
if (event) event.preventDefault(); // Prevent default anchor behavior
document.querySelectorAll('section').forEach(section => {
section.style.display = 'none';
});
const selectedSection = document.getElementById(sectionId);
if (selectedSection) {
selectedSection.style.display = sectionId === 'cart' ? 'block' : 'grid';
if (sectionId === 'cart') updateCartPage();
}
}
// Go Home (show home section dynamically without reloading the page)
function goHome(event) {
event.preventDefault(); // Prevent default behavior of the Home link
showSection('hero', event); // Show the home section
if (cartCountElem) {
cartCountElem.textContent = cartItemCount; // Update cart count dynamically
}
}
// Add item to Cart
function addToCart(productName, productPrice) {
if (cart[productName]) {
cart[productName].quantity++;
cart[productName].total = cart[productName].quantity * productPrice;
} else {
cart[productName] = {
price: productPrice,
quantity: 1,
total: productPrice,
};
}
cartItemCount++;
if (cartCountElem) {
cartCountElem.textContent = cartItemCount;
}
localStorage.setItem('cart', JSON.stringify(cart)); // Save cart to localStorage
showSection('cart'); // Show the cart page after adding
alert(`${productName} has been added to your cart!`);
}
// Update Cart Page with items
function updateCartPage() {
const cartItemsElem = document.getElementById('cartItems');
if (!cartItemsElem) return;
cartItemsElem.innerHTML = ''; // Clear the cart items list
let totalAmount = 0;
for (const [productName, productDetails] of Object.entries(cart)) {
totalAmount += productDetails.total;
const row = document.createElement('tr');
row.innerHTML = `
<td>${productName}</td>
<td>${formatCurrency(productDetails.price)}</td>
<td>${productDetails.quantity}</td>
<td>${formatCurrency(productDetails.total)}</td>
<td><button class="remove-btn" onclick="removeFromCart('${productName}')">Remove</button></td>
`;
cartItemsElem.appendChild(row);
}
const cartTotalElem = document.getElementById('cartTotal');
if (cartTotalElem) {
cartTotalElem.textContent = formatCurrency(totalAmount);
}
}
// Remove item from Cart
function removeFromCart(productName) {
cartItemCount -= cart[productName].quantity;
delete cart[productName];
if (cartCountElem) {
cartCountElem.textContent = cartItemCount;
}
localStorage.setItem('cart', JSON.stringify(cart)); // Update localStorage
updateCartPage(); // Update cart page after removal
}
// Clear Cart
function clearCart() {
if (confirm('Are you sure you want to clear the cart?')) {
cart = {}; // Empty the cart
cartItemCount = 0;
if (cartCountElem) {
cartCountElem.textContent = cartItemCount;
}
localStorage.removeItem('cart'); // Remove cart data from localStorage
updateCartPage(); // Update cart page
}
}
// Debounced Search and Filter Products
let debounceTimer;
function filterProducts() {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
const searchTerm = document.getElementById('searchInput').value.toLowerCase();
const products = document.querySelectorAll('.product');
products.forEach(product => {
const productName = product.querySelector('h3').textContent.toLowerCase();
product.style.display = productName.includes(searchTerm) ? '' : 'none';
});
}, 300); // Delay for 300ms
}
// Placeholder for search functionality
function performSearch() {
const searchTerm = document.getElementById('searchInput').value.toLowerCase();
alert(`Searching for: ${searchTerm}`);
// Implement actual search logic here
}
// Display initial cart state
document.addEventListener('DOMContentLoaded', () => {
updateCartPage();
});