forked from adrianhajdin/project_graphql_blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostWidget.jsx
50 lines (45 loc) · 1.55 KB
/
PostWidget.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import React, { useState, useEffect } from 'react';
import Image from 'next/image';
import moment from 'moment';
import Link from 'next/link';
import { grpahCMSImageLoader } from '../util';
import { getSimilarPosts, getRecentPosts } from '../services';
const PostWidget = ({ categories, slug }) => {
const [relatedPosts, setRelatedPosts] = useState([]);
useEffect(() => {
if (slug) {
getSimilarPosts(categories, slug).then((result) => {
setRelatedPosts(result);
});
} else {
getRecentPosts().then((result) => {
setRelatedPosts(result);
});
}
}, [slug]);
return (
<div className="bg-white shadow-lg rounded-lg p-8 pb-12 mb-8">
<h3 className="text-xl mb-8 font-semibold border-b pb-4">{slug ? 'Related Posts' : 'Recent Posts'}</h3>
{relatedPosts.map((post, index) => (
<div key={index} className="flex items-center w-full mb-4">
<div className="w-16 flex-none">
<Image
loader={grpahCMSImageLoader}
alt={post.title}
height="60px"
width="60px"
unoptimized
className="align-middle rounded-full"
src={post.featuredImage.url}
/>
</div>
<div className="flex-grow ml-4">
<p className="text-gray-500 font-xs">{moment(post.createdAt).format('MMM DD, YYYY')}</p>
<Link href={`/post/${post.slug}`} className="text-md" key={index}>{post.title}</Link>
</div>
</div>
))}
</div>
);
};
export default PostWidget;