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

Fix Inifinite Load issue in notes #9190

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/components/Facility/PatientConsultationNotesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ const PatientConsultationNotesList = (props: PatientNotesProps) => {
const [isLoading, setIsLoading] = useState(true);

const fetchNotes = async () => {
setIsLoading(true);

const { data } = await request(routes.getPatientNotes, {
pathParams: {
patientId: props.state.patientId || "",
Expand All @@ -69,18 +67,20 @@ const PatientConsultationNotesList = (props: PatientNotesProps) => {
}));
}
}
setIsLoading(false);
setReload?.(false);
};

useEffect(() => {
if (reload) {
fetchNotes();
fetchNotes().then(() => {
setIsLoading(false);
setReload?.(false);
});
Comment on lines +74 to +77
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 23, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

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:

 fetchNotes().then(() => {
   setIsLoading(false);
   setReload?.(false);
-});
+}).catch((error) => {
+  setIsLoading(false);
+  setReload?.(false);
+  // Consider adding error state management here
+});
📝 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
fetchNotes().then(() => {
setIsLoading(false);
setReload?.(false);
});
fetchNotes().then(() => {
setIsLoading(false);
setReload?.(false);
}).catch((error) => {
setIsLoading(false);
setReload?.(false);
// Consider adding error state management here
});

Copy link
Member

@khavinshankar khavinshankar Nov 26, 2024

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.

Copy link
Contributor

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!

Copy link
Member

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?

Copy link
Contributor Author

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

}
}, [reload]);

useEffect(() => {
fetchNotes();
setIsLoading(true);
setReload?.(true);
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
}, [thread]);

useEffect(() => {
Expand Down
11 changes: 6 additions & 5 deletions src/components/Facility/PatientNotesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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
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 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;
+      };

Committable suggestion skipped: line range outside the PR's diff.

JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
}
}, [reload]);

useEffect(() => {
fetchNotes();
setIsLoading(true);
setReload(true);
}, [thread]);

useEffect(() => {
Expand Down
Loading