File: shop-2.js

package info (click to toggle)
libzeep 5.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,596 kB
  • sloc: cpp: 27,393; xml: 7,798; javascript: 180; sh: 37; makefile: 8
file content (121 lines) | stat: -rw-r--r-- 3,060 bytes parent folder | download | duplicates (3)
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
/*
	simple shopping cart example
*/

class ShoppingCart {
	constructor(cart) {
		this.cartContent = cart;
		this.cartContent.items = [];

		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 ix = this.cartContent.items.findIndex((i) => i.name === item);
		if (ix < 0)
			this.cartContent.items.push({ name: item, count: 1 });
		else
			this.cartContent.items[ix].count += 1;
		this.updateOrder();
	}

	deleteCartItem(item) {
		const ix = this.cartContent.items.findIndex((i) => i.name === item);
		if (ix < 0)
			this.cartContent.items.push({ name: item, count: 1 });
		else if (this.cartContent.items[ix].count == 1)
			this.cartContent.items.splice(ix, 1);
		else
			this.cartContent.items[ix].count -= 1;

		this.updateOrder();
	}

	updateOrder() {
		fetch(`/cart/${this.cartContent.cartID}`, {
			method: "PUT",
			headers: { 'Content-Type': 'application/json' },
			body: JSON.stringify(this.cartContent)
		})
		.then(r => {
			if (! r.ok)
				throw 'error';

			const cartListContainer = document.getElementById('cart-list-container');
			
			if (this.cartContent.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');

				this.cartContent.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);
				});
			}
		})
		.catch(err => {
			console.log(err);
			alert('Failed to add item to cart');
		});
	}
};

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
		{
			const newCart = {
				client: client
			};

			fetch('/cart', {
					method: "POST",
					headers: {
						'Content-Type': 'application/json'
					},
					body: JSON.stringify(newCart)
				})
				.then(r => r.json())
				.then(cartID => {
					newCart.cartID = cartID;
					new ShoppingCart(newCart);
				})
				.catch(err => {
					console.log(err);
					alert("failed to create shopping cart");
				})
		}
	});
});