Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: when you edit the product qty to zero, the product won't be removed from the cart #542

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/js/pages/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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);

voucherCodes.forEach((voucher) => {
voucher.addEventListener('click', (event: Event) => {
Expand All @@ -33,12 +34,32 @@ export default () => {
});
});

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

if (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();
}
}
}

if (eventTarget.classList.contains('js-decrement-button')) {
if (targetValue.getAttribute('value') === '1' && targetValue.getAttribute('min') === '1') {
if (removeButton) {
removeButton.click();
}
}
}
}
Comment on lines +45 to +61
Copy link
Contributor

@davidglezz davidglezz Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

The code can be simplified, the first if avoid 2 ifs.

All could go in an single if but I wouldn't know how readable it would be.


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