-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
108 lines (97 loc) · 3.19 KB
/
script.js
File metadata and controls
108 lines (97 loc) · 3.19 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
// Cart functionality with SessionStorage
const CART_STORAGE_KEY = 'cartItems';
// Initialize cart from sessionStorage
let cart = JSON.parse(sessionStorage.getItem(CART_STORAGE_KEY) || '[]');
// Add to Cart functionality
document.querySelectorAll('.add-to-cart-btn').forEach(button => {
button.addEventListener('click', () => {
const product = {
name: button.dataset.product,
price: parseFloat(button.dataset.price)
};
cart.push(product);
sessionStorage.setItem(CART_STORAGE_KEY, JSON.stringify(cart));
alert('Item added to the cart');
updateCartDisplay();
});
});
// View Cart functionality
document.getElementById('viewCartBtn')?.addEventListener('click', () => {
const modal = document.getElementById('cartModal');
cart = JSON.parse(sessionStorage.getItem(CART_STORAGE_KEY) || '[]');
updateCartDisplay();
modal.style.display = 'block';
});
// Clear Cart functionality
document.getElementById('clearCartBtn')?.addEventListener('click', () => {
if (cart.length === 0) {
alert('No items to clear');
} else {
cart = [];
sessionStorage.removeItem(CART_STORAGE_KEY);
alert('Cart cleared');
updateCartDisplay();
document.getElementById('cartModal').style.display = 'none';
}
});
// Process Order functionality
document.getElementById('processOrderBtn')?.addEventListener('click', () => {
if (cart.length === 0) {
alert('Cart is empty');
} else {
alert('Thank you for your order');
cart = [];
sessionStorage.removeItem(CART_STORAGE_KEY);
updateCartDisplay();
document.getElementById('cartModal').style.display = 'none';
}
});
// Update Cart Display
function updateCartDisplay() {
const cartItems = document.getElementById('cartItems');
if (cartItems) {
cartItems.innerHTML = '';
cart.forEach(item => {
cartItems.innerHTML += `<p>${item.name} - $${item.price}</p>`;
});
const total = cart.reduce((sum, item) => sum + item.price, 0);
document.getElementById('cartTotal').textContent = total.toFixed(2);
}
}
// Newsletter subscription functionality
document.querySelectorAll('.newsletter-form').forEach(form => {
form.addEventListener('submit', (e) => {
e.preventDefault();
const emailInput = form.querySelector('input[type="email"]');
if (emailInput.value) {
alert('Thank you for subscribing');
emailInput.value = '';
}
});
});
// Contact form with localStorage
const contactForm = document.getElementById('contactForm');
if (contactForm) {
contactForm.addEventListener('submit', (e) => {
e.preventDefault();
const formData = {
name: contactForm.name.value,
email: contactForm.email.value,
message: contactForm.message.value,
timestamp: new Date().toISOString()
};
// Save to localStorage
const contacts = JSON.parse(localStorage.getItem('contacts') || '[]');
contacts.push(formData);
localStorage.setItem('contacts', JSON.stringify(contacts));
alert('Thank you for your message');
contactForm.reset();
});
}
// Close modal when clicking outside
window.addEventListener('click', (event) => {
const modal = document.getElementById('cartModal');
if (event.target === modal) {
modal.style.display = 'none';
}
});