Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions utils/paginate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ module.exports = (
totalPages,
perPage = 3
) => {
const pages = Math.ceil(totalPages / perPage);
_.times(pages, index => {
const pageCount = Math.ceil(totalPages / perPage);
_.times(pageCount, index => {
createPage({
// Calculate the path for this page like `/blog`, `/blog/2`
path: paginationPath(basePath, index, totalPages),
path: paginationPath(basePath, index, pageCount),
// Set the component as normal
component: componentPath,
// Pass the following context to the component
Expand All @@ -36,11 +36,11 @@ module.exports = (
// How many posts to show on this paginated page
limit: perPage,
// How many paginated pages there are in total
totalPages,
totalPages: pageCount,
// The path to the previous paginated page (or an empty string)
prevPath: paginationPath(basePath, index - 1, totalPages),
prevPath: paginationPath(basePath, index - 1, pageCount),
// The path to the next paginated page (or an empty string)
nextPath: paginationPath(basePath, index + 1, totalPages),
nextPath: paginationPath(basePath, index + 1, pageCount),
// Current page ID for displaying between chevrons
pageID: index + 1
}
Expand Down
22 changes: 22 additions & 0 deletions utils/paginate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const test = require('node:test');
const assert = require('node:assert/strict');

const paginate = require('./paginate');

test('stops generating a next page link on the final paginated page', () => {
const createdPages = [];

paginate(
page => createdPages.push(page),
'/tmp/component',
'/page',
160,
10
);

assert.equal(createdPages.length, 16);
assert.equal(createdPages[0].context.nextPath, '/page/2');
assert.equal(createdPages[14].context.nextPath, '/page/16');
assert.equal(createdPages[15].context.pageID, 16);
assert.equal(createdPages[15].context.nextPath, '');
});