Skip to content

Commit

Permalink
Add navigateToPage function for pagination handling on profile page
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-jo1 committed Sep 8, 2024
1 parent 8f4d218 commit 4d2e779
Showing 1 changed file with 60 additions and 8 deletions.
68 changes: 60 additions & 8 deletions frontend/src/components/pages/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { useNavigate, useParams, useSearchParams } from "react-router-dom";

import { format } from "date-fns";
import { paths } from "gen/api";
Expand Down Expand Up @@ -160,8 +160,20 @@ const Profile = () => {
const [canEdit, setCanEdit] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [listingIds, setListingIds] = useState<string[] | null>(null);
const [moreListings, setMoreListings] = useState<boolean>(false);

const pageNumber = parseInt("1", 10);
const [searchParams] = useSearchParams();
const navigate = useNavigate();

const page = searchParams.get("page");
const pageNumber = parseInt(page || "1", 10);
if (isNaN(pageNumber) || pageNumber < 0) {
navigate("/404");
}

const prevButton = pageNumber > 1;
const nextButton = moreListings;
const hasButton = prevButton || nextButton;

useEffect(() => {
const fetchUser = async () => {
Expand Down Expand Up @@ -202,7 +214,10 @@ const Profile = () => {
}
}
};

const fetchUserListing = async () => {
setListingIds(null);

if (id === undefined) {
const { data, error } = await auth.client.GET("/listings/me", {
params: {
Expand All @@ -216,6 +231,7 @@ const Profile = () => {
addErrorAlert(error);
} else {
setListingIds(data.listing_ids);
setMoreListings(data.has_next);
}
} else {
try {
Expand All @@ -232,6 +248,7 @@ const Profile = () => {
addErrorAlert(error);
} else {
setListingIds(data.listing_ids);
setMoreListings(data.has_next);
}
} catch (err) {
console.error("Failed to fetch User Listings", err);
Expand All @@ -243,7 +260,14 @@ const Profile = () => {
fetchUser();
fetchUserListing();
}
}, [id, auth.currentUser, auth.isLoading, auth.client, addErrorAlert]);
}, [
id,
auth.currentUser,
auth.isLoading,
auth.client,
addErrorAlert,
pageNumber,
]);

const handleUpdateProfile = async (updatedUser: Partial<UserResponse>) => {
try {
Expand All @@ -261,6 +285,12 @@ const Profile = () => {
}
};

const navigateToPage = (pageNumber: number) => {
const profileURL = id ? `/profile/${id}` : "/profile";
const paginationURL = `${profileURL}?page=${pageNumber}`;
navigate(paginationURL);
};

if (auth.isLoading || isLoading) {
return (
<div className="flex justify-center items-center pt-8">
Expand All @@ -276,11 +306,33 @@ const Profile = () => {
onUpdateProfile={handleUpdateProfile}
canEdit={canEdit}
/>
{listingIds && (
<div className="mt-4">
<ListingGrid listingIds={listingIds} />
</div>
)}
<div>
{hasButton && (
<div className="flex justify-center mt-4">
{prevButton && (
<button
className="bg-gray-300 dark:bg-gray-700 hover:bg-gray-400 dark:hover:bg-gray-600 text-gray-800 dark:text-gray-300 font-bold py-2 px-4 rounded-l mr-auto"
onClick={() => navigateToPage(pageNumber - 1)}
>
Previous
</button>
)}
{nextButton && (
<button
className="bg-gray-300 dark:bg-gray-700 hover:bg-gray-400 dark:hover:bg-gray-600 text-gray-800 dark:text-gray-300 font-bold py-2 px-4 rounded-r ml-auto"
onClick={() => navigateToPage(pageNumber + 1)}
>
Next
</button>
)}
</div>
)}
{listingIds && (
<div className="mt-4">
<ListingGrid listingIds={listingIds} />
</div>
)}
</div>
</>
) : (
<div className="flex justify-center items-center pt-8">
Expand Down

0 comments on commit 4d2e779

Please sign in to comment.