A dynamic e-commerce web application that i built with Svelte. The application fetches data from an API and incorporates user interactions, such as selecting categories, sorting products, and viewing detailed product information.
function applyFilters() {
let filtered = [...products];
if (sortOption === 'asc') {
filtered.sort((a, b) => a.price - b.price);
} else if (sortOption === 'desc') {
filtered.sort((a, b) => b.price - a.price);
}
filteredProducts = filtered;
}
async function fetchProducts(category = '') {
const url = category
? `https://fakestoreapi.com/products/category/${category}`
: 'https://fakestoreapi.com/products';
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Failed to fetch data');
}
products = await response.json();
applyFilters();
} catch (err) {
error = err.message || 'An error occurred';
} finally {
loading = false;
}
}