-
Notifications
You must be signed in to change notification settings - Fork 518
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
Refactor FileBlock, FIleUpload, UserAvatar, UserHome and migrated to TanStack's query #10045
base: develop
Are you sure you want to change the base?
Refactor FileBlock, FIleUpload, UserAvatar, UserHome and migrated to TanStack's query #10045
Conversation
…d, UserAvatar, and UserHome components
WalkthroughThis pull request focuses on refactoring data fetching across multiple components by replacing a custom query hook with the standard Changes
Sequence DiagramsequenceDiagram
participant Component
participant useQuery
participant query Utility
participant API
Component->>useQuery: Configure query
useQuery->>query Utility: Call with params
query Utility->>API: Make network request
API-->>query Utility: Return data
query Utility-->>useQuery: Process response
useQuery-->>Component: Provide data and state
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
✅ Deploy Preview for care-ohc ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this 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 (3)
src/components/Users/UserHome.tsx (1)
39-51
: Handle error state in data fetchingCurrently, the component only handles the loading state and the absence of
userData
but does not handle errors that might occur during the data fetching process. It's important to provide feedback to the user when an error occurs to improve the user experience.Consider adding error handling by checking for the
error
state returned byuseQuery
:const { data: userData, isLoading, error } = useQuery({ ... }); if (isLoading) { return <Loading />; } if (error) { return <div>Error fetching user details</div>; // Replace with a user-friendly error component }This ensures that users are informed if there's an issue retrieving the user details.
src/components/Files/FileUpload.tsx (1)
137-147
: Addenabled
option touseQuery
hooks to prevent unnecessary queriesThe
useQuery
hooks foractiveFilesQuery
andarchivedFilesQuery
do not have theenabled
option specified. IfassociatedId
is empty or invalid, these queries may run unnecessarily, leading to redundant API calls or potential errors.Include the
enabled
option to ensure the queries only execute whenassociatedId
is valid:useQuery({ queryKey: ["viewUpload", "active", type, associatedId, offset], queryFn: query(routes.viewUpload, { // ... }), + enabled: Boolean(associatedId), });
Apply the same change to the
archivedFilesQuery
:useQuery({ queryKey: ["viewUpload", "archived", type, associatedId, offset], queryFn: query(routes.viewUpload, { // ... }), + enabled: Boolean(associatedId), });
This prevents unnecessary queries when
associatedId
is not available.Also applies to: 150-163
src/components/Users/UserAvatar.tsx (1)
31-38
: Handle error state in data fetchingThe component currently only checks for the loading state and absence of
userData
, but it doesn't handle errors that might occur during the data fetching process usinguseQuery
. Including error handling improves the robustness of the component and provides better feedback to the user.Modify the code to handle errors by updating the destructured values and adding an error check:
const { data: userData, isLoading, error } = useQuery({ ... }); if (isLoading) { return <Loading />; } if (error) { return <div>Error loading user data</div>; // Replace with an appropriate error component }This ensures users are notified when there's an issue fetching the user data.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
src/components/Files/FileBlock.tsx
(4 hunks)src/components/Files/FileUpload.tsx
(4 hunks)src/components/Users/UserAvatar.tsx
(3 hunks)src/components/Users/UserHome.tsx
(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: cypress-run (1)
🔇 Additional comments (4)
src/components/Files/FileBlock.tsx (4)
1-1
: LGTM! Clean transition to TanStack Query.The import changes correctly reflect the migration from a custom query hook to TanStack Query's official implementation.
Also applies to: 16-16
38-43
: Excellent query key structure!The array-based query key structure with multiple elements (
path
,file.id
,fileManager.type
,associating_id
) provides excellent cache management and invalidation capabilities. This approach aligns with TanStack Query best practices.
94-94
: LGTM! Correct data access pattern.The update to
fileData?.read_signed_url
correctly reflects TanStack Query's data structure, maintaining null safety with optional chaining.
37-48
: Consider adding input validation for file.id.While the query implementation is solid, the fallback to an empty string for
file.id
might indicate missing validation. Consider validating the file ID before making the query.
const { data: dischargeSummaryQuery, isLoading: dischargeSummaryLoading } = | ||
useQuery({ | ||
queryKey: ["viewUpload", "discharge_summary", associatedId, offset], | ||
queryFn: query(routes.viewUpload, { | ||
queryParams: { | ||
file_type: "discharge_summary", | ||
associating_id: associatedId, | ||
is_archived: false, | ||
limit: RESULTS_PER_PAGE_LIMIT, | ||
offset: offset, | ||
silent: true, | ||
}, | ||
}), | ||
enabled: type === "consultation", | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure consistent casing of type
when checking conditions
In the useQuery
for dischargeSummaryQuery
, the enabled
condition is checking type === "consultation"
. However, elsewhere in the code, type
appears to be in uppercase (e.g., "CONSULTATION"
in the associatedId
mapping). This inconsistency could prevent the query from running as expected.
To ensure the condition works properly, normalize the type
to be uppercase or lowercase consistently throughout the code. For example:
- enabled: type === "consultation",
+ enabled: type.toUpperCase() === "CONSULTATION",
Alternatively, if type
should be lowercase, adjust the rest of the code to match.
📝 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.
const { data: dischargeSummaryQuery, isLoading: dischargeSummaryLoading } = | |
useQuery({ | |
queryKey: ["viewUpload", "discharge_summary", associatedId, offset], | |
queryFn: query(routes.viewUpload, { | |
queryParams: { | |
file_type: "discharge_summary", | |
associating_id: associatedId, | |
is_archived: false, | |
limit: RESULTS_PER_PAGE_LIMIT, | |
offset: offset, | |
silent: true, | |
}, | |
}), | |
enabled: type === "consultation", | |
}); | |
const { data: dischargeSummaryQuery, isLoading: dischargeSummaryLoading } = | |
useQuery({ | |
queryKey: ["viewUpload", "discharge_summary", associatedId, offset], | |
queryFn: query(routes.viewUpload, { | |
queryParams: { | |
file_type: "discharge_summary", | |
associating_id: associatedId, | |
is_archived: false, | |
limit: RESULTS_PER_PAGE_LIMIT, | |
offset: offset, | |
silent: true, | |
}, | |
}), | |
enabled: type.toUpperCase() === "CONSULTATION", | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abhimanyurajeesh confirm this and mark this as resolved or make changes if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!
@rithviknishad requesting for review :), Thank you |
queryKey: [ | ||
routes.retrieveUpload.path, | ||
file.id, | ||
fileManager.type, | ||
associating_id, | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
queryKey: [ | |
routes.retrieveUpload.path, | |
file.id, | |
fileManager.type, | |
associating_id, | |
], | |
queryKey: [ | |
"file", | |
{ id: file.id, type: fileManager.type, associating_id }, | |
], |
limit: RESULTS_PER_PAGE_LIMIT, | ||
offset: offset, | ||
}, | ||
const { data: activeFilesQuery, isLoading: activeFilesLoading } = useQuery({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const { data: activeFilesQuery, isLoading: activeFilesLoading } = useQuery({ | |
const { data: activeFiles, isLoading: activeFilesLoading } = useQuery({ |
prefetch: type === "consultation", | ||
silent: true, | ||
}); | ||
const { data: archivedFilesQuery, isLoading: archivedFilesLoading } = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these aren't queries, these are data objects right?
const { data: archivedFilesQuery, isLoading: archivedFilesLoading } = | |
const { data: archivedFiles, isLoading: archivedFilesLoading } = |
}), | ||
}); | ||
|
||
const { data: dischargeSummaryQuery, isLoading: dischargeSummaryLoading } = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here too
const { data: dischargeSummaryQuery, isLoading: dischargeSummaryLoading } = | ||
useQuery({ | ||
queryKey: ["viewUpload", "discharge_summary", associatedId, offset], | ||
queryFn: query(routes.viewUpload, { | ||
queryParams: { | ||
file_type: "discharge_summary", | ||
associating_id: associatedId, | ||
is_archived: false, | ||
limit: RESULTS_PER_PAGE_LIMIT, | ||
offset: offset, | ||
silent: true, | ||
}, | ||
}), | ||
enabled: type === "consultation", | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abhimanyurajeesh confirm this and mark this as resolved or make changes if needed.
const { | ||
data: userData, | ||
isLoading, | ||
refetch: refetchUserDetails, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use invalidateQueries instead of taking refetch
Part of #9837
@ohcnetwork/care-fe-code-reviewers
Merge Checklist
Summary by CodeRabbit
Release Notes
Refactor
useQuery
hookuseQuery
implementationPerformance
Maintenance