Download script.js from ebaycom/techsphere-marketplace-revamp: direct link, hf CLI and curl.
- Browser
- Download file 11 kB
-
https://huggingface.co/spaces/ebaycom/techsphere-marketplace-revamp/resolve/main/script.js
- Command line
-
hf download hf://spaces/ebaycom/techsphere-marketplace-revamp/script.js
-
curl -L -o script.js https://huggingface.co/spaces/ebaycom/techsphere-marketplace-revamp/resolve/main/script.js
11 kB
| // 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 => ` | |
| <div class="product-card bg-white rounded-xl shadow-sm hover:shadow-lg transition duration-300 overflow-hidden"> | |
| <div class="relative"> | |
| <img src="${product.image}" alt="${product.title}" class="w-full h-48 object-cover"> | |
| </div> | |
| <div class="p-4"> | |
| <h3 class="font-semibold text-lg mb-2">${product.title}</h3> | |
| <p class="text-gray-600 text-sm mb-3">${product.description.substring(0, 100)}...</p> | |
| <div class="flex justify-between items-center"> | |
| <span class="text-blue-600 font-bold text-xl">$${product.price}</span> | |
| </div> | |
| <a href="#" class="block w-full bg-blue-600 hover:bg-blue-700 text-white text-center py-2 rounded-lg font-semibold transition duration-300"> | |
| Add to Cart | |
| </a> | |
| </div> | |
| </div> | |
| `).join(''); | |
| } | |
| // Initialize product loading | |
| loadProducts(); |