-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
198 lines (163 loc) · 6.66 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Cakes toggle ingredients functionality
document.addEventListener("DOMContentLoaded", function () {
const cakes = document.querySelectorAll('.cakes');
cakes.forEach(cake => {
cake.addEventListener('click', function () {
const ingredients = cake.querySelector('.ingredients');
// Toggle visibility of ingredients
if (ingredients.style.display === "none" || ingredients.style.display === "") {
ingredients.style.display = "block";
} else {
ingredients.style.display = "none";
}
});
});
});
// Slider functionality
const slides = document.querySelector('.slides');
const slideCount = document.querySelectorAll('.slide').length;
const dots = document.querySelectorAll('.dot');
let currentIndex = 0;
// Ensure the slides element exists before applying slider logic
if (slides) {
function showNextSlide() {
currentIndex = (currentIndex + 1) % slideCount;
updateSlider();
}
function updateSlider() {
slides.style.transform = `translateX(${-currentIndex * 100}%)`; // Corrected transform syntax
dots.forEach((dot, index) => {
dot.classList.toggle('active', index === currentIndex);
});
}
setInterval(showNextSlide, 3000);
dots.forEach((dot, index) => {
dot.addEventListener('click', () => {
currentIndex = index;
updateSlider();
});
});
} else {
console.error("Slides container not found.");
}
// Category Dropdown Redirect functionality
const categorySelect = document.getElementById('category-select');
if (categorySelect) {
categorySelect.addEventListener('change', function () {
const selectedValue = this.value;
if (selectedValue) {
window.location.href = selectedValue;
}
});
}
// Custom Cake Form Submission
const customCakeForm = document.getElementById('custom-cake-form');
if (customCakeForm) {
customCakeForm.addEventListener('submit', function (event) {
event.preventDefault();
const flavor = document.getElementById('flavor').value;
const size = document.getElementById('size').value;
const theme = document.getElementById('theme').value;
const message = document.getElementById('message').value;
const notes = document.getElementById('notes').value;
// Basic validation
if (!flavor || !size || !theme || !message) {
alert('Please fill in all required fields.');
return;
}
// Feedback message before redirection
alert('Your custom cake order has been submitted!');
// Store the data in localStorage with error handling
try {
localStorage.setItem('customCakeOrder', JSON.stringify({ flavor, size, theme, message, notes }));
} catch (e) {
console.error('Error saving data to localStorage:', e);
}
// Redirect to the submission confirmation page
window.location.href = 'submit-order.html';
});
}
// Add to Cart functionality
const addToCartButtons = document.querySelectorAll('.btn1, .btn2'); // Selecting either .btn1 or .btn2
addToCartButtons.forEach(button => {
button.addEventListener('click', function () {
const productElement = this.closest('.cakes');
const productName = productElement.querySelector('.text p').textContent.trim();
const productPrice = parseFloat(productElement.querySelector('.text p:nth-child(2)').textContent.replace('₹', '').trim());
const productImage = productElement.querySelector('img').src;
const cartItem = {
name: productName,
price: productPrice,
imgSrc: productImage,
quantity: 1
};
// Add item to cart or update quantity if it already exists
let cart = JSON.parse(localStorage.getItem('cart')) || [];
const existingItemIndex = cart.findIndex(item => item.name === cartItem.name);
if (existingItemIndex !== -1) {
cart[existingItemIndex].quantity += 1;
} else {
cart.push(cartItem);
}
// Save updated cart to localStorage with error handling
try {
localStorage.setItem('cart', JSON.stringify(cart));
alert(`${productName} added to cart!`);
} catch (e) {
console.error('Error saving cart to localStorage:', e);
}
});
});
// Cart display logic
const cartItems = JSON.parse(localStorage.getItem('cart')) || [];
const cartContainer = document.getElementById('cart-items');
const totalPriceElement = document.getElementById('total-price');
// Function to update the total price
function updateTotalPrice() {
const totalPrice = cartItems.reduce((acc, item) => acc + item.price * item.quantity, 0);
if (totalPriceElement) {
totalPriceElement.textContent = `Total: ₹${totalPrice}`;
}
}
// Function to render the cart items
function renderCartItems() {
if (!cartContainer) return; // Ensure cartContainer exists
cartContainer.innerHTML = '';
if (cartItems.length === 0) {
const emptyMessage = document.createElement('p');
emptyMessage.textContent = 'Your cart is empty';
cartContainer.appendChild(emptyMessage);
return;
}
cartItems.forEach(item => {
const cartItemElement = document.createElement('div');
cartItemElement.classList.add('cart-item');
const img = document.createElement('img');
img.classList.add('cart-item-img');
img.src = item.imgSrc;
img.alt = item.name;
const name = document.createElement('p');
name.textContent = item.name;
const price = document.createElement('p');
price.textContent = `₹${item.price}`;
const quantity = document.createElement('p');
quantity.textContent = `Quantity: ${item.quantity}`;
cartItemElement.appendChild(img);
cartItemElement.appendChild(name);
cartItemElement.appendChild(price);
cartItemElement.appendChild(quantity);
cartContainer.appendChild(cartItemElement);
});
updateTotalPrice();
}
// Initialize the cart on page load
renderCartItems();
// Checkout button functionality
const checkoutButton = document.getElementById('checkout');
if (checkoutButton) {
checkoutButton.addEventListener('click', () => {
alert('Thank you for your purchase!');
localStorage.removeItem('cart');
window.location.href = 'index.html';
});
}