Skip to content

Commit a728393

Browse files
authored
Merge pull request #8 from ameen4455/pagination
Feat: Pagination
2 parents cb27fe9 + 6c69d72 commit a728393

File tree

5 files changed

+87
-31
lines changed

5 files changed

+87
-31
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 & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +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

9-
return (
10-
<nav className="flex justify-center">
11-
<ul class="flex">
12-
<div>
13-
{previousPagePath && (
12+
console.log(pageContext)
1413

15-
<Link
16-
className="underline relative block py-1.5 px-3 rounded border-0 outline-none transition-all duration-300 rounded text-gray-800 hover:text-gray-800 hover:bg-gray-200 focus:shadow-none"
17-
to={previousPagePath} rel="prev">
18-
Previous
19-
</Link>
14+
function handlePageClick(e) {
15+
if (e.selected == 0) {
16+
navigate(`/${pathPrefix}`)
17+
} else {
18+
navigate(`/${pathPrefix}/${e.selected + 1}`)
19+
}
20+
}
2021

21-
)}
22-
</div>
23-
{numberOfPages > 1 && (
24-
<div className="relative block mx-2 py-1.5 px-3 rounded border-2 transition-all duration-300 rounded">
25-
Page {humanPageNumber} of {numberOfPages}
26-
</div>
27-
)}
28-
<div>
29-
{nextPagePath && (
30-
<Link
31-
className="underline relative block py-1.5 px-3 rounded border-0 bg-transparent outline-none transition-all duration-300 rounded text-gray-800 hover:text-gray-800 hover:bg-gray-200 focus:shadow-none"
32-
to={nextPagePath} rel="next">
33-
Next
34-
</Link>
35-
)}
36-
</div>
37-
</ul>
22+
return (
23+
<nav className="pagination" role="navigation">
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+
/>
3836
</nav>
3937
);
4038
};

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)