Skip to content

Commit b4e3022

Browse files
Merge pull request #156 from Web-Dev-Path/feature/blog-search
Add Search feature in blog page
2 parents bd6d3ff + a378547 commit b4e3022

File tree

11 files changed

+70
-30
lines changed

11 files changed

+70
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
8484
### Added
8585

8686
- Blog page which pulls content from dev.to
87+
- Search functionality for blog posts
8788

8889
### Fixed
8990

components/blog/SearchBar.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import styles from '@/styles/SearchBar.module.scss';
2+
import { useRef } from 'react';
23

3-
const SearchBar = () => {
4+
const SearchBar = ({ setSearchTerm }) => {
5+
const searchInput = useRef(null);
6+
const search = () => {
7+
setSearchTerm(searchInput.current.value);
8+
};
49
return (
510
<div className={styles.searchBar}>
6-
<input type='search' placeholder='keyword or topic' />
7-
<button type='submit'>Search</button>
11+
<input
12+
ref={searchInput}
13+
type='search'
14+
placeholder='keyword or topic'
15+
onChange={search}
16+
/>
17+
<button type='submit' onClick={search}>
18+
Search
19+
</button>
820
</div>
921
);
1022
};

pages/blog/category/[tag].js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import BlogPostsContainer from '@/components/blog/BlogPostsContainer';
22
import { paths, tagToHeading } from '@/utils/blogCategories';
33
import { useRouter } from 'next/router';
4+
import { blogRevalidate } from '@/utils/config';
45

56
export default function BlogCategory({ posts }) {
67
const router = useRouter();
@@ -33,6 +34,7 @@ export async function getStaticProps({ params }) {
3334
tagList: post.tag_list,
3435
})),
3536
},
37+
revalidate: blogRevalidate,
3638
};
3739
}
3840

pages/blog/category/all.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import BlogPostsContainer from '@/components/blog/BlogPostsContainer';
2+
import { blogRevalidate } from '@/utils/config';
23

34
export default function BlogCategory({ posts }) {
45
return (
@@ -26,5 +27,6 @@ export async function getStaticProps() {
2627
tagList: post.tag_list,
2728
})),
2829
},
30+
revalidate: blogRevalidate,
2931
};
3032
}

pages/blog/first-post.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

pages/blog/index.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
1+
import { 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';
6+
import { blogRevalidate } from '@/utils/config';
7+
import { tagToHeading } from '@/utils/blogCategories';
8+
import { blogSearch } from '@/utils/search';
69

710
export 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
}

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/SearchBar.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
background: url('/images/svg/search.svg');
3232
background-size: cover;
3333
background-repeat: no-repeat;
34-
margin: 0.5rem 1rem 0.5rem 0;
34+
margin: 0.5rem 1rem 0.5rem 1rem;
3535
cursor: pointer;
3636
}
3737
}

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
}

utils/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const blogRevalidate = 60 * 60 * 24;

0 commit comments

Comments
 (0)