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 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { useEffect } from 'react'
import { useEffect, useState } from 'react'
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
import { EnvironmentSVG } from '@public/svg/dashboard'
import {
Expand All @@ -25,23 +25,48 @@ function EnvironmentPage(): React.JSX.Element {
const [environments, setEnvironments] = useAtom(environmentsOfProjectAtom)
const selectedProject = useAtomValue(selectedProjectAtom)
const selectedEnvironment = useAtomValue(selectedEnvironmentAtom)
const [page, setPage] = useState(0)
const [hasMore, setHasMore] = useState(true)
const [isLoading, setIsLoading] = useState(false)

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

useEffect(() => {
selectedProject &&
getAllEnvironmentsOfProject().then(({ data, success }) => {
if(!selectedProject) return
Copy link
Member

Choose a reason for hiding this comment

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

You can remove this check since its already present in handleEnvironmentsFetch

handleEnvironmentsFetch();
}, [selectedProject, getAllEnvironmentsOfProject, setEnvironments])

const handleEnvironmentsFetch = (newPage = 0) => {
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 fetchEnvironments

Copy link
Member

Choose a reason for hiding this comment

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

remove newPage. it should be using the page state var

if (!selectedProject) return
setIsLoading(true)
getAllEnvironmentsOfProject()
.then(({ data, success }) => {
if (success && data) {
setEnvironments(data.items)
const newData = newPage === 0 ? data.items : [...environments, ...data.items]
if(newPage == 0 && page !== 0) setPage(0);
Copy link
Member

Choose a reason for hiding this comment

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

this line doesnt make sense. initially you should set page to -1 or null. before calling getAllEnvironmentsOfProject, you should always increment the value of page using setPage(prev => prev+1)

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.

you can check for data.metadata.next. if its null, you can set hasMore to false

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

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 && !isLoading) {
const finalPage = page + 1;
setPage(finalPage)
handleEnvironmentsFetch(finalPage)
}
}

return (
<div
Expand Down Expand Up @@ -69,23 +94,33 @@ function EnvironmentPage(): React.JSX.Element {
</Button>
</div>
) : (
// Showing this when environments are present
<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.map((environment) => (
<EnvironmentCard environment={environment} key={environment.id} />
))}
//Showing this when environments are present
<div className="flex w-full flex-col">
<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.map((environment) => (
<EnvironmentCard environment={environment} key={environment.id} />
))}

{/* Delete environment alert dialog */}
{isDeleteEnvironmentOpen && selectedEnvironment ? (
<ConfirmDeleteEnvironment />
) : null}

{/* Delete environment alert dialog */}
{isDeleteEnvironmentOpen && selectedEnvironment ? (
<ConfirmDeleteEnvironment />
) : null}
{/* Edit environment dialog */}
{isEditEnvironmentOpen && selectedEnvironment ? (
<EditEnvironmentDialogue />
) : null}

{/* Edit environment dialog */}
{isEditEnvironmentOpen && selectedEnvironment ? (
<EditEnvironmentDialogue />
) : null}
{hasMore ? (
<div className="col-span-full flex justify-center">
<Button disabled={isLoading} onClick={handlePageShift}>
{isLoading ? 'Loading...' : 'Load More'}
</Button>
</div>
) : null}
</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