Skip to content

Commit

Permalink
fix: delete cart product when click on minus
Browse files Browse the repository at this point in the history
  • Loading branch information
tblivet committed Aug 10, 2023
1 parent 34fe1bc commit 482bc85
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/js/pages/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ 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) => {
Expand All @@ -33,8 +55,6 @@ export default () => {
});
});

const cartContainer = document.querySelector<HTMLElement>(Theme.selectors.cart.container);

if (cartContainer) {
cartContainer.addEventListener('click', (event: Event) => {
const eventTarget = event.target as HTMLElement;
Expand Down

0 comments on commit 482bc85

Please sign in to comment.