-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
100 lines (88 loc) · 3.47 KB
/
cart.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
import { getCartItems, updateCartItemQuantity, removeFromCart } from './cart-service.js';
document.addEventListener('DOMContentLoaded', async () => {
try {
await loadCartItems();
} catch (error) {
console.error('Error loading cart:', error);
// Show error message to user
alert('Error loading cart items. Please try again later.');
}
});
async function loadCartItems() {
try {
const cartItems = await getCartItems();
displayCartItems(cartItems);
updateCartTotals(cartItems);
} catch (error) {
throw error;
}
}
function displayCartItems(items) {
const cartItemsContainer = document.getElementById('cart-items');
cartItemsContainer.innerHTML = '';
if (items.length === 0) {
cartItemsContainer.innerHTML = `
<tr>
<td colspan="6" style="text-align: center;">Your cart is empty</td>
</tr>
`;
return;
}
items.forEach(item => {
const row = document.createElement('tr');
row.innerHTML = `
<td><a href="#" class="remove-item" data-id="${item.id}"><i class="far fa-times-circle"></i></a></td>
<td><img src="${item.product.imagePath}" alt="${item.product.name}"></td>
<td>${item.product.name}</td>
<td>$ ${item.product.price.toFixed(2)}</td>
<td>
<input type="number" value="${item.quantity}" min="1"
class="quantity-input" data-id="${item.id}">
</td>
<td>$ ${(item.quantity * item.product.price).toFixed(2)}</td>
`;
cartItemsContainer.appendChild(row);
});
// Add event listeners for quantity changes and remove buttons
addCartEventListeners();
}
function updateCartTotals(items) {
const subtotal = items.reduce((sum, item) => sum + (item.quantity * item.product.price), 0);
document.getElementById('cart-subtotal').textContent = `$ ${subtotal.toFixed(2)}`;
document.getElementById('cart-total').textContent = `$ ${subtotal.toFixed(2)}`;
}
function addCartEventListeners() {
// Handle quantity changes
document.querySelectorAll('.quantity-input').forEach(input => {
input.addEventListener('change', async (e) => {
const cartItemId = e.target.dataset.id;
const newQuantity = parseInt(e.target.value);
if (newQuantity < 1) {
e.target.value = 1;
return;
}
try {
await updateCartItemQuantity(cartItemId, newQuantity);
await loadCartItems(); // Reload cart to update totals
} catch (error) {
console.error('Error updating quantity:', error);
alert('Error updating quantity. Please try again.');
await loadCartItems(); // Reload to reset to previous state
}
});
});
// Handle remove item clicks
document.querySelectorAll('.remove-item').forEach(button => {
button.addEventListener('click', async (e) => {
e.preventDefault();
const cartItemId = e.target.closest('.remove-item').dataset.id;
try {
await removeFromCart(cartItemId);
await loadCartItems(); // Reload cart after removing item
} catch (error) {
console.error('Error removing item:', error);
alert('Error removing item. Please try again.');
}
});
});
}