Add pagination

This commit is contained in:
alxshelepenok
2018-11-11 14:19:06 +03:00
parent 38090a3a0f
commit 338317803e
24 changed files with 404 additions and 48 deletions

View File

@@ -14,16 +14,21 @@ module.exports = async (graphql, actions) => {
}
`);
const numPages = Math.ceil(result.data.allMarkdownRemark.totalCount / siteConfig.postsPerPage);
const { postsPerPage } = siteConfig;
const numPages = Math.ceil(result.data.allMarkdownRemark.totalCount / postsPerPage);
for (let i = 0; i < numPages; i += 1) {
createPage({
path: i === 0 ? '/' : `/page/${i}`,
component: path.resolve('./src/templates/index-template.js'),
context: {
page: i,
limit: siteConfig.postsPerPage,
skip: i * siteConfig.postsPerPage,
currentPage: i,
postsLimit: postsPerPage,
postsOffset: i * postsPerPage,
prevPagePath: i <= 1 ? '/' : `/page/${i - 1}`,
nextPagePath: `/page/${i + 1}`,
hasPrevPage: i !== 0,
hasNextPage: i !== numPages - 1
}
});
}