This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
/
category.js
124 lines (114 loc) · 2.73 KB
/
category.js
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
import { FaTag } from "react-icons/fa/";
import PropTypes from "prop-types";
import React from "react";
import { graphql } from "gatsby";
import { ThemeContext } from "../layouts";
import Article from "../components/Article/";
import Headline from "../components/Article/Headline";
import List from "../components/List";
import Seo from "../components/Seo";
const CategoryPage = props => {
const {
data: {
posts: { edges: posts },
site: {
siteMetadata: { facebook }
}
}
} = props;
// Create category list
const categories = {};
posts.forEach(edge => {
const {
node: {
frontmatter: { category }
}
} = edge;
if (category && category != null) {
if (!categories[category]) {
categories[category] = [];
}
categories[category].push(edge);
}
});
const categoryList = [];
for (var key in categories) {
categoryList.push([key, categories[key]]);
}
return (
<React.Fragment>
<ThemeContext.Consumer>
{theme => (
<Article theme={theme}>
<header>
<Headline title="Posts by categories" theme={theme} />
</header>
{categoryList.map(item => (
<section key={item[0]}>
<h2>
<FaTag /> {item[0]}
</h2>
<List edges={item[1]} theme={theme} />
</section>
))}
{/* --- STYLES --- */}
<style jsx>{`
h2 {
margin: 0 0 0.5em;
}
h2 :global(svg) {
height: 0.8em;
fill: ${theme.color.brand.primary};
}
`}</style>
</Article>
)}
</ThemeContext.Consumer>
<Seo facebook={facebook} />
</React.Fragment>
);
};
CategoryPage.propTypes = {
data: PropTypes.object.isRequired
};
export default CategoryPage;
//eslint-disable-next-line no-undef
export const query = graphql`
query PostsQuery {
posts: allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "//posts/[0-9]+.*--/" } }
sort: { fields: [fields___prefix], order: DESC }
) {
edges {
node {
excerpt
fields {
slug
prefix
}
frontmatter {
title
category
author
cover {
children {
... on ImageSharp {
fluid(maxWidth: 800, maxHeight: 360) {
...GatsbyImageSharpFluid_withWebp
}
}
}
}
}
}
}
}
site {
siteMetadata {
facebook {
appId
}
}
}
}
`;