|
| 1 | +// Constants for API endpoint and cart |
| 2 | +const API_ENDPOINT = 'https://fakestoreapi.com/products'; |
| 3 | +const cartItems = []; |
| 4 | + |
| 5 | +// Fetch product data from FakeStore API |
| 6 | +async function fetchProducts() { |
| 7 | + try { |
| 8 | + const response = await fetch(API_ENDPOINT); |
| 9 | + const data = await response.json(); |
| 10 | + return data; |
| 11 | + } catch (error) { |
| 12 | + throw new Error('Error fetching data:', error); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +// Create a product card element |
| 17 | +function createProductCard(product) { |
| 18 | + const productCard = document.createElement('div'); |
| 19 | + productCard.classList.add('col-md-4', 'mb-4'); |
| 20 | + |
| 21 | + const card = document.createElement('div'); |
| 22 | + card.classList.add('card', 'h-100'); |
| 23 | + |
| 24 | + const image = document.createElement('img'); |
| 25 | + image.src = product.image; |
| 26 | + image.alt = product.title; |
| 27 | + image.classList.add('card-img-top'); |
| 28 | + |
| 29 | + const cardBody = document.createElement('div'); |
| 30 | + cardBody.classList.add('card-body'); |
| 31 | + |
| 32 | + const title = document.createElement('h5'); |
| 33 | + title.textContent = product.title; |
| 34 | + title.classList.add('card-title'); |
| 35 | + |
| 36 | + const price = document.createElement('p'); |
| 37 | + price.textContent = `$${product.price}`; |
| 38 | + price.classList.add('card-text'); |
| 39 | + |
| 40 | + const addToCartBtn = document.createElement('button'); |
| 41 | + addToCartBtn.textContent = 'Add to Cart'; |
| 42 | + addToCartBtn.classList.add('btn', 'btn-primary', 'btn-block'); |
| 43 | + addToCartBtn.addEventListener('click', () => addToCart(product)); |
| 44 | + |
| 45 | + cardBody.appendChild(title); |
| 46 | + cardBody.appendChild(price); |
| 47 | + cardBody.appendChild(addToCartBtn); |
| 48 | + |
| 49 | + card.appendChild(image); |
| 50 | + card.appendChild(cardBody); |
| 51 | + |
| 52 | + productCard.appendChild(card); |
| 53 | + |
| 54 | + return productCard; |
| 55 | +} |
| 56 | + |
| 57 | +let products = []; |
| 58 | + |
| 59 | +// Render product cards on the product listing page |
| 60 | +function renderProducts(productsToRender) { |
| 61 | + const productContainer = document.querySelector('.product-list'); |
| 62 | + productContainer.innerHTML = ''; // Clear existing products before rendering |
| 63 | + |
| 64 | + productsToRender.forEach(product => { |
| 65 | + const productCard = createProductCard(product); |
| 66 | + productContainer.appendChild(productCard); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +// Add a product to the shopping cart |
| 71 | +function addToCart(product) { |
| 72 | + cartItems.push(product); |
| 73 | + updateCart(); |
| 74 | +} |
| 75 | + |
| 76 | +// Update the cart display |
| 77 | +function updateCart() { |
| 78 | + const cartItemsList = document.getElementById('cart-items'); |
| 79 | + cartItemsList.innerHTML = ''; |
| 80 | + |
| 81 | + cartItems.forEach(item => { |
| 82 | + const li = document.createElement('li'); |
| 83 | + li.textContent = `${item.title} - $${item.price}`; |
| 84 | + li.classList.add('list-group-item'); |
| 85 | + cartItemsList.appendChild(li); |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +// Show confetti effect on checkout |
| 90 | +function showConfetti() { |
| 91 | + particlesJS('confetti-container', { |
| 92 | + particles: { |
| 93 | + number: { |
| 94 | + value: 100, |
| 95 | + density: { |
| 96 | + enable: true, |
| 97 | + value_area: 800 |
| 98 | + } |
| 99 | + }, |
| 100 | + color: { |
| 101 | + value: '#ff6347' |
| 102 | + }, |
| 103 | + shape: { |
| 104 | + type: 'circle', |
| 105 | + stroke: { |
| 106 | + width: 0, |
| 107 | + color: '#000000' |
| 108 | + }, |
| 109 | + polygon: { |
| 110 | + nb_sides: 5 |
| 111 | + } |
| 112 | + }, |
| 113 | + opacity: { |
| 114 | + value: 0.7, |
| 115 | + random: true, |
| 116 | + anim: { |
| 117 | + enable: true, |
| 118 | + speed: 1, |
| 119 | + opacity_min: 0.1, |
| 120 | + sync: false |
| 121 | + } |
| 122 | + }, |
| 123 | + size: { |
| 124 | + value: 5, |
| 125 | + random: true, |
| 126 | + anim: { |
| 127 | + enable: true, |
| 128 | + speed: 2, |
| 129 | + size_min: 0.1, |
| 130 | + sync: false |
| 131 | + } |
| 132 | + }, |
| 133 | + line_linked: { |
| 134 | + enable_auto: true, |
| 135 | + distance: 100, |
| 136 | + color: '#ff6347', |
| 137 | + opacity: 0.4, |
| 138 | + width: 1, |
| 139 | + condensed_mode: { |
| 140 | + enable: false, |
| 141 | + rotateX: 600, |
| 142 | + rotateY: 600 |
| 143 | + } |
| 144 | + }, |
| 145 | + move: { |
| 146 | + enable: true, |
| 147 | + speed: 1, |
| 148 | + direction: 'none', |
| 149 | + random: false, |
| 150 | + straight: false, |
| 151 | + out_mode: 'out', |
| 152 | + bounce: false, |
| 153 | + attract: { |
| 154 | + enable: false, |
| 155 | + rotateX: 600, |
| 156 | + rotateY: 1200 |
| 157 | + } |
| 158 | + } |
| 159 | + }, |
| 160 | + interactivity: { |
| 161 | + detect_on: 'canvas', |
| 162 | + events: { |
| 163 | + onhover: { |
| 164 | + enable: true, |
| 165 | + mode: 'repulse' |
| 166 | + }, |
| 167 | + onclick: { |
| 168 | + enable: true, |
| 169 | + mode: 'push' |
| 170 | + }, |
| 171 | + resize: true |
| 172 | + }, |
| 173 | + modes: { |
| 174 | + grab: { |
| 175 | + distance: 400, |
| 176 | + line_linked: { |
| 177 | + opacity: 1 |
| 178 | + } |
| 179 | + }, |
| 180 | + bubble: { |
| 181 | + distance: 400, |
| 182 | + size: 40, |
| 183 | + duration: 2, |
| 184 | + opacity: 8, |
| 185 | + speed: 3 |
| 186 | + }, |
| 187 | + repulse: { |
| 188 | + distance: 200 |
| 189 | + }, |
| 190 | + push: { |
| 191 | + particles_nb: 4 |
| 192 | + }, |
| 193 | + remove: { |
| 194 | + particles_nb: 2 |
| 195 | + } |
| 196 | + } |
| 197 | + }, |
| 198 | + retina_detect: true |
| 199 | + }); |
| 200 | +} |
| 201 | + |
| 202 | +// Show checkout page with confetti |
| 203 | +function showCheckoutPage() { |
| 204 | + const main = document.querySelector('main'); |
| 205 | + main.innerHTML = ` |
| 206 | + <div class="checkout-page"> |
| 207 | + <h2>Checkout</h2> |
| 208 | + <form> |
| 209 | + <!-- Add more fields for shipping and payment info --> |
| 210 | + <div class="form-group"> |
| 211 | + <label for="name">Name</label> |
| 212 | + <input type="text" class="form-control" id="name" required> |
| 213 | + </div> |
| 214 | + <div class="form-group"> |
| 215 | + <label for="email">Email</label> |
| 216 | + <input type="email" class="form-control" id="email" required> |
| 217 | + </div> |
| 218 | + <button type="submit" class="btn btn-primary btn-block">Place Order</button> |
| 219 | + </form> |
| 220 | + </div> |
| 221 | + `; |
| 222 | + |
| 223 | + showConfetti(); |
| 224 | + const checkoutForm = document.getElementById('checkout-form'); |
| 225 | + checkoutForm.addEventListener('submit', handleCheckout); |
| 226 | +} |
| 227 | +function handleCheckout(event) { |
| 228 | + event.preventDefault(); // Prevent form submission and redirect |
| 229 | + |
| 230 | + // Get the form inputs |
| 231 | + const name = document.getElementById('name').value; |
| 232 | + const email = document.getElementById('email').value; |
| 233 | + |
| 234 | + |
| 235 | + // Redirect back to the product listing page after a delay |
| 236 | + setTimeout(() => { |
| 237 | + location.href = 'index.html'; |
| 238 | + }, 10000); |
| 239 | +} |
| 240 | +const searchInput = document.getElementById('search-input'); |
| 241 | +const categoryFilter = document.getElementById('category-filter'); |
| 242 | +const priceRange = document.getElementById('price-range'); |
| 243 | + |
| 244 | +searchInput.addEventListener('input', applyFilters); |
| 245 | +categoryFilter.addEventListener('change', applyFilters); |
| 246 | +priceRange.addEventListener('input', applyFilters); |
| 247 | + |
| 248 | +// Function to filter products based on search and filters |
| 249 | +function applyFilters() { |
| 250 | + const searchText = searchInput.value.toLowerCase(); |
| 251 | + const selectedCategory = categoryFilter.value.toLowerCase(); |
| 252 | + const maxPrice = priceRange.value; |
| 253 | + |
| 254 | + const filteredProducts = products.filter(product => { |
| 255 | + const titleMatch = product.title.toLowerCase().includes(searchText); |
| 256 | + const categoryMatch = selectedCategory === '' || product.category.toLowerCase() === selectedCategory; |
| 257 | + const priceMatch = parseFloat(product.price) <= parseFloat(maxPrice); |
| 258 | + |
| 259 | + return titleMatch && categoryMatch && priceMatch; |
| 260 | + }); |
| 261 | + |
| 262 | + renderProducts(filteredProducts); |
| 263 | +} |
| 264 | + |
| 265 | + |
| 266 | +const checkoutBtn = document.getElementById('checkout-btn'); |
| 267 | +checkoutBtn.addEventListener('click', showCheckoutPage); |
| 268 | + |
| 269 | +// Initialize the website |
| 270 | +async function init() { |
| 271 | + try { |
| 272 | + products = await fetchProducts(); |
| 273 | + renderProducts(products); |
| 274 | + } catch (error) { |
| 275 | + console.error('Error fetching data:', error); |
| 276 | + } |
| 277 | +} |
| 278 | + |
| 279 | + |
| 280 | +init(); |
0 commit comments