-
Notifications
You must be signed in to change notification settings - Fork 454
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
Fix Inifinite Load issue in notes #9190
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,6 @@ const PatientNotesList = (props: PatientNotesProps) => { | |
const [isLoading, setIsLoading] = useState(true); | ||
|
||
const fetchNotes = async () => { | ||
setIsLoading(true); | ||
const { data }: any = await request(routes.getPatientNotes, { | ||
pathParams: { patientId: props.patientId }, | ||
query: { | ||
|
@@ -53,18 +52,20 @@ const PatientNotesList = (props: PatientNotesProps) => { | |
totalPages: Math.ceil(data.count / pageSize), | ||
})); | ||
} | ||
setIsLoading(false); | ||
setReload(false); | ||
}; | ||
|
||
useEffect(() => { | ||
if (reload) { | ||
fetchNotes(); | ||
fetchNotes().then(() => { | ||
setIsLoading(false); | ||
setReload(false); | ||
}); | ||
Comment on lines
+59
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider adding error handling and preventing race conditions While moving state updates into Promise.then() is good, we should handle errors and prevent race conditions during concurrent fetches. Consider this improvement: - fetchNotes().then(() => {
- setIsLoading(false);
- setReload(false);
- });
+ let isMounted = true;
+ fetchNotes()
+ .then(() => {
+ if (isMounted) {
+ setIsLoading(false);
+ setReload(false);
+ }
+ })
+ .catch((error) => {
+ if (isMounted) {
+ console.error('Failed to fetch notes:', error);
+ setIsLoading(false);
+ setReload(false);
+ }
+ });
+ return () => {
+ isMounted = false;
+ };
JavidSumra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, [reload]); | ||
|
||
useEffect(() => { | ||
fetchNotes(); | ||
setIsLoading(true); | ||
setReload(true); | ||
}, [thread]); | ||
|
||
useEffect(() => { | ||
|
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.
Add error handling to prevent UI from being stuck in loading state
While moving the loading state update to the promise resolution is good, we should also handle errors to prevent the UI from being stuck in a loading state if the fetch fails.
Consider adding error handling:
📝 Committable suggestion
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.
@JavidSumra can u look into coderabbit comment, just check if when the fetch request fails with an error status, it's not loading infinitely.
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.
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.
@JavidSumra can you update the status here?
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.
Yeah @bodhish I'm looking into it sorry for late response.
cc @khavinshankar