Skip to content

Commit

Permalink
Added pagination 9 posts / page in posts.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetDOD committed Jun 1, 2024
1 parent 06faa9f commit f50a2f0
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion frontend/src/pages/Posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const Posts = () => {
const [filterTags, setFilterTags] = useState<string[]>([]);
const filterRef = useRef<HTMLDivElement>(null);
const [searchQuery, setSearchQuery] = useState('');
const [currentPage, setCurrentPage] = useState(1);
const postsPerPage = 9;

useEffect(() => {
const fetchPosts = async () => {
Expand Down Expand Up @@ -71,6 +73,25 @@ const Posts = () => {
post.author.username.toLowerCase().includes(searchQuery.toLowerCase()))
);

const totalPages = Math.ceil(filteredPosts.length / postsPerPage);

const currentPosts = filteredPosts.slice(
(currentPage - 1) * postsPerPage,
currentPage * postsPerPage
);

const handlePreviousPage = () => {
if (currentPage > 1) {
setCurrentPage(currentPage - 1);
}
};

const handleNextPage = () => {
if (currentPage < totalPages) {
setCurrentPage(currentPage + 1);
}
};

if (loading) {
return <Loader />;
}
Expand Down Expand Up @@ -139,10 +160,35 @@ const Posts = () => {
/>
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4 w-full">
{filteredPosts.map((post, index) => (
{currentPosts.map((post, index) => (
<PostCard key={index} post={post} />
))}
</div>
<div className="flex justify-center items-center mt-4">
<button
onClick={handlePreviousPage}
disabled={currentPage === 1}
className={`px-4 py-2 mx-1 text-white rounded ${currentPage === 1 ? 'bg-gray-600 cursor-not-allowed' : 'bg-blue-600 hover:bg-blue-700'}`}
>
Previous
</button>
{[...Array(totalPages)].map((_, index) => (
<button
key={index}
onClick={() => setCurrentPage(index + 1)}
className={`px-4 py-2 mx-1 text-white rounded ${currentPage === index + 1 ? 'bg-blue-500' : 'bg-blue-600 hover:bg-blue-700'}`}
>
{index + 1}
</button>
))}
<button
onClick={handleNextPage}
disabled={currentPage === totalPages}
className={`px-6 py-2 mx-1 text-white rounded ${currentPage === totalPages ? 'bg-gray-600 cursor-not-allowed' : 'bg-blue-600 hover:bg-blue-700'}`}
>
Next
</button>
</div>
</div>
);
};
Expand Down

0 comments on commit f50a2f0

Please sign in to comment.