Skip to content

Commit f2e909c

Browse files
authored
Merge pull request uidotdev#33 from danedavid/pagination
Add pagination using gatsby-paginate. Fixes uidotdev#4.
2 parents a48bd9b + 267ad9a commit f2e909c

File tree

8 files changed

+121
-58
lines changed

8 files changed

+121
-58
lines changed

gatsby-node.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require("path");
22
const { createFilePath } = require("gatsby-source-filesystem");
3+
const createPaginatedPages = require('gatsby-paginate');
34

45
exports.createPages = ({ actions, graphql }) => {
56
const { createPage } = actions;
@@ -13,12 +14,24 @@ exports.createPages = ({ actions, graphql }) => {
1314
edges {
1415
node {
1516
id
17+
excerpt(pruneLength: 400)
18+
html
1619
fields {
1720
slug
1821
}
1922
frontmatter {
2023
templateKey
2124
title
25+
date(formatString: "MMMM DD, YYYY")
26+
composes
27+
gist
28+
sandbox
29+
links {
30+
url
31+
name
32+
description
33+
}
34+
code
2235
}
2336
}
2437
}
@@ -32,6 +45,15 @@ exports.createPages = ({ actions, graphql }) => {
3245

3346
let posts = result.data.allMarkdownRemark.edges;
3447

48+
createPaginatedPages({
49+
edges: posts,
50+
createPage: createPage,
51+
pageTemplate: 'src/templates/index.js',
52+
pageLength: 5, // This is optional and defaults to 10 if not used
53+
pathPrefix: '', // This is optional and defaults to an empty string if not used
54+
context: {}, // This is optional and defaults to an empty object if not used
55+
});
56+
3557
posts.forEach((edge, index) => {
3658
const id = edge.node.id;
3759

package-lock.json

Lines changed: 62 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"babel-plugin-styled-components": "^1.10.0",
77
"gatsby": "^2.0.85",
8+
"gatsby-paginate": "^1.0.17",
89
"gatsby-plugin-catch-links": "^2.0.10",
910
"gatsby-plugin-feed": "^2.0.11",
1011
"gatsby-plugin-react-helmet": "^3.0.5",

src/components/Layout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const GitHubLink = () => {
118118
href="https://github.com/gragland/usehooks"
119119
>
120120
<i
121-
class="fab fa-github"
121+
className="fab fa-github"
122122
style={{
123123
position: "absolute",
124124
fontSize: "1.5rem",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import styled from 'styled-components';
2+
3+
export default styled.div`
4+
display: flex;
5+
justify-content: space-between;
6+
`;

src/components/Search.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export default ({ value, onChange }) => {
1212
value={value}
1313
onChange={({ target: { value } }) => onChange(value)}
1414
/>
15-
<span class="icon is-small is-left">
16-
<i class="fas fa-search" />
15+
<span className="icon is-small is-left">
16+
<i className="fas fa-search" />
1717
</span>
1818
</div>
1919
</div>

src/pages/index.js renamed to src/templates/index.js

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@ import React from "react";
22
import styled from "styled-components";
33
import { Link } from "gatsby";
44
import Layout from "../components/Layout";
5+
import PaginationContainer from "../components/PaginationContainer";
56
import Search from "../components/Search";
67
import { PostTemplate } from "../templates/post.js";
78

9+
const NavLink = props => {
10+
if (!props.test) {
11+
return <Link to={`/${props.url}`}>{props.text}</Link>
12+
} else {
13+
return <span>{props.text}</span>
14+
}
15+
};
16+
817
class IndexPage extends React.Component {
918
state = { search: "" };
1019

1120
render() {
12-
const { data } = this.props;
21+
const { pageContext } = this.props;
22+
const { group, index, first, last, pageCount } = pageContext;
23+
const previousUrl = index - 1 == 1 ? '' : (index - 1).toString();
24+
const nextUrl = (index + 1).toString()
1325
const { search } = this.state;
14-
const { edges: posts } = data.allMarkdownRemark;
1526

16-
const filteredPosts = search ? searchPosts(posts, search) : posts;
27+
// const filteredPosts = search ? searchPosts(posts, search) : posts;
1728

1829
return (
1930
<Layout>
@@ -24,54 +35,26 @@ class IndexPage extends React.Component {
2435
/>
2536
*/}
2637

27-
{filteredPosts.map(post => (
38+
{group.map(({ node }) => (
2839
<PostTemplate
29-
content={post.node.html}
30-
frontmatter={post.node.frontmatter}
31-
slug={post.node.fields.slug}
40+
key={node.id}
41+
content={node.excerpt}
42+
frontmatter={node.frontmatter}
43+
slug={node.fields.slug}
3244
/>
3345
))}
46+
<PaginationContainer>
47+
<NavLink test={first} url={previousUrl} text="<<< Previous" />
48+
<span>{` Page ${index} of ${pageCount}`}</span>
49+
<NavLink test={last} url={nextUrl} text="Next >>>" />
50+
</PaginationContainer>
3451
</Layout>
3552
);
3653
}
3754
}
3855

3956
export default IndexPage;
4057

41-
export const pageQuery = graphql`
42-
query IndexQuery {
43-
allMarkdownRemark(
44-
sort: { order: DESC, fields: [frontmatter___date] }
45-
filter: { frontmatter: { templateKey: { eq: "post" } } }
46-
) {
47-
edges {
48-
node {
49-
excerpt(pruneLength: 400)
50-
id
51-
html
52-
fields {
53-
slug
54-
}
55-
frontmatter {
56-
templateKey
57-
title
58-
date(formatString: "MMMM DD, YYYY")
59-
composes
60-
gist
61-
sandbox
62-
links {
63-
url
64-
name
65-
description
66-
}
67-
code
68-
}
69-
}
70-
}
71-
}
72-
}
73-
`;
74-
7558
// Quicky and hacky search
7659
function searchPosts(posts, search) {
7760
// Store title matches for quick lookup
@@ -104,4 +87,4 @@ function searchPosts(posts, search) {
10487
// Add description matches to end of results
10588
const filteredPosts = filterPostsByTitle.concat(filterPostsByDescription);
10689
return filteredPosts;
107-
}
90+
}

0 commit comments

Comments
 (0)