Skip to content

Commit 7097f57

Browse files
committed
fix: fast double click removal
1 parent 0e9a5a8 commit 7097f57

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/js/components/useQuantityInput.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ const updateQuantity = async (qtyInputGroup: Theme.QuantityInput.InputGroup, cha
145145
const baseValue = Number(qtyInput.getAttribute('value'));
146146
const quantity = targetValue - baseValue;
147147

148+
// Skip update if value is 0 and min is 1 (item is being removed)
149+
if (targetValue === 0 && qtyInput.getAttribute('min') === '1') {
150+
return;
151+
}
152+
148153
if (isValidInputNum(targetValue) && quantity !== 0) {
149154
const requestUrl = qtyInput.dataset.updateUrl;
150155

src/js/pages/cart.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ import {isHTMLElement} from '@helpers/typeguards';
88
import handleCartAction from '@js/components/UseHandleCartAction';
99
import SelectorsMap from '@constants/selectors-map';
1010
import {state, availableLastUpdateAction} from '@js/state';
11+
import debounce from '@helpers/debounce';
1112

1213
export default () => {
14+
// Debounced function to trigger remove action, preventing multiple rapid clicks
15+
const debouncedRemoveAction = debounce(async (...args) => {
16+
const removeButton = args[0] as HTMLElement;
17+
18+
if (removeButton) {
19+
removeButton.click();
20+
}
21+
}, 500);
22+
1323
// Event delegation for voucher code clicks
1424
const handleVoucherClick = (event: Event) => {
1525
event.stopPropagation();
@@ -50,15 +60,12 @@ export default () => {
5060
) as HTMLElement | null;
5161

5262
if (targetValue) {
53-
if (eventTarget.classList.contains('js-increment-button')) {
54-
if (targetValue.dataset.mode === 'confirmation' && Number(targetValue.value) < 1) {
55-
removeButton?.click();
56-
}
57-
}
63+
const isDecrement = eventTarget.classList.contains('js-decrement-button');
5864

59-
if (eventTarget.classList.contains('js-decrement-button')) {
60-
if (targetValue.value === '0' && targetValue.getAttribute('min') === '1') {
61-
removeButton?.click();
65+
// Debounce remove action to prevent multiple rapid clicks
66+
if (isDecrement && targetValue.value === '0' && targetValue.getAttribute('min') === '1') {
67+
if (removeButton) {
68+
debouncedRemoveAction(removeButton);
6269
}
6370
}
6471
}

0 commit comments

Comments
 (0)