Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added pagination to blogs #3579

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
69 changes: 68 additions & 1 deletion pages/blog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export default function BlogIndexPage() {
: []
);
const [isClient, setIsClient] = useState(false);
const [currentPage, setCurrentPage] = useState(1);
const [pageRange, setPageRange] = useState([1, 10]);
const postsPerPage = 12;

const onFilter = (data: IBlogPost[]) => setPosts(data);
const toFilter = [
Expand All @@ -59,6 +62,34 @@ export default function BlogIndexPage() {
const description = 'Find the latest and greatest stories from our community';
const image = '/img/social/blog.webp';

const handlePageChange = (pageNumber: number) => {
setCurrentPage(pageNumber);
if (pageNumber > pageRange[1] - 1) {
setPageRange([pageRange[0] + 3, pageRange[1] + 3]);
} else if (pageNumber < pageRange[0] + 1) {
const newStart = Math.max(pageRange[0] - 3, 1);
const newEnd = newStart + 9;

setPageRange([newStart, newEnd]);
}
Comment on lines +66 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide an explanation of the sequence of events once this function (handlePageChange) gets triggered?

};

const handlePrevious = () => {
if (currentPage > 1) {
handlePageChange(currentPage - 1);
}
};

const handleNext = () => {
if (currentPage < Math.ceil(posts.length / postsPerPage)) {
handlePageChange(currentPage + 1);
}
};

const indexOfLastPost = currentPage * postsPerPage;
const indexOfFirstPost = indexOfLastPost - postsPerPage;
const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost);

useEffect(() => {
setIsClient(true);
}, []);
Expand Down Expand Up @@ -122,7 +153,7 @@ export default function BlogIndexPage() {
)}
{Object.keys(posts).length > 0 && isClient && (
<ul className='mx-auto mt-12 grid max-w-lg gap-5 lg:max-w-none lg:grid-cols-3'>
{posts.map((post, index) => (
{currentPosts.map((post, index) => (
<BlogPostItem key={index} post={post} />
))}
</ul>
Expand All @@ -133,6 +164,42 @@ export default function BlogIndexPage() {
</div>
)}
</div>
<div className='mt-8 flex justify-center'>
<button
onClick={handlePrevious}
className={`mx-1 rounded-md border px-3 py-1 ${currentPage === 1 ? 'bg-gray-300' : 'bg-white'}`}
disabled={currentPage === 1}
>
Previous
</button>
{pageRange[0] > 1 && <span className='mx-1 px-3 py-1'>...</span>}
{Array.from(
{
length: Math.min(12, Math.ceil(posts.length / postsPerPage) - pageRange[0] + 1)
},
(_, index) => {
const pageNumber = pageRange[0] + index;

return (
<button
key={pageNumber}
onClick={() => handlePageChange(pageNumber)}
className={`mx-1 rounded-md border px-3 py-1 ${currentPage === pageNumber ? 'bg-gray-300' : 'bg-white'}`}
>
{pageNumber}
</button>
);
}
)}
{pageRange[1] < Math.ceil(posts.length / postsPerPage) && <span className='mx-1 px-3 py-1'>...</span>}
<button
onClick={handleNext}
className={`mx-1 rounded-md border px-3 py-1 ${currentPage === Math.ceil(posts.length / postsPerPage) ? 'bg-gray-300' : 'bg-white'}`}
disabled={currentPage === Math.ceil(posts.length / postsPerPage)}
>
Next
</button>
</div>
Comment on lines +167 to +202
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a new component file with reusable code. I feel like this should go in Navigation folder (https://github.com/asyncapi/website/tree/master/components/navigation) as pagination is kind of an on-page navigation thing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this needs to be responsive. This is currently overflowing on mobile screens.

</div>
</div>
</GenericLayout>
Expand Down
Loading