-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathBlogList.jsx
More file actions
172 lines (161 loc) · 5 KB
/
BlogList.jsx
File metadata and controls
172 lines (161 loc) · 5 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { useState, useEffect } from "react";
import { Link, graphql } from "gatsby";
import { FiArrowLeft, FiArrowRight } from "react-icons/fi";
import { Row, Col } from "reactstrap";
import Layout from "../layout";
import PostListing from "../components/PostListing/PostListing";
import SEO from "../components/SEO/SEO";
import BlogSearchForm from "../components/BlogSearchForm/BlogSearchForm";
import SearchResults from "../components/SearchResult/SearchResult";
import config from "../../data/SiteConfig";
import blogList from "../generated/blog-result.json";
function BlogList({ data, pageContext, location }) {
const { blogCurrentPage, postsNumPages } = pageContext;
const postEdges = data.allMarkdownRemark.edges;
const blogData = blogList;
const prefix = "/blog";
const isFirst = blogCurrentPage === 1;
const isLast = blogCurrentPage === postsNumPages;
const prevPage =
blogCurrentPage - 1 === 1 ? "" : (blogCurrentPage - 1).toString();
const nextPage = (blogCurrentPage + 1).toString();
const [isShown, setIsShown] = useState(false);
const [results, setResults] = useState([]);
let srcLocation = location;
if (typeof window !== `undefined`) {
srcLocation = location.search;
}
const searchQuery = new URLSearchParams(srcLocation).get("keywords") || "";
const filterTag = new URLSearchParams(srcLocation).get("tag") || "";
const filterAuthor = new URLSearchParams(srcLocation).get("author") || "";
const filterdate = new URLSearchParams(srcLocation).get("date") || "";
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
useEffect(() => {
if (searchQuery || filterTag || filterAuthor || filterdate) {
setResults(
blogData.filter((blogPost) => {
const searchRgx = new RegExp(escapeRegExp(searchQuery), "gi");
const tagRgx = new RegExp(escapeRegExp(filterTag), "gi");
const authorRgx = new RegExp(escapeRegExp(filterAuthor), "gi");
const dateRgx = new RegExp(escapeRegExp(filterdate), "gi");
const matchedTag = blogPost.tags
.filter((tag) => tag != null)
.map((t) => t.match(tagRgx));
return (
(blogPost.content?.match(searchRgx) ||
blogPost.title?.match(searchRgx)) &&
matchedTag.toString().match(tagRgx) &&
blogPost.author?.match(authorRgx) &&
blogPost.date?.match(dateRgx)
);
})
);
setIsShown(true);
} else {
setResults([]);
setIsShown(false);
}
}, [blogData, filterAuthor, filterTag, filterdate, searchQuery]);
const postList = postEdges.map(({ node }) => {
const { frontmatter, fields, excerpt } = node;
const { posttype, tags, cover, title, author } = frontmatter;
const { slug, date } = fields;
return {
posttype,
title,
path: `${prefix}${slug}-${date
.replace(/[-T:.Z]/g, "-")
.substring(0, 10)}`,
cover,
tags,
excerpt,
date,
author,
};
});
return (
<Layout title="Blog">
<div className="index-container">
<BlogSearchForm
query={searchQuery}
tag={filterTag}
author={filterAuthor}
date={filterdate}
location={location}
prefix={prefix}
/>
{isShown && (
<SearchResults query={searchQuery} results={results} type="blog" />
)}
{!isShown && <PostListing postList={postList} />}
</div>
<Row>
{!isFirst && results.length === 0 && (
<Col className="text-center m-4">
<Link
to={`${prefix}/${prevPage}`}
rel="prev"
className="btn-primary"
>
<FiArrowLeft />
{` Previous Page`}
</Link>
</Col>
)}
{!isLast && results.length === 0 && (
<Col className="text-center m-4">
<Link
to={`${prefix}/${nextPage}`}
rel="next"
className="btn-primary"
>
{`Next Page `}
<FiArrowRight />
</Link>
</Col>
)}
</Row>
</Layout>
);
}
/* eslint no-undef: "off" */
export const blogQuery = graphql`
query blogQuery($skip: Int!, $limit: Int!) {
allMarkdownRemark(
sort: { frontmatter: { date: DESC } }
filter: { frontmatter: { posttype: { eq: "blog" } } }
limit: $limit
skip: $skip
) {
edges {
node {
fields {
slug
date
}
excerpt(format: PLAIN, pruneLength: 120, truncate: true)
timeToRead
frontmatter {
title
date
author
tags
posttype
description
cover {
childImageSharp {
gatsbyImageData
}
}
}
}
}
}
}
`;
export default BlogList;
export function Head() {
return <SEO title={`Blog | ${config.siteTitle}`} />;
}