diff --git a/src/components/blog/index.jsx b/src/components/blog/index.jsx new file mode 100644 index 0000000..2daa067 --- /dev/null +++ b/src/components/blog/index.jsx @@ -0,0 +1,213 @@ +import { getDevtoArticle, getMediumArticle } from 'article-api'; +import moment from 'moment'; +import { Fragment, useContext, useEffect, useState } from 'react'; +import { CgHashtag } from 'react-icons/cg'; +import { LoadingContext } from '../../contexts/LoadingContext'; +import config from '../../ezprofile.config'; +import { ga, skeleton } from '../../helpers/utils'; +import LazyImage from '../lazy-image'; + +const displaySection = () => { + if ( + typeof config.blog !== 'undefined' && + typeof config.blog.source !== 'undefined' && + typeof config.blog.username !== 'undefined' && + config.blog.source && + config.blog.username + ) { + return true; + } else { + return false; + } +}; + +const Blog = () => { + const [articles, setArticles] = useState(null); + const [loading] = useContext(LoadingContext); + + useEffect(() => { + if (displaySection()) { + if (config.blog.source === 'medium') { + getMediumArticle({ + user: config.blog.username, + }).then((res) => { + setArticles(res); + }); + } else if (config.blog.source === 'dev.to') { + getDevtoArticle({ + user: config.blog.username, + }).then((res) => { + setArticles(res); + }); + } + } + }, []); + + const renderSkeleton = () => { + let array = []; + for (let index = 0; index < config.blog.limit; index++) { + array.push( +
+
+
+
+
+ {skeleton({ + width: 'w-full', + height: 'h-full', + shape: '', + })} +
+
+
+
+
+

+ {skeleton({ + width: 'w-full', + height: 'h-8', + className: 'mb-2 mx-auto md:mx-0', + })} +

+ {skeleton({ + width: 'w-24', + height: 'h-3', + className: 'mx-auto md:mx-0', + })} +
+ {skeleton({ + width: 'w-full', + height: 'h-4', + className: 'mx-auto md:mx-0', + })} +
+
+ {skeleton({ + width: 'w-32', + height: 'h-4', + className: 'md:mr-2 mx-auto md:mx-0', + })} +
+
+
+
+
+
+
+ ); + } + + return array; + }; + + const renderArticles = () => { + return ( + articles && + articles.slice(0, config.blog.limit).map((article, index) => ( +
{ + try { + if (config.googleAnalytics?.id) { + ga.event({ + action: 'Click Blog Post', + params: { + post: article.title, + }, + }); + } + } catch (error) { + console.error(error); + } + + window.open(article.link, '_blank'); + }} + > +
+
+
+
+ +
+
+
+
+
+

+ {article.title} +

+

+ {moment(article.publishedAt).fromNow()} +

+

+ {article.description} +

+
+ {article.categories.map((category, index2) => ( +
+ + + + {category} +
+ ))} +
+
+
+
+
+
+
+ )) + ); + }; + + return ( + + {displaySection() && ( +
+
+
+
+
+
    +
  • +
    +
    + {loading || !articles ? ( + skeleton({ width: 'w-28', height: 'h-8' }) + ) : ( + Recent Posts + )} +
    +
    +
  • +
+
+
+
+
+
+ {loading || !articles ? renderSkeleton() : renderArticles()} +
+
+
+
+ )} +
+ ); +}; + +export default Blog;