Skip to content

Commit b24e2a0

Browse files
committed
bit of refactoring
1 parent de4510c commit b24e2a0

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

src/js/constants/selectors-map.ts

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const cart = {
4242
displayPromo: '.display-promo',
4343
promoCode: '#promo-code',
4444
deleteLinkAction: 'delete-from-cart',
45+
removeVoucherAction: 'remove-voucher',
4546
productQuantity: '.cart__items .js-quantity-button',
4647
productItem: '.cart__item',
4748
removeFromCartLink: '.remove-from-cart',

src/js/pages/cart.ts

+13-17
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,22 @@ export default () => {
1414

1515
if (cartSummary) {
1616
cartSummary.addEventListener('click', (event: Event) => {
17-
const target = event.target as HTMLElement;
18-
19-
// Check if the clicked element is a voucher code or inside one
20-
const voucher = target.closest(Theme.selectors.cart.discountCode);
21-
22-
if (!voucher || !isHTMLElement(voucher)) return;
23-
24-
event.stopPropagation();
17+
const eventTarget = event.target as HTMLElement;
18+
const voucherTarget = eventTarget.closest(Theme.selectors.cart.discountCode);
2519

26-
const discountInput = document.querySelector<HTMLInputElement>(Theme.selectors.cart.discountName);
27-
const promoCode = document.querySelector(Theme.selectors.cart.promoCode);
20+
if (voucherTarget && isHTMLElement(voucherTarget)) {
21+
const discountInput = document.querySelector<HTMLInputElement>(Theme.selectors.cart.discountName);
22+
const promoCode = document.querySelector(Theme.selectors.cart.promoCode);
2823

29-
if (promoCode && discountInput) {
30-
const formCollapser = new Collapse(promoCode, {
31-
toggle: false,
32-
});
24+
if (promoCode && discountInput) {
25+
const formCollapser = new Collapse(promoCode, {
26+
toggle: false,
27+
});
3328

34-
discountInput.value = voucher.innerText;
35-
// Show promo code field
36-
formCollapser.show();
29+
discountInput.value = voucherTarget.innerText;
30+
// Show promo code field
31+
formCollapser.show();
32+
}
3733
}
3834
});
3935
}

templates/checkout/_partials/cart-voucher.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<span class="cart-voucher__name col">{$voucher.name}</span>
1717
<span class="fw-bold">{$voucher.reduction_formatted}</span>
1818
{if isset($voucher.code) && $voucher.code !== ''}
19-
<form action="{$urls.pages.cart}?action=show" data-link-action="remove-voucher" class="d-flex" method="post">
19+
<form action="{$urls.pages.cart}?action=show" data-form-action="remove-voucher" class="d-flex" method="post">
2020
<input type="hidden" name="token" value="{$static_token}">
2121
<input type="hidden" name="deleteDiscount" value="{$voucher.id_cart_rule}">
2222
<button type="submit" class="btn btn-link ms-2"><span><i class="material-icons" title="{l s='Remove Voucher' d='Shop.Theme.Checkout'}">&#xE872;</i></span></button>

0 commit comments

Comments
 (0)