From e1e5d9a71f6984b510ebe8bf615fe3534b52e33e Mon Sep 17 00:00:00 2001 From: dharshib Date: Sun, 3 Nov 2024 19:51:49 +0530 Subject: [PATCH] products page --- script/products.js | 162 +-------------------------------------------- 1 file changed, 3 insertions(+), 159 deletions(-) diff --git a/script/products.js b/script/products.js index 9449870e..abfbaf91 100644 --- a/script/products.js +++ b/script/products.js @@ -75,8 +75,6 @@ function displayProducts(products) { box.appendChild(addToCartButton); - // Add click event for the quick view icon - box.querySelector(".quick-view-icon").addEventListener("click", () => { showQuickView(product); }); @@ -84,8 +82,8 @@ function displayProducts(products) { productsSection?.appendChild(box); }); - handleQuantityButtonsInProductCard(); } + function displayFlavorFilter() { let flavorFilterSection = document.querySelector(".categories-wrapper"); @@ -291,6 +289,7 @@ function handleQuantityButtonsInProductCard() { } }); } +handleQuantityButtonsInProductCard(); function handleQuantityButtonsInCart() { const cartList = document.querySelector(".cart-list-items"); @@ -348,161 +347,6 @@ function handleQuantityButtonsInCart() { }); } - -// Add items to wishlist - -function addToWishlist(e) { - let wishlistItemId = e.target.closest(".box").getAttribute("id"); - let pcs = parseInt(e.target.closest(".box").querySelector(".pcs").textContent, 10); - - // Ensure pcs is a valid number - if (isNaN(pcs) || pcs <= 0) { - showToast("Please select the number of cups you want!"); - return; - } - - let existingWishlistItem = wishlistItems.find(item => item.itemId == wishlistItemId); - let item = products_list.find(item => item.id == wishlistItemId); - let amount = item.price * pcs; - - if (existingWishlistItem) { - existingWishlistItem.pcs += pcs; // Increment by the correct number of pcs - existingWishlistItem.amount = existingWishlistItem.pcs * item.price; - showToast(`${pcs} more ${item.name} ice cream/s successfully added to the wishlist!`); - } else { - wishlistItems.push({ - itemId: wishlistItemId, - pcs: pcs, // Ensure pcs is a number - amount: amount - }); - showToast(`${pcs} ${item.name} ice cream/s successfully added to the wishlist!`); - } - - localStorage.setItem('wishlistItems', JSON.stringify(wishlistItems)); - displayWishlistItems(); -} - - - - -function handleRemoveButtonInWishlist() { - let removeBtns = document.querySelectorAll(".remove-wishlist-item-btn"); - removeBtns.forEach(btn => { - btn.addEventListener("click", () => { - let itemId = btn.closest('li').getAttribute("id"); - wishlistItems = wishlistItems.filter(item => item.itemId !== itemId); - - localStorage.setItem('wishlistItems', JSON.stringify(wishlistItems)); - displayWishlistItems(); // Update the wishlist display - - // Reset button text to "Add to Wishlist" if necessary - const productBox = document.querySelector(`.box[id='${itemId}']`); - if (productBox) { - const addToWishlistButton = productBox.querySelector(".wishlist-btn"); - addToWishlistButton.innerHTML = ''; // Reset icon and text - } - }); - }); -} - - -function displayWishlistItems() { - const wishlistUlList = document.querySelector(".wishlist-list-items"); - wishlistUlList.innerHTML = ""; - - if (wishlistItems.length > 0) { - document.querySelector(".empty-wishlist").classList.remove("active"); - document.querySelector(".no-empty-wishlist").classList.add("active"); - } else { - document.querySelector(".empty-wishlist").classList.add("active"); - document.querySelector(".no-empty-wishlist").classList.remove("active"); - } - - wishlistItems.forEach(wi => { - let itemLi = document.createElement("li"); - itemLi.setAttribute("id", wi.itemId); - let product = products_list.find(p => p.id == wi.itemId); - - if (product) { - itemLi.innerHTML = ` - -
- ${product.name}
- - ${ci.pcs} x ${product.price}
-
$${ci.amount}
- -
-
- - - ${ci.pcs} - + - -
- `; - wishlistUlList.appendChild(itemLi); - handleRemoveButtonInWishlist(); // Attach remove functionality - - let moveToCartBtn = itemLi.querySelector(".move-to-cart"); - moveToCartBtn.addEventListener("click", () => moveToCart(wi.itemId)); - } - }); - -} - - -// Function to move item from wishlist to cart -function moveToCart(wishlistItemId) { - // Find the item in wishlist - let wishlistItemIndex = wishlistItems.findIndex(item => item.itemId == wishlistItemId); - if (wishlistItemIndex > -1) { - let wishlistItem = wishlistItems[wishlistItemIndex]; - - // Check if the item already exists in the cart - let existingCartItem = cartItems.find(item => item.itemId == wishlistItemId); - if (existingCartItem) { - existingCartItem.pcs += wishlistItem.pcs; // Update quantity if it already exists - existingCartItem.amount = existingCartItem.pcs * products_list.find(p => p.id == wishlistItemId).price; // Update amount - showToast(`${wishlistItem.pcs} ${products_list.find(p => p.id == wishlistItemId).name} moved to cart!`); - } else { - cartItems.push({ - itemId: wishlistItem.itemId, - pcs: wishlistItem.pcs, - amount: wishlistItem.amount - }); - showToast(`${wishlistItem.pcs} ${products_list.find(p => p.id == wishlistItemId).name} added to cart!`); - } - - // Remove the item from the wishlist - wishlistItems.splice(wishlistItemIndex, 1); - localStorage.setItem('wishlistItems', JSON.stringify(wishlistItems)); - - // Re-display the wishlist and cart items - displayWishlistItems(); - displayCartItems(); - } -} - - -// Scroll animation - - - -document.addEventListener("DOMContentLoaded", () => { - // Apply ScrollReveal to products - const productBoxes = document.querySelectorAll(".products-box .box"); - productBoxes.forEach((box) => { - sr.reveal(box, { - origin: "left", - distance: "80px", - duration: 600, - easing: "ease-in", - interval: 400, - reset: true, - }); - }); -}); - function createSearchBar() { const searchBarContainer = document.querySelector(".search-container"); @@ -612,4 +456,4 @@ window.onclick = function (event) { if (event.target === modal) { modal.style.display = "none"; } -}; \ No newline at end of file +};