Skip to content

Commit 769d791

Browse files
author
Tharanishwaran
committed
ui-implement prev/next navigation buttons
Signed-off-by: Tharanishwaran <your-email@example.com>
1 parent 807d3a5 commit 769d791

File tree

2 files changed

+179
-29
lines changed

2 files changed

+179
-29
lines changed

src/sections/Resources/Resources-grid/paginate.js

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,90 @@
11
import React from "react";
22
import { ResourcePageWrapper } from "./resourceGrid.style";
3+
import { FaChevronLeft } from "@react-icons/all-files/fa/FaChevronLeft";
4+
import { FaChevronRight } from "@react-icons/all-files/fa/FaChevronRight";
35

46
const Pagination = ({ postsPerPage, totalPosts, paginate, currentPage }) => {
57
const pageNumbers = [];
8+
const totalPages = Math.ceil(totalPosts / postsPerPage);
69

7-
for (let i = 1; i <= Math.ceil(totalPosts / postsPerPage); i++) {
10+
for (let i = 1; i <= totalPages; i++) {
811
pageNumbers.push(i);
912
}
1013

14+
const handlePrevious = () => {
15+
if (currentPage > 1) {
16+
paginate(currentPage - 1);
17+
}
18+
};
19+
20+
const handleNext = () => {
21+
if (currentPage < totalPages) {
22+
paginate(currentPage + 1);
23+
}
24+
};
25+
26+
const getVisiblePageNumbers = () => {
27+
if (totalPages <= 5) {
28+
return pageNumbers;
29+
}
30+
31+
const visiblePages = [];
32+
33+
visiblePages.push(1);
34+
35+
if (currentPage > 3) {
36+
visiblePages.push("...");
37+
}
38+
39+
for (let i = Math.max(2, currentPage - 1); i <= Math.min(currentPage + 1, totalPages - 1); i++) {
40+
visiblePages.push(i);
41+
}
42+
43+
if (currentPage < totalPages - 2) {
44+
visiblePages.push("...");
45+
}
46+
47+
if (totalPages > 1) {
48+
visiblePages.push(totalPages);
49+
}
50+
51+
return visiblePages;
52+
};
53+
54+
const visiblePageNumbers = getVisiblePageNumbers();
55+
1156
return (
1257
<ResourcePageWrapper>
13-
<div className="btn-container">
14-
{pageNumbers.map(number => (
15-
<button key={number} onClick={() => paginate(number)} className={(number === currentPage) ? "btn active-btn" : "btn page-btn"}>
16-
{number}
17-
</button>
18-
))}
58+
<div className="pagination-container">
59+
<button
60+
onClick={handlePrevious}
61+
className={`pagination-btn prev-btn ${currentPage === 1 ? "disabled-btn" : ""}`}
62+
disabled={currentPage === 1}
63+
>
64+
<FaChevronLeft className="arrow-icon" /> Previous
65+
</button>
66+
67+
<div className="page-numbers">
68+
{visiblePageNumbers.map((item, index) => (
69+
item === "..." ?
70+
<span key={`ellipsis-${index}`} className="ellipsis">...</span> :
71+
<button
72+
key={item}
73+
onClick={() => paginate(item)}
74+
className={item === currentPage ? "page-btn active-btn" : "page-btn"}
75+
>
76+
{item}
77+
</button>
78+
))}
79+
</div>
80+
81+
<button
82+
onClick={handleNext}
83+
className={`pagination-btn next-btn ${currentPage === totalPages ? "disabled-btn" : ""}`}
84+
disabled={currentPage === totalPages}
85+
>
86+
Next <FaChevronRight className="arrow-icon" />
87+
</button>
1988
</div>
2089
</ResourcePageWrapper>
2190
);

src/sections/Resources/Resources-grid/resourceGrid.style.js

Lines changed: 103 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,120 @@ export const ResourcePageWrapper = styled.div`
3333
margin:0;
3434
}
3535
}
36-
.btn-container {
36+
37+
.pagination-container {
3738
display: flex;
3839
justify-content: center;
39-
flex-wrap: wrap;
40-
margin: 0 auto 2rem;
41-
}
42-
.btn {
43-
width: 2rem;
44-
height: 2rem;
40+
align-items: center;
41+
margin: 2rem auto;
42+
max-width: 600px;
43+
border-radius: 30px;
44+
background-color: ${props => props.theme.grey212121ToWhite};
45+
padding: 0.5rem;
46+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
47+
}
48+
49+
.page-numbers {
50+
display: flex;
51+
align-items: center;
52+
justify-content: center;
53+
}
54+
55+
.page-btn {
56+
width: 2.5rem;
57+
height: 2.5rem;
58+
display: flex;
59+
align-items: center;
60+
justify-content: center;
61+
margin: 0 0.25rem;
4562
border-radius: 5px;
63+
border: none;
64+
background: transparent;
65+
color: ${props => props.theme.whiteEightToBlack};
66+
font-weight: 500;
4667
cursor: pointer;
47-
margin: 0.5rem;
48-
transition: all 0.3s linear;
49-
:hover {
50-
box-shadow: 0px 1px 5px 1px rgba(0, 179, 159, 0.5);
68+
transition: all 0.2s ease;
69+
70+
&:hover {
71+
background: ${props => props.theme.secondaryColor};
72+
color: white;
5173
}
52-
}
53-
.page-btn {
54-
background: ${props => props.theme.grey212121ToWhite};
55-
border: solid 2px;
56-
border-color: ${props => props.theme.secondaryColor};
57-
color: ${props => props.theme.whiteEightToBlack};
58-
}
59-
.active-btn {
74+
}
75+
76+
.active-btn {
6077
background: ${props => props.theme.secondaryColor};
61-
border-color: transparent;
62-
color: #fff;
63-
}
78+
color: white;
79+
}
80+
81+
.pagination-btn {
82+
display: flex;
83+
align-items: center;
84+
justify-content: center;
85+
padding: 0.5rem 1rem;
86+
border: none;
87+
background: transparent;
88+
color: ${props => props.theme.whiteEightToBlack};
89+
font-weight: 500;
90+
cursor: pointer;
91+
transition: all 0.2s ease;
92+
border-radius: 5px;
93+
94+
&:hover:not(.disabled-btn) {
95+
background: ${props => props.theme.secondaryColor};
96+
color: white;
97+
}
98+
99+
.arrow-icon {
100+
font-size: 0.8rem;
101+
margin: 0 0.5rem;
102+
}
103+
}
104+
105+
.prev-btn {
106+
margin-right: auto;
107+
}
108+
109+
.next-btn {
110+
margin-left: auto;
111+
}
112+
113+
.disabled-btn {
114+
opacity: 0.5;
115+
cursor: not-allowed;
116+
&:hover {
117+
background: transparent;
118+
box-shadow: none;
119+
}
120+
}
121+
122+
.ellipsis {
123+
width: 2rem;
124+
display: flex;
125+
align-items: center;
126+
justify-content: center;
127+
color: ${props => props.theme.whiteEightToBlack};
128+
}
64129
65130
@media only screen and (max-width: 575px) {
66131
.resource-grid-wrapper{
67132
margin: 0 auto 5rem;
68133
}
134+
135+
.pagination-container {
136+
flex-wrap: wrap;
137+
padding: 0.5rem 0.25rem;
138+
}
139+
140+
.page-btn {
141+
width: 2rem;
142+
height: 2rem;
143+
margin: 0 0.1rem;
144+
}
145+
146+
.pagination-btn {
147+
padding: 0.5rem;
148+
font-size: 0.9rem;
149+
}
69150
}
70151
71152
.no-resources-page{

0 commit comments

Comments
 (0)