Skip to content

Commit 51a4894

Browse files
committed
refactor tag page
1 parent f8f6aee commit 51a4894

File tree

5 files changed

+74
-111
lines changed

5 files changed

+74
-111
lines changed

gatsby-node.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -444,19 +444,6 @@ exports.createPages = async ({ graphql, actions }) => {
444444
},
445445
})
446446
})
447-
448-
// Create tag pages
449-
const tagTemplate = path.resolve("src/templates/tags.tsx")
450-
const tags = result.data.tagsGroup.group
451-
tags.forEach(tag => {
452-
createPage({
453-
path: `/tags/${tag.fieldValue}/`,
454-
component: tagTemplate,
455-
context: {
456-
tag: tag.fieldValue,
457-
},
458-
})
459-
})
460447
}
461448

462449
exports.onCreateWebpackConfig = ({ actions }) => {

src/components/BlogSidebar/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ const BlogSidebar: React.FC = () => {
4545
return (
4646
<li key={tag}>
4747
{pathname === currentPath ? (
48-
tag
48+
formattedTag
4949
) : (
50-
<Link to={`/tags/${tag}`}>{formattedTag}</Link>
50+
<Link to={pathname}>{formattedTag}</Link>
5151
)}
5252
</li>
5353
)

src/pages/blog/{BlogPost.postId}.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as React from "react"
2+
import type { PageProps } from "gatsby"
3+
import { graphql } from "gatsby"
4+
import Layout from "../../components/Layout"
5+
import BlogLayout from "../../components/BlogLayout"
6+
7+
export const query = graphql`
8+
query BlogPostPage($id: String!) {
9+
blogPost(id: { eq: $id }) {
10+
title
11+
...BlogLayout_post
12+
}
13+
}
14+
`
15+
16+
type Props = PageProps<GatsbyTypes.BlogPostPageQuery, GatsbyTypes.SitePageContext>
17+
18+
const BlogPostPage: React.FC<Props> = ({
19+
data
20+
}) => {
21+
// Always exist since it is collected by Gatsby filesystem route API
22+
const post = data.blogPost!
23+
24+
return (
25+
<Layout title={`${post.title} | GraphQL`} pageContext={{}}>
26+
<BlogLayout post={post} />
27+
</Layout>
28+
)
29+
}
30+
31+
export default BlogPostPage

src/pages/tags/{BlogPost.tags}.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as React from "react"
2+
import type { PageProps } from "gatsby"
3+
import { graphql } from "gatsby"
4+
import Layout from "../../components/Layout"
5+
import BlogPostPreview from "../../components/BlogPostPreview"
6+
import BlogSidebar from "../../components/BlogSidebar"
7+
8+
export const query = graphql`
9+
query TagPage($tags: [String!]!) {
10+
allBlogPost(
11+
filter: { tags: { in: $tags } }
12+
) {
13+
nodes {
14+
id
15+
...BlogPostPreview_post
16+
}
17+
}
18+
}
19+
`
20+
21+
type Props = PageProps<GatsbyTypes.TagPageQuery, GatsbyTypes.SitePageContext>
22+
23+
const TagPage: React.FC<Props> = ({ data, params }) => {
24+
const currentTag = params.tags!
25+
return (
26+
<Layout title={`Blog: ${currentTag} | GraphQL`} pageContext={{}}>
27+
<section>
28+
<div className="documentationContent">
29+
<div>
30+
{data.allBlogPost.nodes.map(post => (
31+
<BlogPostPreview key={post.id} post={post} />
32+
))}
33+
</div>
34+
<BlogSidebar />
35+
</div>
36+
</section>
37+
</Layout>
38+
)
39+
}
40+
41+
export default TagPage

src/templates/tags.tsx

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

0 commit comments

Comments
 (0)