Skip to content

Commit ca3449e

Browse files
committed
👌 IMPROVE: Top 10 best sellers
1 parent 6c73806 commit ca3449e

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

src/Views/Home.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1-
import React from 'react'
2-
import HelloWorld from '../Components/HelloWorld'
1+
import React, { useState, useEffect } from 'react'
2+
import axios from 'axios'
3+
import ProductCard from '../Components/ProductCard'
34

45
function Home(){
6+
let content=null
7+
const url=`https://5f2a8d0d6ae5cc0016422a91.mockapi.io/api/v1/products?page=1&limit=10`
8+
const [products,setProducts] = useState({
9+
data: null,
10+
error: false
11+
})
12+
13+
useEffect(()=>{
14+
axios.get(url)
15+
.then(res=>{
16+
setProducts({
17+
data: res.data,
18+
error: false
19+
})
20+
})
21+
.catch(()=>{
22+
setProducts({
23+
data: null,
24+
error: true
25+
})
26+
})
27+
},[url])
28+
29+
30+
if(products.error){
31+
content = <p>
32+
There was an error, please refresh or try again later!
33+
</p>
34+
}
35+
36+
if(products.data){
37+
content =
38+
products.data.map((product) =>
39+
<ProductCard key={product.id}
40+
product={product}
41+
/>
42+
)
43+
}
44+
545
return (
646
<div>
7-
<h1 className="font-bold text-2xl">Home page</h1>
8-
<HelloWorld name="Drake"/>
47+
<h1 className="font-bold text-2xl">Best Sellers</h1>
48+
{content}
949
</div>
1050
)
1151
}

0 commit comments

Comments
 (0)