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

Adds support for query.paginated #10529

Merged
merged 6 commits into from
Feb 11, 2025

Conversation

rithviknishad
Copy link
Member

@rithviknishad rithviknishad commented Feb 10, 2025

Proposed Changes

  • Adds support for query.paginated

@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

  • New Features

    • Introduced a new method for fetching and aggregating results from paginated API data, allowing for customizable page size and maximum pages.
  • Refactor

    • Simplified the pagination response format by removing navigation details, resulting in a cleaner and more intuitive data structure.

@rithviknishad rithviknishad requested a review from a team as a code owner February 10, 2025 06:35
Copy link
Contributor

coderabbitai bot commented Feb 10, 2025

Walkthrough

This pull request introduces a new function called paginatedQuery to the query module, enabling the fetching of paginated API data in a loop and aggregating the results into a single array. It includes an optional maxPages parameter to limit the number of pages fetched. Additionally, the PaginatedResponse type is updated by removing the next and previous properties, simplifying its structure. The changes affect both functionality and type declarations within the module.

Changes

File Summary
src/…/query.ts Added the paginatedQuery function for fetching and aggregating paginated data, attached as query.paginated, and imported the PaginatedResponse type.
src/…/types.ts Removed the next and previous properties from the PaginatedResponse interface, simplifying its structure.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant QueryModule as query.paginated
    participant API

    User->>QueryModule: Call query.paginated(route, options)
    loop While more pages exist AND maxPages not reached
        QueryModule->>API: Fetch current page data
        API-->>QueryModule: Return paginated response
        QueryModule->>QueryModule: Append items to results
    end
    QueryModule->>User: Return aggregated results
Loading

Poem

In the garden of code, I hop with delight,
Gathering pages by day and by night.
Each query a carrot, each fetch a treat,
My code-burrow grows sturdy and neat.
Hoppin' along, I celebrate updates in flight!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ebfcd79 and 8c9875d.

📒 Files selected for processing (1)
  • src/Utils/request/query.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: cypress-run (1)
  • GitHub Check: OSSAR-Scan
  • GitHub Check: Cloudflare Pages: care-fe
🔇 Additional comments (4)
src/Utils/request/query.ts (4)

3-11: LGTM!

The imports are well-organized and include all necessary dependencies for the pagination functionality.


124-143: LGTM!

The documentation is clear, well-structured, and includes helpful examples.


144-187: Add error handling and improve query parameters handling.

The implementation needs several improvements:

  1. Add error handling for failed requests within the loop
  2. Fix query parameters order to prevent overriding custom parameters
  3. Add progress tracking for UI feedback
  4. Consider memory implications for large datasets

Here's a suggested implementation:

 const paginatedQuery = <
   Route extends ApiRoute<PaginatedResponse<unknown>, unknown>,
 >(
   route: Route,
   options?: ApiCallOptions<Route> & { pageSize?: number; maxPages?: number },
 ) => {
   return async ({ signal }: { signal: AbortSignal }) => {
     const items: Route["TRes"]["results"] = [];
     let hasNextPage = true;
     let page = 0;
     let count = 0;
 
     const pageSize = options?.pageSize ?? RESULTS_PER_PAGE_LIMIT;
 
     while (hasNextPage) {
+      try {
         const res = await query(route, {
           ...options,
           queryParams: {
+            ...options?.queryParams,
             limit: pageSize,
             offset: page * pageSize,
-            ...options?.queryParams,
           },
         })({ signal });
 
         count = res.count;
         items.push(...res.results);
 
         if (options?.maxPages && page >= options.maxPages - 1) {
           hasNextPage = false;
         }
 
         if (items.length >= res.count) {
           hasNextPage = false;
         }
 
         page++;
+      } catch (error) {
+        // Rethrow abort errors
+        if (error instanceof DOMException && error.name === 'AbortError') {
+          throw error;
+        }
+        // Handle other errors
+        throw new Error(`Failed to fetch page ${page + 1}: ${error.message}`);
+      }
     }
 
     return {
       count,
       results: items,
+      // Add metadata for UI feedback
+      pagination: {
+        totalPages: Math.ceil(count / pageSize),
+        currentPage: page,
+        pageSize,
+      },
     };
   };
 };

188-188: LGTM!

The export is consistent with the module's pattern of attaching utilities to the query object.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

netlify bot commented Feb 10, 2025

Deploy Preview for care-ohc ready!

Name Link
🔨 Latest commit 8c9875d
🔍 Latest deploy log https://app.netlify.com/sites/care-ohc/deploys/67a9e6e5343ba20008979efd
😎 Deploy Preview https://deploy-preview-10529--care-ohc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@rithviknishad rithviknishad self-assigned this Feb 10, 2025
Copy link

cloudflare-workers-and-pages bot commented Feb 10, 2025

Deploying care-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8c9875d
Status: ✅  Deploy successful!
Preview URL: https://84c8a42c.care-fe.pages.dev
Branch Preview URL: https://rithviknishad-feat-use-pagin.care-fe.pages.dev

View logs

Copy link
Contributor

@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: 1

🧹 Nitpick comments (1)
src/Utils/request/use-paginated-query.ts (1)

27-27: Consider using type guard instead of type assertion.

Replace the type assertion with a type guard to ensure runtime type safety:

-      return response as PaginatedResponse<Route["TRes"]>;
+      if (isPaginatedResponse(response)) {
+        return response;
+      }
+      throw new Error("Expected paginated response");

Add this type guard function:

function isPaginatedResponse<T>(response: unknown): response is PaginatedResponse<T> {
  return (
    typeof response === "object" &&
    response !== null &&
    "count" in response &&
    "results" in response &&
    Array.isArray((response as PaginatedResponse<T>).results)
  );
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between be85955 and 020261a.

📒 Files selected for processing (1)
  • src/Utils/request/use-paginated-query.ts (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: Lint Code Base
src/Utils/request/use-paginated-query.ts

[warning] 1-1: File is unimported. Consider importing or removing it.

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: OSSAR-Scan
  • GitHub Check: cypress-run (1)
🔇 Additional comments (4)
src/Utils/request/use-paginated-query.ts (4)

13-15: LGTM! Well-typed generic hook signature.

The function signature is well-designed with proper generic type constraints and follows React hook naming conventions.


42-46: LGTM! Well-structured return value.

The return object provides a good balance of convenience (flattened data) and flexibility (paginated results) while maintaining type safety.


1-12: Address the pipeline warning about unimported file.

The linter indicates that this file is not imported anywhere in the codebase. Ensure that this hook is properly imported and utilized where needed.

Let's verify the file usage:

❌ Verification inconclusive

🏁 Script executed:

#!/bin/bash
# Search for any imports of use-paginated-query
rg -l "use-paginated-query"

Length of output: 27


Pipeline Warning: "use-paginated-query" Not Imported

Based on the search results—which produced no output—the file "src/Utils/request/use-paginated-query.ts" does not appear to be imported anywhere in the codebase. This aligns with the linter's warning. Please manually verify if the hook is meant to be used (perhaps via dynamic or indirect imports) or if it should be added to where necessary.

🧰 Tools
🪛 GitHub Actions: Lint Code Base

[warning] 1-1: File is unimported. Consider importing or removing it.


23-23: Verify the RESULTS_PER_PAGE_LIMIT constant value.

Ensure that the constant value aligns with the API's pagination limits.

Let's check the constant definition:

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Search for RESULTS_PER_PAGE_LIMIT definition
rg "RESULTS_PER_PAGE_LIMIT.*=.*" -A 2

Length of output: 161


RESULTS_PER_PAGE_LIMIT is set to 14

  • The constant is defined in src/common/constants.tsx as follows:
    export const RESULTS_PER_PAGE_LIMIT = 14;
  • Provided that the API's pagination limit is expected to be 14 results per page, this constant aligns with the API constraints.

Comment on lines 36 to 40
useEffect(() => {
if (hasNextPage && fetchNextPage) {
fetchNextPage();
}
}, [hasNextPage, fetchNextPage, data]);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding safeguards and optimizing dependencies.

The current implementation has two potential issues:

  1. No safeguards against infinite loops if the API response is malformed
  2. Unnecessary data dependency might cause extra re-renders

Consider this improved implementation:

  useEffect(() => {
+   const MAX_PAGES = 1000; // Safeguard against infinite loops
+   const currentPages = data?.pages.length ?? 0;
-   if (hasNextPage && fetchNextPage) {
+   if (hasNextPage && fetchNextPage && currentPages < MAX_PAGES) {
      fetchNextPage();
    }
-  }, [hasNextPage, fetchNextPage, data]);
+  }, [hasNextPage, fetchNextPage, data?.pages.length]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
if (hasNextPage && fetchNextPage) {
fetchNextPage();
}
}, [hasNextPage, fetchNextPage, data]);
useEffect(() => {
const MAX_PAGES = 1000; // Safeguard against infinite loops
const currentPages = data?.pages.length ?? 0;
if (hasNextPage && fetchNextPage && currentPages < MAX_PAGES) {
fetchNextPage();
}
}, [hasNextPage, fetchNextPage, data?.pages.length]);

Copy link

cypress bot commented Feb 10, 2025

CARE    Run #4727

Run Properties:  status check failed Failed #4727  •  git commit 8c9875db48: Adds support for `usePaginatedQuery`
Project CARE
Branch Review rithviknishad/feat/use-paginated-query
Run status status check failed Failed #4727
Run duration 06m 24s
Commit git commit 8c9875db48: Adds support for `usePaginatedQuery`
Committer Rithvik Nishad
View all properties for this run ↗︎

Test results
Tests that failed  Failures 2
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 9
View all changes introduced in this branch ↗︎

Tests for review

Failed  facility_spec/facility_creation.cy.ts • 1 failed test

View Output

Test Artifacts
Facility Management > Create a new facility using the admin role and verify validation errors Test Replay Screenshots
Failed  users_spec/user_creation.cy.ts • 1 failed test

View Output

Test Artifacts
User Creation > should create a new user successfully Test Replay Screenshots

Copy link
Contributor

@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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eb3ee74 and 4940cb0.

📒 Files selected for processing (2)
  • src/Utils/request/query.ts (2 hunks)
  • src/Utils/request/types.ts (0 hunks)
💤 Files with no reviewable changes (1)
  • src/Utils/request/types.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: cypress-run (1)
🔇 Additional comments (4)
src/Utils/request/query.ts (4)

4-9: LGTM!

The imports are well-organized and include all necessary types for the pagination functionality.


122-141: Documentation looks good!

The documentation is clear and includes a helpful example of usage with useQuery.


174-174: LGTM!

The extension follows the same pattern as query.debounced, maintaining consistency in the codebase.


1-175: Verify documentation and type usage.

Please ensure:

  1. The documentation at https://docs.ohc.network is updated to reflect the new usePaginatedQuery functionality.
  2. All components using PaginatedResponse type are updated to handle the removed next and previous properties.

Run the following script to find components that might need updates:

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Find components using PaginatedResponse type
rg -l "PaginatedResponse" src/

Length of output: 1143


I'll verify that the PaginatedResponse type in the code no longer includes the removed next/previous properties. Additionally, I'll check that any components using PaginatedResponse across the codebase have been updated accordingly.

Let's begin by inspecting the PaginatedResponse definition in "src/Utils/request/types.ts" for any remaining usage of "next" or "previous".

I'll run a script to search for "next:" or "previous:" in that file.


🏁 Script executed:

#!/bin/bash
# Search for any occurrences of "next:" or "previous:" in the PaginatedResponse type definition.
rg -n "interface PaginatedResponse" -n src/Utils/request/types.ts
rg -n "next\s*:" src/Utils/request/types.ts
rg -n "previous\s*:" src/Utils/request/types.ts

Length of output: 210


Documentation & PaginatedResponse Usage Verification

  • Confirmed the PaginatedResponse interface (in src/Utils/request/types.ts) no longer includes next or previous properties.
  • Verified that components referencing PaginatedResponse (as identified by the rg search in several files) are now expected to handle only count and results.
  • Please ensure the external documentation at https://docs.ohc.network is updated accordingly to reflect the new usePaginatedQuery functionality.

Comment on lines 142 to 173
const paginatedQuery = <
Route extends ApiRoute<PaginatedResponse<unknown>, unknown>,
>(
route: Route,
options?: ApiCallOptions<Route> & { maxPages?: number },
) => {
return async ({ signal }: { signal: AbortSignal }) => {
const items: Route["TRes"]["results"] = [];
let hasNextPage = true;
let page = 0;

while (hasNextPage) {
const res = await query(route, { ...options })({ signal });
items.push(...res.results);

if (options?.maxPages && page >= options.maxPages) {
hasNextPage = false;
}

if (items.length >= res.count) {
hasNextPage = false;
}

page++;
}

return {
count: items.length,
results: items,
};
};
};
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding error handling and progress tracking.

The pagination implementation could be improved in several areas:

  1. Add error handling for failed requests within the loop
  2. Manage offset in query parameters for each page
  3. Consider memory implications for large datasets
  4. Add progress tracking for UI feedback

Here's a suggested implementation:

 const paginatedQuery = <
   Route extends ApiRoute<PaginatedResponse<unknown>, unknown>,
 >(
   route: Route,
   options?: ApiCallOptions<Route> & { maxPages?: number },
 ) => {
   return async ({ signal }: { signal: AbortSignal }) => {
     const items: Route["TRes"]["results"] = [];
     let hasNextPage = true;
     let page = 0;
+    let offset = options?.queryParams?.offset ?? 0;
+    const limit = options?.queryParams?.limit ?? 10;
 
     while (hasNextPage) {
-      const res = await query(route, { ...options })({ signal });
+      try {
+        const res = await query(route, {
+          ...options,
+          queryParams: {
+            ...options?.queryParams,
+            offset: offset + (page * limit),
+          },
+        })({ signal });
+
+        items.push(...res.results);
 
-      items.push(...res.results);
+        if (options?.maxPages && page >= options.maxPages - 1) {
+          hasNextPage = false;
+        }
 
-      if (options?.maxPages && page >= options.maxPages) {
-        hasNextPage = false;
-      }
+        if (items.length >= res.count) {
+          hasNextPage = false;
+        }
 
-      if (items.length >= res.count) {
-        hasNextPage = false;
+        page++;
+      } catch (error) {
+        // Rethrow abort errors
+        if (error instanceof DOMException && error.name === 'AbortError') {
+          throw error;
+        }
+        // Handle other errors
+        throw new Error(`Failed to fetch page ${page + 1}: ${error.message}`);
       }
-
-      page++;
     }
 
     return {
       count: items.length,
       results: items,
+      // Add metadata for UI feedback
+      pagination: {
+        totalPages: page,
+        currentPage: page,
+      },
     };
   };
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const paginatedQuery = <
Route extends ApiRoute<PaginatedResponse<unknown>, unknown>,
>(
route: Route,
options?: ApiCallOptions<Route> & { maxPages?: number },
) => {
return async ({ signal }: { signal: AbortSignal }) => {
const items: Route["TRes"]["results"] = [];
let hasNextPage = true;
let page = 0;
while (hasNextPage) {
const res = await query(route, { ...options })({ signal });
items.push(...res.results);
if (options?.maxPages && page >= options.maxPages) {
hasNextPage = false;
}
if (items.length >= res.count) {
hasNextPage = false;
}
page++;
}
return {
count: items.length,
results: items,
};
};
};
const paginatedQuery = <
Route extends ApiRoute<PaginatedResponse<unknown>, unknown>,
>(
route: Route,
options?: ApiCallOptions<Route> & { maxPages?: number },
) => {
return async ({ signal }: { signal: AbortSignal }) => {
const items: Route["TRes"]["results"] = [];
let hasNextPage = true;
let page = 0;
let offset = options?.queryParams?.offset ?? 0;
const limit = options?.queryParams?.limit ?? 10;
while (hasNextPage) {
try {
const res = await query(route, {
...options,
queryParams: {
...options?.queryParams,
offset: offset + (page * limit),
},
})({ signal });
items.push(...res.results);
if (options?.maxPages && page >= options.maxPages - 1) {
hasNextPage = false;
}
if (items.length >= res.count) {
hasNextPage = false;
}
page++;
} catch (error) {
// Rethrow abort errors
if (error instanceof DOMException && error.name === 'AbortError') {
throw error;
}
// Handle other errors
throw new Error(`Failed to fetch page ${page + 1}: ${error.message}`);
}
}
return {
count: items.length,
results: items,
// Add metadata for UI feedback
pagination: {
totalPages: page,
currentPage: page,
},
};
};
};

@rithviknishad rithviknishad changed the title Adds support for usePaginatedQuery Adds support for query.paginated Feb 11, 2025
@amjithtitus09 amjithtitus09 merged commit 8c94318 into develop Feb 11, 2025
20 of 23 checks passed
@amjithtitus09 amjithtitus09 deleted the rithviknishad/feat/use-paginated-query branch February 11, 2025 12:56
Copy link

@rithviknishad @amjithtitus09 Your efforts have helped advance digital healthcare and TeleICU systems. 🚀 Thank you for taking the time out to make CARE better. We hope you continue to innovate and contribute; your impact is immense! 🙌

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

Successfully merging this pull request may close these issues.

2 participants