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 Environments Screen #780

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,47 @@ import ControllerInstance from '@/lib/controller-instance'
import { Button } from '@/components/ui/button'
import { useHttp } from '@/hooks/use-http'

const INITIAL_DISPLAY_COUNT = 10

function EnvironmentPage(): React.JSX.Element {
const setIsCreateEnvironmentOpen = useSetAtom(createEnvironmentOpenAtom)
const isDeleteEnvironmentOpen = useAtomValue(deleteEnvironmentOpenAtom)
const isEditEnvironmentOpen = useAtomValue(editEnvironmentOpenAtom)
const [environments, setEnvironments] = useAtom(environmentsOfProjectAtom)
const selectedProject = useAtomValue(selectedProjectAtom)
const selectedEnvironment = useAtomValue(selectedEnvironmentAtom)
const [visibleCount, setVisibleCount] = useState<number>(INITIAL_DISPLAY_COUNT)
const [page, setPage] = useState(0)
const [hasMore, setHasMore] = useState(true)
const [loading, setLoading] = useState(false)
Copy link
Member

Choose a reason for hiding this comment

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

rename this to isLoading to match out conventions


const getAllEnvironmentsOfProject = useHttp(() =>
ControllerInstance.getInstance().environmentController.getAllEnvironmentsOfProject(
{
projectSlug: selectedProject!.slug
projectSlug: selectedProject!.slug,
page,
}
)
)

useEffect(() => {
selectedProject &&
getAllEnvironmentsOfProject().then(({ data, success }) => {
if (!selectedProject) return
setLoading(true)
getAllEnvironmentsOfProject()
.then(({ data, success }) => {
if (success && data) {
setEnvironments(data.items)
const newData = page === 0 ? data.items : [...environments, ...data.items];
setEnvironments(newData);
if (newData.length >= data.metadata.totalCount) setHasMore(false);
Copy link
Member

Choose a reason for hiding this comment

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

Instead of this complex calculation, you can check for thw metadata.next field in the response. It will be null if this is the last page.
Uploading image.png…

Copy link
Author

Choose a reason for hiding this comment

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

Screenshot 2025-02-24 at 9 47 17 PM

i didnt find any next key

}
})
}, [getAllEnvironmentsOfProject, selectedProject, setEnvironments])
.finally(() => {
setLoading(false)
})
}, [selectedProject, page, getAllEnvironmentsOfProject, setEnvironments])

const handlePageShift = () => {
Copy link
Member

Choose a reason for hiding this comment

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

I feel you should extract all of the logic in the use effect hook to a separate function.

  • call that function once in use effect
  • call that function whenever load more is clicked

if (hasMore && !loading) {
setPage(prevPage => prevPage + 1)
}
}

return (
<div
Expand Down Expand Up @@ -77,7 +91,7 @@ function EnvironmentPage(): React.JSX.Element {
<div
className={`grid h-fit w-full grid-cols-1 gap-8 p-3 text-white md:grid-cols-2 xl:grid-cols-3 ${isDeleteEnvironmentOpen ? 'inert' : ''} `}
>
{environments?.slice(0, visibleCount)?.map((environment) => (
{environments.map((environment) => (
<EnvironmentCard environment={environment} key={environment.id} />
))}

Expand All @@ -90,12 +104,15 @@ function EnvironmentPage(): React.JSX.Element {
{isEditEnvironmentOpen && selectedEnvironment ? (
<EditEnvironmentDialogue />
) : null}

{hasMore ? <div className="col-span-full flex justify-center">
<Button disabled={loading} onClick={handlePageShift}>
{loading ? 'Loading...' : 'Load More'}
</Button>
</div> : null}

</div>
{visibleCount < environments.length && (
<div className="my-2 flex w-full justify-center py-2">
<Button onClick={()=> setVisibleCount((prev) => prev + INITIAL_DISPLAY_COUNT)}>Load More</Button>
</div>
)}

</div>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ function DetailedProjectPage({
})
}, [getProject, params.project, setSelectedProject])

useEffect(() => {
selectedProject &&
getAllEnvironmentsOfProject().then(({ data, success }) => {
if (success && data) {
setEnvironments(data.items)
}
})
}, [getAllEnvironmentsOfProject, selectedProject, setEnvironments])

return (
<main className="flex h-full flex-col gap-4">
<div className="flex h-[3.625rem] w-full justify-between p-3 ">
Expand Down