Skip to content

Commit c8d9f4e

Browse files
Ameen AzeezAmeen Azeez
authored andcommitted
Feat: Pagination
1 parent d517ca9 commit c8d9f4e

File tree

5 files changed

+87
-23
lines changed

5 files changed

+87
-23
lines changed

gatsby-node.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,27 @@ exports.createPages = async ({ graphql, actions }) => {
6464
const authorTemplate = path.resolve(`./src/templates/author.js`);
6565
const pageTemplate = path.resolve(`./src/templates/page.js`);
6666
const postTemplate = path.resolve(`./src/templates/post.js`);
67+
const blogsTemplate = path.resolve(`./src/templates/blogs.js`);
6768

69+
paginate({
70+
createPage,
71+
items: posts,
72+
itemsPerPage: postsPerPage,
73+
component: blogsTemplate,
74+
pathPrefix: "blogs",
75+
context: {
76+
pathPrefix: "blogs",
77+
// slug: node.slug,
78+
},
79+
});
80+
6881
// Create tag pages
6982
tags.forEach(({ node }) => {
7083
const totalPosts = node.postCount !== null ? node.postCount : 0;
7184

7285
// This part here defines, that our tag pages will use
7386
// a `/tag/:slug/` permalink.
74-
const url = `/tag/${node.slug}`;
87+
const url = `tag/${node.slug}`;
7588

7689
const items = Array.from({ length: totalPosts });
7790

@@ -81,9 +94,9 @@ exports.createPages = async ({ graphql, actions }) => {
8194
items: items,
8295
itemsPerPage: postsPerPage,
8396
component: tagsTemplate,
84-
pathPrefix: ({ pageNumber }) =>
85-
pageNumber === 0 ? url : `${url}/page`,
97+
pathPrefix: url,
8698
context: {
99+
pathPrefix: url,
87100
slug: node.slug,
88101
},
89102
});

package-lock.json

Lines changed: 20 additions & 0 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
@@ -66,6 +66,7 @@
6666
"react": "18.2.0",
6767
"react-dom": "18.2.0",
6868
"react-helmet": "6.1.0",
69+
"react-paginate": "^8.1.4",
6970
"url": "0.11.0"
7071
}
7172
}

src/components/common/Pagination.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
import * as React from "react";
22
import PropTypes from "prop-types";
33
import { Link } from "gatsby";
4+
import ReactPaginate from 'react-paginate';
5+
import { navigate } from "gatsby"
6+
47

58
const Pagination = ({ pageContext }) => {
6-
const { previousPagePath, nextPagePath, humanPageNumber, numberOfPages } =
9+
const { previousPagePath, nextPagePath, pageNumber, numberOfPages, pathPrefix } =
710
pageContext;
811

12+
console.log(pageContext)
13+
14+
function handlePageClick(e) {
15+
if (e.selected == 0) {
16+
navigate(`/${pathPrefix}`)
17+
} else {
18+
navigate(`/${pathPrefix}/${e.selected + 1}`)
19+
}
20+
}
21+
922
return (
1023
<nav className="pagination" role="navigation">
11-
<div>
12-
{previousPagePath && (
13-
<Link to={previousPagePath} rel="prev">
14-
Previous
15-
</Link>
16-
)}
17-
</div>
18-
{numberOfPages > 1 && (
19-
<div className="pagination-location">
20-
Page {humanPageNumber} of {numberOfPages}
21-
</div>
22-
)}
23-
<div>
24-
{nextPagePath && (
25-
<Link to={nextPagePath} rel="next">
26-
Next
27-
</Link>
28-
)}
29-
</div>
24+
<ReactPaginate
25+
breakLabel="."
26+
containerClassName={"pagination"}
27+
nextLabel=">"
28+
onPageChange={handlePageClick}
29+
pageRangeDisplayed={3}
30+
pageCount={numberOfPages}
31+
previousLabel="<"
32+
marginPagesDisplayed={2}
33+
renderOnZeroPageCount={null}
34+
initialPage={pageNumber}
35+
/>
3036
</nav>
3137
);
3238
};

src/styles/global.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,27 @@
4747

4848
}
4949
}
50+
51+
52+
.pagination{
53+
display: flex;
54+
flex-direction: row;
55+
justify-content: space-around;
56+
}
57+
58+
.pagination li {
59+
/* border: 0.1px solid gray; */
60+
padding: 0.1rem 0.5rem;
61+
cursor: pointer;
62+
display: block;
63+
font-weight: bold;
64+
@apply text-gray-400 md:text-lg w-10 text-center mt-auto rounded-full;
65+
}
66+
67+
.pagination .selected {
68+
padding: 0.1rem 0.5rem;
69+
cursor: pointer;
70+
display: block;
71+
font-weight: bold;
72+
@apply text-orange-700 bg-orange-100;
73+
}

0 commit comments

Comments
 (0)