Skip to main content

max / makenotwork

1.2 KB · 32 lines History Blame Raw
1 (function() {
2 window.toggleWishlist = function(itemId) {
3 var btn = document.getElementById('wishlist-btn');
4 fetch('/api/wishlists/' + itemId, { method: 'POST', headers: csrfHeaders() })
5 .then(function(r) { return r.json(); })
6 .then(function(data) {
7 if (data.wishlisted) {
8 btn.textContent = 'Wishlisted';
9 } else {
10 btn.textContent = 'Add to Wishlist';
11 }
12 });
13 };
14
15 window.toggleCart = function(itemId) {
16 var btn = document.getElementById('cart-btn');
17 fetch('/api/cart/' + itemId, { method: 'POST', headers: csrfHeaders() })
18 .then(function(r) { return r.json(); })
19 .then(function(data) {
20 if (data.in_cart) {
21 btn.textContent = 'In Cart';
22 showToast('Added to cart. Buying multiple items together saves the creator on processing fees.', 'info');
23 } else {
24 btn.textContent = 'Add to Cart';
25 }
26 })
27 .catch(function(err) {
28 showToast(err.message || 'Failed to update cart');
29 });
30 };
31 })();
32