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
|
/*
simple shopping cart example
*/
class ShoppingCart {
constructor(name, id) {
this.name = name;
this.cartID = id;
const accountForm = document.getElementById('account-form');
accountForm.style.display = 'none';
const shoppingPage = document.getElementById('shopping-page');
shoppingPage.style.display = 'unset';
document.getElementById('user-name').textContent = name;
[...document.getElementsByClassName('shopping-item')]
.forEach(item => {
item.addEventListener('click', (evt) => {
evt.preventDefault();
this.addToCart(item.dataset.item);
})
});
}
addToCart(item) {
const fd = new FormData();
fd.append("name", item);
fetch(`/cart/${this.cartID}/item`, { method: "POST", body: fd})
.then(r => r.json())
.then(order => this.updateOrder(order))
.catch(err => {
console.log(err);
alert(`Failed to add ${item} to cart`);
});
}
deleteCartItem(item) {
const fd = new FormData();
fd.append("name", item);
fetch(`/cart/${this.cartID}/item`, { method: "DELETE", body: fd})
.then(r => r.json())
.then(order => this.updateOrder(order))
.catch(err => {
console.log(err);
alert(`Failed to remove ${item} from cart`);
});
}
updateOrder(order) {
const cartListContainer = document.getElementById('cart-list-container');
if (order.items.length == 0)
cartListContainer.style.display = 'none';
else
{
cartListContainer.style.display = 'initial';
const cartList = document.getElementById('cart-list');
[...cartList.querySelectorAll('li:not(:first-child)')]
.forEach(li => li.remove());
const li = cartListContainer.querySelector('li');
order.items.forEach(item => {
const lic = li.cloneNode(true);
lic.querySelector('span.text-placeholder').textContent = `${item.name} (${item.count})`;
const removeBtn = lic.querySelector('span.cart-item');
removeBtn.addEventListener('click', (li) => this.deleteCartItem(item.name));
cartList.append(lic);
});
}
console.log(order);
}
};
window.addEventListener('load', () => {
const createCartBtn = document.getElementById('create-cart-btn');
createCartBtn.addEventListener('click', (evt) => {
evt.preventDefault();
const client = document.forms['client-form']['client-name'].value;
if (client == "")
alert("Please enter a user name");
else
{
fetch('/cart', { method: "POST" })
.then(r => r.json())
.then(cartID => new ShoppingCart(client, cartID))
.catch(err => {
console.log(err);
alert("failed to create shopping cart");
})
}
});
});
|