-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.tsx
70 lines (63 loc) · 1.98 KB
/
index.tsx
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { useState } from 'react';
import BlogPostsContainer from '@/components/blog/BlogPostsContainer';
import Container from '@/components/containers/Container';
import SearchBar from '@/components/blog/SearchBar';
import Title from '@/components/snippets/Title';
// import styles from '@/styles/Blog.module.scss';
import { BlogSearch } from '@/components/snippets/BlogSearch';
import { blogRevalidate } from '@/utils/config';
import { tagToHeading } from '@/utils/blogCategories';
import { blogSearch } from '@/utils/search';
export default function Blog({ posts }) {
const [searchTerm, setSearchTerm] = useState('');
const filteredData = {
posts: posts.slice(0, 3),
};
if (searchTerm) {
const filteredPosts = blogSearch(posts, searchTerm);
filteredData.posts = filteredPosts;
filteredData.heading = `${
filteredPosts.length === 0 ? 'no' : filteredPosts.length
} search ${
filteredPosts.length > 1 ? 'results' : 'result'
} for '${searchTerm}'`;
filteredData.viewall = false;
}
return (
<>
<Container>
<BlogSearch>
<Title blogTitle title={!searchTerm && 'Latest Posts'} />
<SearchBar setSearchTerm={setSearchTerm} />
</BlogSearch>
</Container>
<BlogPostsContainer {...filteredData} />
{!searchTerm &&
Object.keys(tagToHeading).map(tag => (
<BlogPostsContainer
key={tag}
posts={posts.filter(post => post.tagList.includes(tag))}
tag={tag}
/>
))}
</>
);
}
export async function getStaticProps() {
const res = await fetch('https://dev.to/api/articles?username=wdp');
const posts = await res.json();
return {
props: {
posts: posts.map(post => ({
title: post.title,
image: post.cover_image,
altTag: post.title,
content: post.description,
link: post.url,
linkText: 'Read more',
tagList: post.tag_list,
})),
},
revalidate: blogRevalidate,
};
}