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
67 lines (54 loc) · 2.18 KB
/
cart.ts
File metadata and controls
67 lines (54 loc) · 2.18 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
/**
* 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 '../components/UseHandleCartAction';
export default () => {
const {Theme} = window;
const voucherCodes = document.querySelectorAll(Theme.selectors.cart.discountCode);
const cartContainer = document.querySelector<HTMLElement>(Theme.selectors.cart.container);
if (cartContainer) {
cartContainer.addEventListener('click', (event: Event) => {
const eventTarget = event.target as HTMLElement;
if (eventTarget.classList.contains('js-decrement-button')) {
const targetItem = eventTarget.closest('.cart__item');
const targetValue = targetItem?.querySelector('.js-cart-line-product-quantity') as HTMLElement | null;
if (targetValue && targetValue.getAttribute('value') === '1' && targetValue.getAttribute('min') === '1') {
if (targetItem) {
const removeButton = targetItem.querySelector('.remove-from-cart') as HTMLElement | null;
if (removeButton) {
removeButton.click();
}
}
}
}
});
}
voucherCodes.forEach((voucher) => {
voucher.addEventListener('click', (event: Event) => {
event.stopPropagation();
if (isHTMLElement(event.currentTarget)) {
const code = event.currentTarget;
const discountInput = document.querySelector<HTMLInputElement>(Theme.selectors.cart.discountName);
const promoCode = document.querySelector(Theme.selectors.cart.promoCode);
if (promoCode && discountInput) {
const formCollapser = new Collapse(promoCode);
discountInput.value = code.innerText;
// Show promo code field
formCollapser.show();
}
}
return false;
});
});
if (cartContainer) {
cartContainer.addEventListener('click', (event: Event) => {
const eventTarget = event.target as HTMLElement;
if (eventTarget.dataset.linkAction === Theme.selectors.cart.deleteLinkAction) {
handleCartAction(event);
}
});
}
};