Skip to content

Commit

Permalink
- add listener to checkout button only if present
Browse files Browse the repository at this point in the history
- remove unused import ModalManager
  • Loading branch information
creme332 committed May 22, 2024
1 parent 21841fd commit 72e17b3
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions public/js_original/cart-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import Cart from "./models/Cart";
import CartItem from "./models/CartItem";
import ModalManager from "./modal";

function updateCart(e) {
const sectionNode = e.target.parentNode.parentNode;
Expand Down Expand Up @@ -67,12 +66,13 @@ async function checkout() {
});

// stop loading animation
document.querySelector("#checkout-btn").setAttribute("aria-busy", "false");
document.querySelector("#checkout-btn").removeAttribute("aria-busy");

if (response.ok) {
// Clear cart items from localStorage if checkout is successful
myCart.clear();
ModalManager("my-modal").openModal();

document.querySelector("#my-modal").setAttribute("open", "");
return;
}
const x = await response.json();
Expand All @@ -91,9 +91,12 @@ function initCartPage() {
...document.querySelectorAll("section input[type='number']"),
];

ModalManager("my-modal").init();
const checkoutBtn = document.querySelector("#checkout-btn");

document.querySelector("#checkout-btn").addEventListener("click", checkout);
// if checkout button is present on page (button is absent when cart is empty)
if (checkoutBtn !== null) {
checkoutBtn.addEventListener("click", checkout);
}

quantityInputs.forEach((input) => {
input.addEventListener("change", updateCart);
Expand Down

0 comments on commit 72e17b3

Please sign in to comment.