(function() { document.querySelectorAll('.pwyw-cart-input').forEach(function(input) { var debounce; input.addEventListener('change', function() { clearTimeout(debounce); var itemId = this.dataset.itemId; var dollars = parseFloat(this.value) || 0; var cents = Math.round(dollars * 100); debounce = setTimeout(function() { fetch('/api/cart/' + itemId, { method: 'PUT', headers: Object.assign({'Content-Type': 'application/json'}, csrfHeaders()), body: JSON.stringify({ amount_cents: cents }) }).then(function(r) { if (!r.ok) return r.json().then(function(d) { showToast(d.error || 'Invalid amount'); }); }).catch(function() { showToast('Failed to update amount'); }); }, 300); }); }); })(); window.removeCartGroup = function(btn) { var name = btn.dataset.sellerName || 'this creator'; if (!confirm('Remove all items from ' + name + '?')) return; var group = btn.closest('.cart-group'); if (!group) return; var rows = group.querySelectorAll('tr[id^="cart-row-"]'); if (rows.length === 0) return; btn.disabled = true; var headers = csrfHeaders(); var pending = rows.length; var failed = false; function finish() { if (--pending !== 0) return; if (failed) { showToast('Some items could not be removed. Refreshing.'); window.location.reload(); } else { window.location.reload(); } } rows.forEach(function(row) { var id = row.id.replace('cart-row-', ''); fetch('/api/cart/' + id, { method: 'DELETE', headers: headers }) .then(function(r) { if (!r.ok) failed = true; }) .catch(function() { failed = true; }) .finally(finish); }); };