From 77a53f90d6a689e6bf0e4d3e8e33fdd809d49409 Mon Sep 17 00:00:00 2001 From: sltan29 Date: Sun, 21 Dec 2025 21:44:06 +0200 Subject: [PATCH] used axios to import api, implemented filterByCategory --- src/components/product-components/Card.tsx | 11 +++++---- src/main.tsx | 4 +--- src/pages/Home.tsx | 27 ++++++++++++++++++---- src/types.ts | 14 ++++++++--- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/components/product-components/Card.tsx b/src/components/product-components/Card.tsx index c25aa5f..422c10d 100644 --- a/src/components/product-components/Card.tsx +++ b/src/components/product-components/Card.tsx @@ -2,21 +2,22 @@ import React from 'react'; import { Product } from '../../types'; interface CardProps { + product: Product } -const Card: React.FC = () => { +const Card: React.FC = ({product}: CardProps) => { return (
- +

-

Price:

-

Rating:

-

Category:

+

Price: {product.price}

+

Rating: {product.rating.rate}

+

Category: {product.category}

diff --git a/src/main.tsx b/src/main.tsx index 393275f..7b58e53 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -6,8 +6,6 @@ import App from './App' const container = document.getElementById('root'); if (container) { createRoot(container).render( - - - , + ) } diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 4eff3ea..3e88fe5 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -7,6 +7,7 @@ import DropDownFilter from '../components/filters/DropDownFilter'; import Loading from '../components/ui-elements/Loading'; import Card from '../components/product-components/Card'; import { Product } from '../types'; +import { lookupService } from 'node:dns'; const Home: React.FC = () => { const [allProducts, setAllProducts] = useState([]); @@ -16,16 +17,30 @@ const Home: React.FC = () => { const [loading, setLoading] = useState(true); const [error, setError] = useState(null); -//TODO - useEffect + axios - +useEffect(() => { + const api = async () => { + try{ + const response = await axios.get('https://fakestoreapi.com/products'); + setProducts(response.data); + setAllProducts(response.data); + } catch (error: any){ + setError(error.message); + } finally { + setLoading(false); + } + } + api(); +},[]) const showFilterByCategory = () => { setIsDropdownOpen(!isDropdownOpen); }; - // TODO - filter products by category - BONUS const filterByCategory = (category: string) => { setSelectedCategory(category); - //TODO - filter products by category + if(category === 'All') + setProducts(allProducts); + else + setProducts(allProducts.filter(product => product.category === category)) }; if (loading) { @@ -57,7 +72,9 @@ const Home: React.FC = () => {
- {/* TODO - map products */} + {products.map((product) => { + return + })}
diff --git a/src/types.ts b/src/types.ts index 1e3d610..bd7cebe 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,11 +1,19 @@ export interface Rating { -//TODO + rate: number, + count: number } export interface Product { -//TODO + id: number, + title: string, + price: number, + description: string, + category: string, + image: string, + rating: Rating } export interface Category { -//TODO + id: string, + name: string }