// TechSphere Marketplace JavaScript document.addEventListener('DOMContentLoaded', function() { // Initialize all components initializeSearch(); initializeFilters(); initializeCart(); loadProducts(); }); // Search functionality function initializeSearch() { const searchInput = document.querySelector('input[type="search"]'); if (searchInput) { let searchTimeout; searchInput.addEventListener('input', function() { clearTimeout(searchTimeout); searchTimeout = setTimeout(() => { const searchTerm = this.value.toLowerCase(); filterProducts(searchTerm); }, 300); } } // Filter products based on search term function filterProducts(searchTerm) { const products = document.querySelectorAll('.product-card'); let visibleCount = 0; products.forEach(product => { const title = product.querySelector('h3').textContent.toLowerCase(); const description = product.querySelector('p').textContent.toLowerCase(); if (title.includes(searchTerm) || description.includes(searchTerm)) { product.style.display = 'block'; visibleCount++; } else { product.style.display = 'none'; } }); // Update results count const resultsElement = document.querySelector('#resultsCount'); if (resultsElement) { resultsElement.textContent = `Showing ${visibleCount} products`; } } // Initialize filter functionality function initializeFilters() { const filterButtons = document.querySelectorAll('.filter-btn'); filterButtons.forEach(button => { button.addEventListener('click', function() { // Remove active class from all buttons filterButtons.forEach(btn => btn.classList.remove('active')); // Add active class to clicked button this.classList.add('active'); applyFilters(); }); } // Price filter const priceFilter = document.querySelector('#priceFilter'); if (priceFilter) { priceFilter.addEventListener('change', applyFilters); } } function applyFilters() { const activeFilter = document.querySelector('.filter-btn.active'); const priceRange = document.querySelector('#priceFilter')?.value; // Apply both category and price filters const products = document.querySelectorAll('.product-card'); let visibleCount = 0; products.forEach(product => { const price = parseFloat(product.querySelector('.price').textContent.replace('$', '')); let shouldShow = true; if (priceRange) { const [min, max] = priceRange.split('-').map(Number); const currentPrice = parseFloat(product.querySelector('.price').textContent.replace('$', '')); if (priceRange === 'under50' && currentPrice > 50) { shouldShow = false; } else if (priceRange === '50-100' && (currentPrice < 50 || currentPrice > 100)) { shouldShow = false; } else if (priceRange === '100-200' && (currentPrice < 100 || currentPrice > 200)) { shouldShow = false; } else if (priceRange === 'over200' && currentPrice < 200) { shouldShow = false; } } if (shouldShow) { product.style.display = 'block'; visibleCount++; } else { product.style.display = 'none'; } }); // Update results count const resultsElement = document.querySelector('#resultsCount'); if (resultsElement) { resultsElement.textContent = `Showing ${visibleCount} products`; } } // Cart functionality function initializeCart() { let cart = JSON.parse(localStorage.getItem('techsphere_cart')) || []; // Add to cart buttons const addToCartButtons = document.querySelectorAll('.add-to-cart'); addToCartButtons.forEach(button => { button.addEventListener('click', function() { const productCard = this.closest('.product-card'); const productName = productCard.querySelector('h3').textContent; const productPrice = parseFloat(productCard.querySelector('.price').textContent.replace('$', '')); const product = { name: productName, price: productPrice, image: productCard.querySelector('img').src }; cart.push(product); localStorage.setItem('techsphere_cart', JSON.stringify(cart)); showCartNotification(); }); } function showCartNotification() { const notification = document.createElement('div'); notification.className = 'fixed top-4 right-4 bg-green-500 text-white px-4 py-2 rounded-lg shadow-lg'; notification.textContent = 'Product added to cart!'; document.body.appendChild(notification); setTimeout(() => { notification.remove(); }, 3000); } // Update cart count function updateCartCount() { const cartCount = document.querySelector('#cartCount'); if (cartCount) { cartCount.textContent = cart.length; } } // Load products from API async function loadProducts() { try { // Simulating API call const response = await fetch('https://fakestoreapi.com/products?limit=8'); const products = await response.json(); displayProducts(products); } catch (error) { console.error('Failed to load products:', error); } } function displayProducts(products) { const container = document.querySelector('#products .grid'); if (!container) return; container.innerHTML = products.map(product => `