Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/components/product-components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import React from 'react';
import { Product } from '../../types';

interface CardProps {
product: Product
}

const Card: React.FC<CardProps> = () => {
const Card: React.FC<CardProps> = ({product}: CardProps) => {

return (
<div className='product scale-effect'>
<div className='product-image'>
<img />
<img src={product.image} />
</div>
<div className='product-info'>
<h2 className='product-title'></h2>
<div className='product-brief'>
<p><strong>Price: </strong></p>
<p><strong>Rating: </strong></p>
<p><strong>Category: </strong></p>
<p><strong>Price: {product.price} </strong></p>
<p><strong>Rating: {product.rating.rate} </strong></p>
<p><strong>Category: {product.category}</strong></p>
</div>
</div>
</div>
Expand Down
4 changes: 1 addition & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import App from './App'
const container = document.getElementById('root');
if (container) {
createRoot(container).render(
<StrictMode>
<App />
</StrictMode>,
<App />
)
}
27 changes: 22 additions & 5 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Product[]>([]);
Expand All @@ -16,16 +17,30 @@ const Home: React.FC = () => {
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(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) {
Expand Down Expand Up @@ -57,7 +72,9 @@ const Home: React.FC = () => {
<div className='container'>
<section>
<div className='products-grid' id='products-list'>
{/* TODO - map products */}
{products.map((product) => {
return <Card product = {product} key={product.id}/>
})}
</div>
</section>
</div>
Expand Down
14 changes: 11 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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
}