Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
YSHOP2-726 : themes > prevent opening cart drawer on cart page (#374)
Browse files Browse the repository at this point in the history
## JIRA Ticket

[YSHOP2-726](https://youcanshop.atlassian.net/browse/YSHOP2-726)

## QA Steps

- [ ] `pnpm i`
- [ ] `pnpm dev`

## Note

Leave empty when you have nothing to say


[YSHOP2-726]:
https://youcanshop.atlassian.net/browse/YSHOP2-726?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
bj-anas authored Jul 25, 2023
1 parent 577a931 commit 0cb95b1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
14 changes: 13 additions & 1 deletion assets/add-to-cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,16 @@ document.addEventListener('DOMContentLoaded', async () => {
await updateCartDrawer();
});

window.updateCartDrawer = updateCartDrawer;
/**
* Remove the click event from the cart icon if the user is inside the cart page
*/
function preventCartDrawerOpening(templateName) {
if(templateName !== 'cart') {
return;
}

const cartDrawerIcon = document.querySelector('#navbar-cart-icon');

cartDrawerIcon.removeEventListener("click", toggleCartDrawer);
window.location.reload();
}
34 changes: 20 additions & 14 deletions assets/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ function setCurrencySymbol() {
element.innerText = currencyCode;
})
}
setCurrencySymbol();

const promo = document.forms['promo'];
if (promo) {
Expand Down Expand Up @@ -36,7 +35,7 @@ function updateCart(item, quantity, totalPriceSelector, cartItemId, productVaria
const increase = input.nextElementSibling;

const productPrice = inputHolder.querySelector('.product-price');
const price = Number(productPrice?.innerText);
const price = productPrice.innerText;
const totalPrice = inputHolder.querySelector(totalPriceSelector);

decrease
Expand All @@ -49,26 +48,32 @@ function updateCart(item, quantity, totalPriceSelector, cartItemId, productVaria
if (isNaN(quantity)) {
totalPrice.innerText = 0;
} else if (price) {
totalPrice.innerText = price * quantity;
totalPrice.innerText = isFloat(Number(price) * quantity);
}
}

function updateTotalPrice() {
let calculateTotalPrice = 0;
const itemPrices = document.querySelectorAll('.item-price');

itemPrices.forEach(itemPrice => {
const price = itemPrice.innerText;
calculateTotalPrice += Number(price);
});

const totalPriceElement = document.querySelector('.item-total-price');
const totalPrice = calculateTotalPrice.toFixed(2);
const totalPrice = isFloat(calculateTotalPrice);

if (totalPriceElement) {
totalPriceElement.innerText = `${totalPrice} ${currencyCode}`;
}
}

document.addEventListener('DOMContentLoaded', () => {
setCurrencySymbol();
updateTotalPrice();
});

function updateDOM(cartItemId, productVariantId, quantity) {
updateCart(cartItemId, quantity, '.total-price', cartItemId, productVariantId);
updateTotalPrice();
Expand All @@ -90,24 +95,17 @@ async function updateQuantity(cartItemId, productVariantId, quantity) {
updateDOM(cartItemId, productVariantId, quantity);
updatePrice(cartItemId,productVariantId,quantity);
updateTotalPrice();
await updateCartDrawer();
}


async function updateOnchange(cartItemId, productVariantId) {
const inputHolder = document.getElementById(cartItemId);
const input = inputHolder.querySelector(`input[id="${productVariantId}"]`);
const quantity = input.value;

await updateQuantity(cartItemId, productVariantId, quantity);
updateDOM(cartItemId, productVariantId, quantity);
updatePrice(cartItemId,productVariantId,quantity);
updateTotalPrice();
}

document.addEventListener('DOMContentLoaded', () => {
updateTotalPrice();
});

async function decreaseQuantity(cartItemId, productVariantId, quantity) {
if (quantity < 1) {
return;
Expand All @@ -119,13 +117,23 @@ async function increaseQuantity(cartItemId, productVariantId, quantity) {
await updateQuantity(cartItemId, productVariantId, quantity);
}

function updateCartItemCount(count) {
const cartItemsCount = document.getElementById('cart-items-count');
if (cartItemsCount) {
cartItemsCount.textContent = count;
}
}

async function removeItem(cartItemId, productVariantId) {
load(`#loading__${cartItemId}`);
try {
await youcanjs.cart.removeItem({ cartItemId, productVariantId });
document.getElementById(cartItemId).remove();
document.getElementById(`cart-item-${cartItemId}`).remove();

const updatedCart = await youcanjs.cart.fetch();
updateCartItemCount(updatedCart.count);

updateTotalPrice();
await updateCartDrawer();

Expand Down Expand Up @@ -160,5 +168,3 @@ async function removeItem(cartItemId, productVariantId) {
stopLoad(`#loading__${cartItemId}`);
}
}

window.removeItem = removeItem;
19 changes: 19 additions & 0 deletions assets/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,22 @@ function mountSlider(isMobile, mobileSlider, desktopSlider) {
runSlider();
isMobile.addEventListener('change', runSlider, { passive: true });
}

/**
* If the value is float fix the decimal in tow digits
* @param {string | number} value
* @returns {number} formated value
*/
function isFloat(value) {
if (isNaN(value)) {
return 0;
}

const numericValue = Number(value);

if (Number.isInteger(numericValue)) {
return numericValue.toFixed(0);
}

return numericValue.toFixed(2);
}
2 changes: 1 addition & 1 deletion sections/main-navbar.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
</form>
</div>
</div>
<button class='cart relative' id='navbar-cart-icon'>
<button class='cart relative' id='navbar-cart-icon' onclick='preventCartDrawerOpening("{{ template.name }}")'>
<span id='cart-items-badge'>
{{- cart.items | size -}}
</span>
Expand Down

0 comments on commit 0cb95b1

Please sign in to comment.