Last active
July 20, 2021 16:44
-
-
Save imabp/f06bea701933b5697ca9c6261722ec2c to your computer and use it in GitHub Desktop.
Integrate blogtraversy with your Portfolio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Thanks for using blogtraversy. | |
Read this before using.... | |
Please configure the following usernames variable, with your respective handles. | |
You can set a type at line:34, default type is 'all' which fetches all the blogs. | |
*/ | |
import React, { useState } from "react"; | |
import { getBlogs } from "blogtraversy"; | |
// importing react bootstrap for quick css. | |
import { | |
Card, | |
Button, | |
Row, | |
Col, | |
Container, | |
InputGroup, | |
FormControl, | |
Nav, | |
} from "react-bootstrap"; | |
const MyBlogs = () => { | |
// initializing states | |
const [blogs, setBlogs] = React.useState([]); | |
const [error, setError] = React.useState(false); | |
// config for usernames, modify accordingly. | |
let usernames = { | |
mediumUsername: `abirwrites`, | |
hashnodeUsername: `imabp`, | |
}; | |
//select type before you render type = 'all' | 'medium' | 'hashnode' | |
const type = "all"; | |
React.useEffect(()=>{ | |
async function fetchBlogs(){ | |
const blogs = await getBlogs(type, usernames); | |
if (type == "all") { | |
// spreading the hashnodeposts and mediumarticles when fetching under 'all' condition. | |
setBlogs([...blogs.hashnodePosts, ...blogs.mediumArticles]); } | |
else { | |
setBlogs(blogs); | |
} | |
} | |
fetchBlogs(); | |
},[]) | |
return ( | |
<Container> | |
<Row> | |
{ | |
blogs && | |
blogs.map((el) => { | |
return ( | |
<Col> | |
<Card style={{ width: "18rem" }}> | |
<Card.Img variant="top" src={`${el.thumbnail}`} width="200" /> | |
<Card.Body> | |
<Card.Title>{el.title}</Card.Title> | |
<Button variant="primary"> | |
<a | |
href={el.link} | |
target="_blank" | |
rel="noreferrer noopenner" | |
> | |
Read Article | |
</a> | |
</Button> | |
</Card.Body> | |
</Card> | |
</Col> | |
); | |
})} | |
</Row> | |
</Container> | |
); | |
}; | |
export default MyBlogs; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment