Skip to content

Commit 0b2a102

Browse files
#TRAPWLS-19 code optimized for production
1 parent 42fdaca commit 0b2a102

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/fetchApiData/FetchApi.jsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import React ,{useEffect, useState} from 'react'
22
import axios from 'axios'
33
import { toast } from 'react-toastify'
44
import ClipLoader from 'react-spinners/ClipLoader';
5+
import { posts } from '../homePage/data';
56

7+
//Sample url(local env) - https://newsapi.org/v2/top-headlines?country=us&apiKey=6004b8fcb1604003b4ead57854e8d2c2
68
function FetchApi() {
79
const [data, setData] = useState([]);
810
const [isLoading, setIsLoading] = useState(false);
@@ -12,9 +14,9 @@ function FetchApi() {
1214
//sample get api
1315
try {
1416
setIsLoading(true);
15-
await axios.get("https://newsapi.org/v2/top-headlines?country=us&apiKey=6004b8fcb1604003b4ead57854e8d2c2")
17+
await axios.get("https://api.thecatapi.com/v1/images/search?limit=10")
1618
.then((response) => {
17-
setData(response?.data?.articles);
19+
setData(response?.data);
1820
})
1921
} catch (e) {
2022
console.error(e);
@@ -37,19 +39,19 @@ function FetchApi() {
3739
return (
3840
<>
3941
<div className="btn-fetchContainer">
40-
<button className='btn-fetch' onClick={data.length > 0 ? showToast : fetchData}>Click here to Fetch Api Data!!</button>
42+
<button className='btn-fetch' onClick={data?.length > 0 ? showToast : fetchData}>Click here to Fetch Api Data!!</button>
4143
</div>
4244

4345
<div className="ui-fetched">
4446
{
4547
data.map((value, index) => {
46-
const imageUrl = value?.urlToImage;
48+
const imageUrl = value?.url;
4749
return(
4850
<div className='card' style={{width: "20rem"}} key={index}>
4951
<img src={(imageUrl && imageUrl !== null) ? imageUrl : thumbImage} className="card-img-top" width={300} alt={value?.title}/>
5052
{imageUrl !== null && <div className="card-body">
51-
<h5 className="card-title">{value?.title}</h5>
52-
<p className="card-desc">{value?.description}</p><br></br>
53+
<h5 className="card-title">{value?.id}</h5>
54+
<p className="card-desc">{posts[0]?.desc}</p><br></br>
5355
Want to Read more, <a href={value?.url || "/notfound"} target='_blank' rel='noopener noreferrer' className="btn-primary"> <span style={{fontWeight: "bold"}}>Click here</span></a>
5456
</div>}
5557
</div>

0 commit comments

Comments
 (0)