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

Conversation

chetan102
Copy link

@chetan102 chetan102 commented Feb 22, 2025

User description

Description

Now we dont get the all environments at one , now we can get it in the batch of 10 if user wants to see the next 10 environments they have to click on Load More Button

Fixes #707

Screenshots of relevant screens

Screenshot 2025-02-22 at 5 08 14 PM

Developer's checklist

  • My PR follows the style guidelines of this project
  • I have performed a self-check on my work

If changes are made in the code:

  • I have followed the coding guidelines
  • My changes in code generate no new warnings
  • My changes are breaking another fix/feature of the project
  • I have added test cases to show that my feature works
  • I have added relevant screenshots in my PR
  • There are no UI/UX issues

Documentation Update

  • This PR requires an update to the documentation at docs.keyshade.xyz
  • I have made the necessary updates to the documentation, or no documentation changes are required.

PR Type

Enhancement


Description

  • Added dynamic pagination to the environments page.

  • Introduced a "Load More" button for additional environments.

  • Limited initial environment display to 10 items.

  • Updated UI layout to support dynamic loading.


Changes walkthrough 📝

Relevant files
Enhancement
page.tsx
Added dynamic pagination and "Load More" button                   

apps/platform/src/app/(main)/(project)/[workspace]/[project]/@environment/page.tsx

  • Added state management for visible environment count.
  • Limited initial environment display to 10 items.
  • Added "Load More" button to load additional environments.
  • Updated UI structure to support dynamic pagination.
  • +26/-16 

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    707 - PR Code Verified

    Compliant requirements:

    • Show first 10 environments by default
    • Add "Load More" button at end of page
    • Load More button shows next 10 environments on click
    • Add dynamic pagination on environments page

    Requires further human verification:

    • Verify UI/UX matches Figma design
    • Test pagination behavior with 20+ environments
    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Performance

    The slice operation on environments array is performed on every render. Consider memoizing the sliced array using useMemo to optimize performance.

    {environments.slice(0, visibleCount).map((environment) => (
    Code Style

    The onClick handler for Load More button uses an inline arrow function. Consider extracting this to a named function for better readability and reusability.

    <Button onClick={()=> setVisibleCount((prev) => prev + INITIAL_DISPLAY_COUNT)}>Load More</Button>

    Copy link
    Contributor

    codiumai-pr-agent-free bot commented Feb 22, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Prevent concurrent loading operations
    Suggestion Impact:The commit implemented loading state handling for the Load More button, including disabling it during loading and showing loading text, though with a different implementation approach using pagination

    code diff:

    +              <Button disabled={loading} onClick={handlePageShift}>
    +                {loading ? 'Loading...' : 'Load More'}
    +              </Button>

    Add loading state to prevent multiple "Load More" clicks while data is being
    fetched, which could cause race conditions or duplicate loads.

    apps/platform/src/app/(main)/(project)/[workspace]/[project]/@environment/page.tsx [96]

    -<Button onClick={()=> setVisibleCount((prev) => prev + INITIAL_DISPLAY_COUNT)}>Load More</Button>
    +<Button 
    +  onClick={()=> setVisibleCount((prev) => prev + INITIAL_DISPLAY_COUNT)}
    +  disabled={isLoading}
    +>
    +  {isLoading ? 'Loading...' : 'Load More'}
    +</Button>

    [Suggestion has been applied]

    Suggestion importance[1-10]: 8

    __

    Why: Adding loading state handling is crucial for preventing race conditions and improving UX by disabling the button during data fetches. This helps avoid potential bugs from multiple concurrent requests.

    Medium
    Handle potential undefined values
    Suggestion Impact:Added optional chaining operator to safely handle potential undefined values when accessing environments array

    code diff:

    -            {environments.slice(0, visibleCount).map((environment) => (
    +            {environments?.slice(0, visibleCount)?.map((environment) => (

    Add error handling for cases where the environment data slice operation fails or
    returns undefined results.

    apps/platform/src/app/(main)/(project)/[workspace]/[project]/@environment/page.tsx [80-82]

    -{environments.slice(0, visibleCount).map((environment) => (
    -  <EnvironmentCard environment={environment} key={environment.id} />
    +{environments?.slice(0, visibleCount)?.map((environment) => (
    +  environment && <EnvironmentCard environment={environment} key={environment.id} />
     ))}

    [Suggestion has been applied]

    Suggestion importance[1-10]: 6

    __

    Why: Adding null checks improves code robustness by safely handling potential undefined values, though the current context suggests environments is already defined as an array through the useAtom hook.

    Low
    • Update

    @chetan102
    Copy link
    Author

    chetan102 commented Feb 22, 2025

    @rajdip-b, Oh i see is it like increasing the perPage when the User clicks the Load More. like preview is 10 then again +10 = 20 when clicking on the Load More , and while api call it still holds the previous environments value before showing the new 20 environment is it like this is it like this.

    @rajdip-b
    Copy link
    Member

    yeap

    @chetan102
    Copy link
    Author

    @rajdip-b Now i done changes according to it u can check it.

    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

    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

    })
    }, [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

    Copy link

    @chetan102, please resolve all open reviews!

    @chetan102 chetan102 requested a review from rajdip-b February 24, 2025 18:23
    @@ -39,24 +39,32 @@ function EnvironmentPage(): React.JSX.Element {
    )

    useEffect(() => {
    if (!selectedProject) return
    setLoading(true)
    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

    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.

    remove newPage. it should be using the page state var

    setEnvironments(newData);
    if (newData.length >= data.metadata.totalCount) setHasMore(false);
    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)

    const newData = newPage === 0 ? data.items : [...environments, ...data.items]
    if(newPage == 0 && page !== 0) setPage(0);
    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

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    PLATFORM: Add pagination on environments screen
    2 participants