forked from PrestaShop/hummingbird
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.ts
More file actions
151 lines (123 loc) · 5.3 KB
/
cart.ts
File metadata and controls
151 lines (123 loc) · 5.3 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import {Collapse} from 'bootstrap';
import {isHTMLElement} from '@helpers/typeguards';
import handleCartAction from '@js/components/UseHandleCartAction';
import SelectorsMap from '@constants/selectors-map';
import {state, availableLastUpdateAction} from '@js/state';
import debounce from '@helpers/debounce';
export default () => {
// Debounced function to trigger remove action, preventing multiple rapid clicks
const debouncedRemoveAction = debounce(async (...args) => {
const removeButton = args[0] as HTMLElement;
if (removeButton) {
removeButton.click();
}
}, 500);
// Event delegation for voucher code clicks
const handleVoucherClick = (event: Event) => {
event.stopPropagation();
const voucher = event.target as HTMLElement;
if (isHTMLElement(voucher) && voucher.matches(SelectorsMap.cart.voucherCode)) {
const code = voucher;
const voucherInput = document.querySelector<HTMLInputElement>(SelectorsMap.cart.voucherInput);
const voucherAccordion = document.querySelector(SelectorsMap.cart.voucherAccordion);
if (voucherAccordion && voucherInput) {
const voucherAccordionCollapser = new Collapse(voucherAccordion, {
toggle: false,
});
voucherInput.value = code.innerText;
voucherAccordionCollapser.show();
}
}
};
// Event delegation for voucher form submit
const handleVoucherSubmit = () => {
state.set('lastUpdateAction', availableLastUpdateAction.SUBMIT_VOUCHER);
};
// Event delegation for cart container (handles item removal, quantity change, etc.)
const handleCartContainerClick = (event: Event) => {
const eventTarget = event.target as HTMLElement;
const targetItem = eventTarget.closest(SelectorsMap.cart.productItem);
const targetValue = targetItem?.querySelector(
SelectorsMap.cart.productItemQuantityInput,
) as HTMLInputElement | null;
const removeButton = targetItem?.querySelector(
SelectorsMap.cart.removeFromCart,
) as HTMLElement | null;
if (targetValue) {
const isDecrement = eventTarget.classList.contains('js-decrement-button');
// Debounce remove action to prevent multiple rapid clicks
if (isDecrement && targetValue.value === '0' && targetValue.getAttribute('min') === '1') {
if (removeButton) {
debouncedRemoveAction(removeButton);
}
}
}
// If it's a delete action, trigger the cart action
if (eventTarget.dataset.linkAction === SelectorsMap.cart.deleteLinkAction) {
handleCartAction(event);
}
};
// Event delegation for summary container (handles voucher removal)
const handleSummaryContainerClick = (event: Event) => {
const target = (event.target as HTMLElement).closest(
`[data-link-action="${SelectorsMap.cart.removeVoucherLinkAction}"]`,
);
if (target) {
// Set state.lastUpdateAction to track the last update action
state.set('lastUpdateAction', availableLastUpdateAction.REMOVE_VOUCHER);
event.preventDefault();
const syntheticClick = new Event('click', {bubbles: true});
Object.defineProperty(syntheticClick, 'target', {value: target});
handleCartAction(syntheticClick);
}
};
// Add event listeners for dynamic elements
const attachEventListeners = () => {
const voucherCodes = document.querySelectorAll(SelectorsMap.cart.voucherCode);
const cartContainer = document.querySelector<HTMLElement>(SelectorsMap.cart.container);
const cartSummaryContainer = document.querySelector<HTMLElement>(SelectorsMap.cart.summaryContainer);
const checkoutSummaryContainer = document.querySelector<HTMLElement>(SelectorsMap.checkout.summaryContainer);
const voucherForm = document.querySelector<HTMLFormElement>(SelectorsMap.cart.voucherForm);
// Add click listener for voucher codes
voucherCodes.forEach((voucher) => {
voucher.addEventListener('click', handleVoucherClick);
});
// Add submit listener for voucher form
if (voucherForm) {
voucherForm.addEventListener('submit', handleVoucherSubmit);
}
// Add click listener for the cart container
if (cartContainer) {
cartContainer.addEventListener('click', handleCartContainerClick);
}
// Add click listener for both summary containers
[cartSummaryContainer, checkoutSummaryContainer].forEach((container) => {
if (container) {
container.addEventListener('click', handleSummaryContainerClick);
}
});
};
// MutationObserver to handle dynamic updates (e.g., when cart is updated or refreshed)
const setupMutationObserver = () => {
const cartSummaryContainer = document.querySelector<HTMLElement>(SelectorsMap.cart.summaryContainer);
const checkoutSummaryContainer = document.querySelector<HTMLElement>(SelectorsMap.checkout.summaryContainer);
const observer = new MutationObserver(() => {
attachEventListeners();
});
[cartSummaryContainer, checkoutSummaryContainer].forEach((container) => {
if (container) {
observer.observe(container, {
childList: true,
subtree: true,
});
}
});
};
// Initialize event listeners and mutation observer
attachEventListeners();
setupMutationObserver();
};