Skip to content

Commit 6d2047f

Browse files
committed
complete search function in blog page
1 parent 09de361 commit 6d2047f

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

components/blog/SearchBar.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import styles from '@/styles/SearchBar.module.scss';
22

3-
const SearchBar = () => {
3+
const SearchBar = ({ posts, setSearchTerm, setSearchResults }) => {
4+
const handleChange = e => {
5+
if (!e.target.value) return setSearchResults(null);
6+
const results = posts.filter(post =>
7+
post.title.toLowerCase().includes(e.target.value.toLowerCase())
8+
);
9+
setSearchResults(results);
10+
setSearchTerm(e.target.value);
11+
};
412
return (
513
<div className={styles.searchBar}>
6-
<input type='search' placeholder='keyword or topic' />
14+
<input
15+
type='search'
16+
placeholder='keyword or topic'
17+
onChange={handleChange}
18+
/>
719
<button type='submit'>Search</button>
820
</div>
921
);

pages/blog/index.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,52 @@
1+
import { useEffect, useState } from 'react';
12
import BlogPostsContainer from '@/components/blog/BlogPostsContainer';
2-
import Container from '@/components/containers/Container';
33
import SearchBar from '@/components/blog/SearchBar';
4-
import styles from '@/styles/Blog.module.scss';
54
import Title from '@/components/snippets/Title';
5+
import styles from '@/styles/Blog.module.scss';
66
import { blogRevalidate } from '@/utils/config';
77
import { tagToHeading } from '@/utils/blogCategories';
88

99
export default function Blog({ posts }) {
10-
const latestPosts = posts.slice(0, 3);
10+
const [searchResults, setSearchResults] = useState(null);
11+
const [searchTerm, setSearchTerm] = useState('temp');
1112
const getPostsByTag = tag => {
1213
return posts.filter(post => post.tagList.includes(tag));
1314
};
1415

16+
useEffect(() => {
17+
console.log('search result', searchResults);
18+
}, [searchResults]);
19+
1520
return (
1621
<>
1722
<div className={styles.blogSearch}>
18-
<Title customClass='blogTitle' title='Latest Posts' />
19-
<SearchBar />
23+
<Title
24+
customClass='blogTitle'
25+
title={searchResults ? '' : 'Latest Posts'}
26+
/>
27+
<SearchBar
28+
posts={posts}
29+
setSearchTerm={setSearchTerm}
30+
setSearchResults={setSearchResults}
31+
/>
2032
</div>
21-
<BlogPostsContainer posts={latestPosts} />
22-
{Object.keys(tagToHeading).map(tag => (
23-
<BlogPostsContainer key={tag} posts={getPostsByTag(tag)} tag={tag} />
24-
))}
33+
{searchResults ? (
34+
<BlogPostsContainer
35+
posts={searchResults}
36+
heading={`${searchResults.length} search Results for '${searchTerm}'`}
37+
/>
38+
) : (
39+
<>
40+
<BlogPostsContainer posts={posts.slice(0, 3)} />
41+
{Object.keys(tagToHeading).map(tag => (
42+
<BlogPostsContainer
43+
key={tag}
44+
posts={getPostsByTag(tag)}
45+
tag={tag}
46+
/>
47+
))}
48+
</>
49+
)}
2550
</>
2651
);
2752
}

styles/Blog.module.scss

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
display: flex;
2626
align-items: center;
2727
padding-top: 2.5rem;
28-
margin-bottom: -2.5rem;
29-
//TO-DO: Add the style below back when implementing search functionality
30-
// margin: 0 auto;
31-
// width: 90%;
32-
// max-width: $large-desktop-breakpoint;
28+
margin: 0 auto;
29+
width: 90%;
30+
max-width: $large-desktop-breakpoint;
3331

3432
@include mobile {
3533
flex-direction: column-reverse;

styles/Title.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
margin: 0 auto 1rem;
88
width: 90%;
99
max-width: $large-desktop-breakpoint;
10+
min-height: 5rem;
1011
}
1112
}

0 commit comments

Comments
 (0)