1+ import { useState } from 'react' ;
12import BlogPostsContainer from '@/components/blog/BlogPostsContainer' ;
2- import Container from '@/components/containers/Container' ;
33import SearchBar from '@/components/blog/SearchBar' ;
4- import styles from '@/styles/Blog.module.scss' ;
54import Title from '@/components/snippets/Title' ;
5+ import styles from '@/styles/Blog.module.scss' ;
6+ import { blogRevalidate } from '@/utils/config' ;
7+ import { tagToHeading } from '@/utils/blogCategories' ;
8+ import { blogSearch } from '@/utils/search' ;
69
710export default function Blog ( { posts } ) {
8- const latestPosts = posts . slice ( 0 , 3 ) ;
9- const nextJsPosts = posts . filter ( post => post . tagList . includes ( 'nextjs' ) ) ;
10- const typescriptPosts = posts . filter ( post =>
11- post . tagList . includes ( 'typescript' )
12- ) ;
11+ const [ searchTerm , setSearchTerm ] = useState ( '' ) ;
12+
13+ const filteredData = {
14+ posts : posts . slice ( 0 , 3 ) ,
15+ } ;
16+
17+ if ( searchTerm ) {
18+ const filteredPosts = blogSearch ( posts , searchTerm ) ;
19+ filteredData . posts = filteredPosts ;
20+ filteredData . heading = `${
21+ filteredPosts . length === 0 ? 'no' : filteredPosts . length
22+ } search ${
23+ filteredPosts . length > 1 ? 'results' : 'result'
24+ } for '${ searchTerm } '`;
25+ filteredData . viewall = false ;
26+ }
1327
1428 return (
1529 < >
1630 < div className = { styles . blogSearch } >
17- < Title customClass = 'blogTitle' title = 'Latest Posts' />
18- { /* <SearchBar /> */ }
31+ < Title customClass = 'blogTitle' title = { ! searchTerm && 'Latest Posts' } />
32+ < SearchBar setSearchTerm = { setSearchTerm } />
1933 </ div >
20- < BlogPostsContainer posts = { latestPosts } />
21- < BlogPostsContainer posts = { nextJsPosts } tag = 'nextjs' />
22- < BlogPostsContainer posts = { typescriptPosts } tag = 'typescript' />
34+ < BlogPostsContainer { ...filteredData } />
35+ { ! searchTerm &&
36+ Object . keys ( tagToHeading ) . map ( tag => (
37+ < BlogPostsContainer
38+ key = { tag }
39+ posts = { posts . filter ( post => post . tagList . includes ( tag ) ) }
40+ tag = { tag }
41+ />
42+ ) ) }
2343 </ >
2444 ) ;
2545}
@@ -40,5 +60,6 @@ export async function getStaticProps() {
4060 tagList : post . tag_list ,
4161 } ) ) ,
4262 } ,
63+ revalidate : blogRevalidate ,
4364 } ;
4465}
0 commit comments