Skip to content

Commit

Permalink
fix: input change on cart qty below 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tblivet committed Aug 31, 2023
1 parent 482bc85 commit fd55b60
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/js/pages/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,13 @@
import {Collapse} from 'bootstrap';
import {isHTMLElement} from '@helpers/typeguards';
import handleCartAction from '../components/UseHandleCartAction';
import { qtyInput } from '@constants/selectors-map';

Check failure on line 9 in src/js/pages/cart.ts

View workflow job for this annotation

GitHub Actions / ESLint

`@constants/selectors-map` import should occur before import of `../components/UseHandleCartAction`

Check failure on line 9 in src/js/pages/cart.ts

View workflow job for this annotation

GitHub Actions / ESLint

There should be no space after '{'

Check warning on line 9 in src/js/pages/cart.ts

View workflow job for this annotation

GitHub Actions / ESLint

'qtyInput' is defined but never used

Check failure on line 9 in src/js/pages/cart.ts

View workflow job for this annotation

GitHub Actions / ESLint

There should be no space before '}'

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();
Expand All @@ -56,9 +36,32 @@ export default () => {
});

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

const targetItem = eventTarget.closest('.cart__item');
const targetValue = targetItem?.querySelector('.js-cart-line-product-quantity') as HTMLInputElement | null;
const removeButton = targetItem?.querySelector('.remove-from-cart') as HTMLElement | null;

if (targetValue) {
if (eventTarget.classList.contains('js-increment-button')) {
if (targetValue.dataset.mode === 'confirmation' && Number(targetValue.value) < 1) {
if (removeButton) {
removeButton.click();

Check failure on line 51 in src/js/pages/cart.ts

View workflow job for this annotation

GitHub Actions / ESLint

Expected indentation of 14 spaces but found 13
}
}
}

if (eventTarget.classList.contains('js-decrement-button')) {
if (targetValue.getAttribute('value') === '1' && targetValue.getAttribute('min') === '1') {
if (removeButton) {
removeButton.click();
}
}
}
}

if (eventTarget.dataset.linkAction === Theme.selectors.cart.deleteLinkAction) {
handleCartAction(event);
}
Expand Down

0 comments on commit fd55b60

Please sign in to comment.