Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination2 #62

Merged
merged 4 commits into from Mar 6, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+46 −2
Diff settings

Always

Just for now

@@ -1,6 +1,7 @@
const _ = require("lodash")
const path = require("path")
const { createFilePath } = require("gatsby-source-filesystem")
const { paginate } = require("gatsby-awesome-pagination")

const getTemplate = name => {
return path.resolve(`src/templates/${String(name)}.js`)
@@ -87,6 +88,17 @@ exports.createPages = async ({ actions, graphql }) => {
})
})
}

const buildPaginate = posts => {
paginate({
createPage,
items: posts,
itemsPerPage: 10,
pathPrefix: ({ pageNumber }) => (pageNumber === 0 ? "/" : "/page"),
component: getTemplate("index")
})
}

// execute
graphql(ql).then(result => {
if (result.errors) {
@@ -98,6 +110,7 @@ exports.createPages = async ({ actions, graphql }) => {
const posts = result.data.allMarkdownRemark.edges
buildPages(posts)
buildTagPages(posts)
buildPaginate(posts)
})
}

@@ -16,6 +16,7 @@
"dotenv": "^6.2.0",
"dotenv-cli": "^1.4.0",
"gatsby": "^2.1.17",
"gatsby-awesome-pagination": "^0.3.3",
"gatsby-link": "^2.0.0",
"gatsby-mdx": "^0.4.0",
"gatsby-plugin-canonical-urls": "^2.0.10",
@@ -1,10 +1,30 @@
import React from "react"
// import PropTypes from "prop-types";
import { BlogItem, BlogList } from "../../app/list/Item"
import { graphql } from "gatsby"
import { graphql, Link } from "gatsby"
import { generatePostFragment } from "../query/query"
import { BlogLayout } from "../provider/BlogLayout"
import styled from "styled-components"
import { headerFont } from "../../app/utils/typography"

const PaginateContainer = styled.div`
padding: 1em;
justify-content: space-evenly;
width: 100%;
display: flex;
font-weight: bold;
font-size: 1.2em;
font-family: ${headerFont};
`

const Paginate = ({ previousPagePath, nextPagePath }) => {
return (
<PaginateContainer>
{previousPagePath && <Link to={previousPagePath}>≪Prev</Link>}
{nextPagePath && <Link to={nextPagePath}>Next≫</Link>}
</PaginateContainer>
)
}
export default class IndexPage extends React.Component {
render() {
const { data } = this.props
@@ -13,14 +33,15 @@ export default class IndexPage extends React.Component {
return (
<BlogLayout>
<BlogList posts={posts} />
<Paginate {...this.props.pathContext} />
</BlogLayout>
)
}
}

export const postFragment = generatePostFragment()
export const query = graphql`
query IndexQuery {
query IndexQuery($skip: Int!, $limit: Int!) {
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
filter: {
@@ -29,6 +50,8 @@ export const query = graphql`
published: { ne: false }
}
}
skip: $skip
limit: $limit
) {
edges {
node {
@@ -6281,6 +6281,13 @@ fuzzy@^0.1.1:
resolved "https://registry.yarnpkg.com/fuzzy/-/fuzzy-0.1.3.tgz#4c76ec2ff0ac1a36a9dccf9a00df8623078d4ed8"
integrity sha1-THbsL/CsGjap3M+aAN+GIweNTtg=

gatsby-awesome-pagination@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/gatsby-awesome-pagination/-/gatsby-awesome-pagination-0.3.3.tgz#36c93ba2b39f3c408277b1809501d1eda94dd14a"
integrity sha512-ajvVcpo0a5mzzr1tgeTv9iKRd4sdtzEEuFq1CBbEQebRhiAcjDR3S36qzKdn+GwMAGFi5Ngs/2ZslxxSUCtLfw==
dependencies:
lodash "^4.17.10"

gatsby-cli@^2.4.11:
version "2.4.11"
resolved "https://registry.yarnpkg.com/gatsby-cli/-/gatsby-cli-2.4.11.tgz#f0ee2407cfef0714be375e3bb5840508d8be77af"
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.