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

Conditionaly renders pagination component if number of entries is more than results per page #9004

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

mnshrm
Copy link
Contributor

@mnshrm mnshrm commented Nov 5, 2024

Added logic which makes sure, if number of entries for staff capacity is less than or equal to results per page of that section, only then pagination component is shown, otherwise not

Screenshots

  • Pagination component is not visible since number of entries (12) is less than equal to results per page (15)

image

  • Pagination component is visible since number of entries (12) is more than results per page (5)

image

@ohcnetwork/care-fe-code-reviewers

Merge Checklist

  • Add specs that demonstrate bug / test a new feature.
  • Update product documentation.
  • Ensure that UI text is kept in I18n files.
  • Prep screenshot or demo video for changelog entry, and attach it to issue.
  • Request for Peer Reviews
  • Completion of QA

Summary by CodeRabbit

  • Bug Fixes
    • Updated pagination logic to prevent rendering when the total item count is less than the number of rows per page, enhancing user experience by avoiding empty pagination controls.

@mnshrm mnshrm requested a review from a team as a code owner November 5, 2024 15:43
Copy link

netlify bot commented Nov 5, 2024

Deploy Preview for care-ohc failed.

Name Link
🔨 Latest commit fdb1995
🔍 Latest deploy log https://app.netlify.com/sites/care-ohc/deploys/672b8423aeb9090008d7b9ef

Copy link

coderabbitai bot commented Nov 5, 2024

Walkthrough

The changes made in the Pagination component involve modifying the rendering logic based on the totalCount of items. The condition for rendering has been updated to return null if either totalCount is zero or less than the number of rows per page. This adjustment ensures that the pagination component will not render when there are insufficient items to fill a page, enhancing the component's behavior in specific scenarios.

Changes

File Change Summary
src/components/Common/Pagination.tsx Updated rendering condition from if (!totalCount) to `if (!totalCount

Assessment against linked issues

Objective Addressed Explanation
Hide the pagination component if there is only one page. (#8992)

🐇 In the garden where numbers play,
Pagination hops, bright as day.
With rows to fill, it takes a stand,
Hiding away when few items land.
A joyful leap, a clever twist,
In the world of data, it can't be missed! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Member

@rithviknishad rithviknishad left a comment

Choose a reason for hiding this comment

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

Instead let's update the logic in the Pagination component instead that way this behaviour is common for all usages.

const totalCount = data.totalCount;
if (!totalCount) {
return null;
}

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
src/components/Facility/FacilityStaffList.tsx (2)

122-129: LGTM! Consider extracting the condition for better readability.

The conditional rendering logic correctly implements the requirement to show pagination only when needed. The implementation is type-safe with proper null handling.

Consider extracting the condition to a semantic variable name:

+     const shouldShowPagination = (doctorsList?.count ?? 0) > resultsPerPage;
-     {(doctorsList?.count ?? 0) > resultsPerPage && (
+     {shouldShowPagination && (
        <Pagination
          cPage={qParams.page}
          defaultPerPage={resultsPerPage}
          data={{ totalCount: doctorsList?.count ?? 0 }}
          onChange={(page) => updatePage(page)}
        />
      )}

122-129: Consider memoizing the Pagination component to optimize performance.

Since the Pagination component's props only depend on a few values (page, resultsPerPage, count), you could optimize re-renders by memoizing it.

Here's how you could implement this:

const MemoizedPagination = useMemo(
  () => (
    <Pagination
      cPage={qParams.page}
      defaultPerPage={resultsPerPage}
      data={{ totalCount: doctorsList?.count ?? 0 }}
      onChange={(page) => updatePage(page)}
    />
  ),
  [qParams.page, resultsPerPage, doctorsList?.count, updatePage]
);

// Then in your JSX:
{(doctorsList?.count ?? 0) > resultsPerPage && MemoizedPagination}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6b57124 and 69f43da.

📒 Files selected for processing (1)
  • src/components/Facility/FacilityStaffList.tsx (1 hunks)

@@ -68,7 +68,7 @@ const Pagination = ({
};

const totalCount = data.totalCount;
if (!totalCount) {
if (!totalCount || totalCount < defaultPerPage) {
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be the right solution? correct me if I am wrong

Suggested change
if (!totalCount || totalCount < defaultPerPage) {
if (!totalCount || totalCount < rowsPerPage) {

Copy link
Contributor Author

@mnshrm mnshrm Nov 5, 2024

Choose a reason for hiding this comment

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

Yes, apologies updated the logic

@nihal467
Copy link
Member

nihal467 commented Nov 6, 2024

image

  • When there are exactly 15 cards—the maximum limit for a single page—the pagination component becomes visible, which shouldn't be the case. Ideally, the pagination component should only appear when there are more than 15 cards, indicating the presence of a second page.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
src/components/Common/Pagination.tsx (2)

Line range hint 6-12: Consider simplifying the component props interface.

The data prop currently only contains totalCount, which could be directly passed as a prop for better clarity and type safety.

Consider this improvement:

 interface PaginationProps {
-  data: { totalCount: number };
+  totalCount: number;
   onChange: (page: number, rowsPerPage: number) => void;
   defaultPerPage: number;
   cPage: number;
   className?: string;
 }

Then update the component usage:

-const totalCount = data.totalCount;
+const totalCount = props.totalCount;

Line range hint 19-20: Optimize state initialization to prevent unnecessary renders.

The rowsPerPage state is initialized to 3 but immediately overridden by defaultPerPage in the useAbortableEffect. Consider initializing it with the defaultPerPage value directly.

-  const [rowsPerPage, setRowsPerPage] = useState(3);
+  const [rowsPerPage, setRowsPerPage] = useState(defaultPerPage);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 69f43da and abfdb83.

📒 Files selected for processing (1)
  • src/components/Common/Pagination.tsx (1 hunks)
🔇 Additional comments (1)
src/components/Common/Pagination.tsx (1)

71-73: LGTM! The pagination visibility logic is now correct.

The updated condition !totalCount || totalCount < rowsPerPage properly handles all cases:

  • Hides pagination when there are no items
  • Hides pagination when items fit in a single page
  • Correctly handles the edge case when total items exactly equal rows per page

Let's verify the component behavior with different scenarios:

✅ Verification successful

Let me analyze the usage patterns to verify the pagination behavior. I'll search for any test files and check the component's implementation details.


Based on the analysis of the codebase, I can now provide a final response regarding the pagination visibility change:

The pagination visibility logic is correctly implemented and consistent with the codebase's requirements.

The verification confirms:

  • The default page sizes across the codebase range from 6 to 36 items (PER_PAGE_LIMIT: 6, RESULTS_PER_PAGE_LIMIT: 14, PAGINATION_LIMIT: 36)
  • Multiple components consistently check if totalCount > PAGINATION_LIMIT before rendering pagination
  • The condition !totalCount || totalCount < rowsPerPage properly handles all cases:
    • Zero items (!totalCount)
    • Items fitting in a single page (totalCount < rowsPerPage)
    • Edge case when total equals page size (correctly hidden)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for test cases or usage patterns of the Pagination component
# to ensure the new condition works as expected in all scenarios.

# Test: Look for test files related to Pagination
fd -e test.tsx -e test.ts -e spec.tsx -e spec.ts Pagination

# Test: Search for Pagination component usage to verify prop patterns
rg -A 3 "<Pagination"

Length of output: 9560


Script:

#!/bin/bash
# Look for default page sizes and pagination limits across the codebase
rg "PAGINATION_LIMIT|PER_PAGE_LIMIT|RESULTS_PER_PAGE|rowsPerPage" -A 2

# Search for Pagination component's implementation details
ast-grep --pattern 'const Pagination = $_' -l typescript

Length of output: 17392

…es/8992/staff-capacity-section-pagination-issue
@github-actions github-actions bot added the Deploy-Failed Deplyment is not showing preview label Nov 6, 2024
@mnshrm
Copy link
Contributor Author

mnshrm commented Nov 6, 2024

Hi @nihal467
I did recheck the fix for 15 entries exactly, Pagination component is not visible in this case as expected, can you please recheck again, I used this facility test-august-secondweek
attaching screenshot for reference
image

…es/8992/staff-capacity-section-pagination-issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changes required Deploy-Failed Deplyment is not showing preview test failed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hide the pagination component in the facility staff capacity section if there is only one page.
3 participants