// Global variables let cart = JSON.parse(localStorage.getItem('cart')) || []; let products = []; // Fetch products from API async function fetchProducts() { try { // In a real app, this would be your API endpoint // For demo purposes, we'll use mock data const mockProducts = [ { id: 1, name: "Premium Wireless Headphones", price: 199.99, category: "electronics", image: "http://static.photos/technology/320x240/1", rating: 4.5, reviews: 128 }, { id: 2, name: "Organic Cotton T-Shirt", price: 29.99, category: "fashion", image: "http://static.photos/people/320x240/2", rating: 4.2, reviews: 56 }, { id: 3, name: "Smart Home Speaker", price: 149.99, category: "electronics", image: "http://static.photos/technology/320x240/3", rating: 4.7, reviews: 204 }, { id: 4, name: "Stainless Steel Water Bottle", price: 24.99, category: "home", image: "http://static.photos/office/320x240/4", rating: 4.3, reviews: 87 } ]; products = mockProducts; renderFeaturedProducts(); } catch (error) { console.error('Error fetching products:', error); } } // Render featured products on homepage function renderFeaturedProducts() { const productsContainer = document.querySelector('.grid'); if (!productsContainer) return; productsContainer.innerHTML = products.map(product => `
`).join(''); feather.replace(); setupAddToCartButtons(); } // Setup add to cart buttons function setupAddToCartButtons() { document.querySelectorAll('.add-to-cart').forEach(button => { button.addEventListener('click', function(e) { e.preventDefault(); const productId = parseInt(this.getAttribute('data-id')); addToCart(productId); }); }); } // Add product to cart function addToCart(productId) { const product = products.find(p => p.id === productId); if (!product) return; const existingItem = cart.find(item => item.id === productId); if (existingItem) { existingItem.quantity += 1; } else { cart.push({ id: product.id, name: product.name, price: product.price, image: product.image, quantity: 1 }); } updateCartCount(); saveCartToLocalStorage(); showToast(`${product.name} added to cart`); } // Update cart count in navbar function updateCartCount() { const countElements = document.querySelectorAll('.cart-count'); const totalItems = cart.reduce((total, item) => total + item.quantity, 0); countElements.forEach(el => { el.textContent = totalItems; el.classList.toggle('hidden', totalItems === 0); }); } // Save cart to localStorage function saveCartToLocalStorage() { localStorage.setItem('cart', JSON.stringify(cart)); } // Show toast notification function showToast(message) { const toast = document.createElement('div'); toast.className = 'fixed bottom-4 right-4 bg-green-500 text-white px-4 py-2 rounded-lg shadow-lg animate-fade-in-up'; toast.textContent = message; document.body.appendChild(toast); setTimeout(() => { toast.classList.add('animate-fade-out'); setTimeout(() => toast.remove(), 300); }, 3000); } // Initialize the app document.addEventListener('DOMContentLoaded', function() { updateCartCount(); if (window.location.pathname === '/' || window.location.pathname === '/index.html') { fetchProducts(); } }); // Animation keyframes for toast const style = document.createElement('style'); style.textContent = ` @keyframes fadeInUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } } @keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } } .animate-fade-in-up { animation: fadeInUp 0.3s ease forwards; } .animate-fade-out { animation: fadeOut 0.3s ease forwards; } `; document.head.appendChild(style);