|
| 1 | +/* eslint-disable no-unused-vars */ |
| 2 | +// Fake API URL (you can replace this with a real API if needed) |
| 3 | +const apiUrl = 'https://fakestoreapi.com/products'; |
| 4 | + |
| 5 | +// Elements |
| 6 | +const productsContainer = document.getElementById('products-container'); |
| 7 | +const cartBtn = document.getElementById('cart-btn'); |
| 8 | +const cartModal = document.getElementById('cart-modal'); |
| 9 | +const closeCartBtn = document.getElementById('close-cart'); |
| 10 | +const cartItemsList = document.getElementById('cart-items'); |
| 11 | + |
| 12 | +// Cart array to store added products |
| 13 | +const cart = []; |
| 14 | + |
| 15 | +// Display fetched products |
| 16 | +const displayProducts = (products) => { |
| 17 | + productsContainer.innerHTML = ''; // Clear previous products |
| 18 | + |
| 19 | + products.forEach((product) => { |
| 20 | + const productElement = document.createElement('div'); |
| 21 | + productElement.classList.add('product'); |
| 22 | + productElement.innerHTML = ` |
| 23 | + <img src="${product.image}" alt="${product.title}"> |
| 24 | + <h3 class="title">${product.title}</h3> |
| 25 | + <p class="price">$${product.price}</p> |
| 26 | + <button onclick="addToCart(${product.id}, '${product.title}', '${product.image}', ${product.price})">Add to Cart</button> |
| 27 | + `; |
| 28 | + productsContainer.appendChild(productElement); |
| 29 | + }); |
| 30 | +}; |
| 31 | + |
| 32 | +// Fetch products from API |
| 33 | +async function fetchProducts() { |
| 34 | + try { |
| 35 | + const response = await fetch(apiUrl); |
| 36 | + const products = await response.json(); |
| 37 | + displayProducts(products); |
| 38 | + } catch (error) { |
| 39 | + console.error('Error fetching products:', error); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Add product to cart |
| 44 | +const addToCart = (id, title, image, price) => { |
| 45 | + const existingProductIndex = cart.findIndex((item) => item.id === id); |
| 46 | + |
| 47 | + if (existingProductIndex === -1) { |
| 48 | + cart.push({ |
| 49 | + id, |
| 50 | + title, |
| 51 | + image, |
| 52 | + price, |
| 53 | + quantity: 1, |
| 54 | + }); |
| 55 | + } else { |
| 56 | + cart[existingProductIndex].quantity += 1; |
| 57 | + } |
| 58 | + |
| 59 | + console.log(cart); // You can replace this with a cart UI or alert |
| 60 | + alert(`${title} added to cart!`); |
| 61 | +}; |
| 62 | + |
| 63 | +// Close cart modal |
| 64 | +closeCartBtn.addEventListener('click', () => { |
| 65 | + cartModal.style.display = 'none'; |
| 66 | +}); |
| 67 | + |
| 68 | +// Display cart contents |
| 69 | +const displayCart = () => { |
| 70 | + cartItemsList.innerHTML = ''; // Clear previous cart items |
| 71 | + |
| 72 | + cart.forEach((item) => { |
| 73 | + const cartItem = document.createElement('li'); |
| 74 | + cartItem.innerHTML = ` |
| 75 | + <span>${item.title} (x${item.quantity})</span> |
| 76 | + <span>$${item.price * item.quantity}</span> |
| 77 | + `; |
| 78 | + cartItemsList.appendChild(cartItem); |
| 79 | + }); |
| 80 | +}; |
| 81 | + |
| 82 | +// Open cart modal |
| 83 | +cartBtn.addEventListener('click', () => { |
| 84 | + if (cart.length === 0) { |
| 85 | + alert('Your cart is empty.'); |
| 86 | + } else { |
| 87 | + displayCart(); |
| 88 | + cartModal.style.display = 'flex'; |
| 89 | + } |
| 90 | +}); |
| 91 | + |
| 92 | +// Initialize |
| 93 | +fetchProducts(); |
0 commit comments