Skip to content

Commit e4a2ea0

Browse files
committed
feat(pagination): integrate Pagination component for article navigation
1 parent 5f4674b commit e4a2ea0

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

src/pages/TopAfrica.jsx

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ListArticlesResult from '../components/ArticleView/ArticleList';
77
import DatePicker from '../components/ArticleForm/DatePicker';
88
import ArticleCardSkeletton from '../components/ArticleView/ArticleCardSkeletton';
99
import getTrueArticles from '../helpers/getTrueArticles';
10+
import Pagination from '../components/common/Pagination';
1011

1112
const africanCountries = countries.Africa;
1213

@@ -31,7 +32,8 @@ export default function Africa() {
3132

3233
// Pagination states
3334
const [currentPage, setCurrentPage] = useState(1);
34-
const articlesPerPage = showCard ? 12 : 18; // Display 9 or 12 items per page
35+
const [currentArticles, setCurrentArticles] = useState([]);
36+
const articlesPerPage = showCard ? 12 : 18;
3537

3638
const handleClicked = () => {
3739
setShowCard(!showCard);
@@ -84,30 +86,19 @@ export default function Africa() {
8486
fetchDataForAllCountries();
8587
}, [date]);
8688

87-
// Calculate pagination values
88-
const indexOfLastArticle = currentPage * articlesPerPage;
89-
const indexOfFirstArticle = indexOfLastArticle - articlesPerPage;
90-
const currentArticles = data.slice(indexOfFirstArticle, indexOfLastArticle);
89+
// Compute total pages
9190
const totalPages = Math.ceil(data.length / articlesPerPage);
9291

93-
// Handle pagination changes
94-
const paginate = (pageNumber) => setCurrentPage(pageNumber);
95-
96-
// Render page numbers for pagination
97-
const renderPageNumbers = () => {
98-
const pageNumbers = [];
99-
for (let i = 1; i <= totalPages; i++) {
100-
pageNumbers.push(
101-
<button
102-
key={i}
103-
onClick={() => paginate(i)}
104-
className={`px-2 py-1 mx-1 ${currentPage === i ? 'bg-blue-500 text-white' : 'bg-gray-300'}`}
105-
>
106-
{i}
107-
</button>,
108-
);
109-
}
110-
return pageNumbers;
92+
// Keep a local slice of current page articles; Pagination component will call onPageChange
93+
useEffect(() => {
94+
const initial = data.slice(0, articlesPerPage);
95+
setCurrentArticles(initial);
96+
setCurrentPage(1);
97+
}, [data, articlesPerPage]);
98+
99+
const handlePageChange = (page, paginatedItems) => {
100+
setCurrentPage(page);
101+
setCurrentArticles(paginatedItems);
111102
};
112103

113104
// Display loading component if data is loading
@@ -175,21 +166,14 @@ export default function Africa() {
175166
)}
176167

177168
<div className='flex items-center justify-center gap-4 my-5 mb-12'>
178-
<button
179-
onClick={() => paginate(currentPage - 1)}
180-
disabled={currentPage === 1}
181-
className='px-4 py-2 bg-gray-300 rounded disabled:opacity-50'
182-
>
183-
&larr;
184-
</button>
185-
<div className='flex'>{renderPageNumbers()}</div>
186-
<button
187-
onClick={() => paginate(currentPage + 1)}
188-
disabled={currentPage === totalPages}
189-
className='px-4 py-2 bg-gray-300 rounded disabled:opacity-50'
190-
>
191-
&rarr;
192-
</button>
169+
<Pagination
170+
items={data}
171+
itemsPerPage={articlesPerPage}
172+
onPageChange={handlePageChange}
173+
onCurrentChange={setCurrentPage}
174+
currentPage={currentPage}
175+
totalPages={totalPages}
176+
/>
193177
</div>
194178
</div>
195179
</main>

0 commit comments

Comments
 (0)