From 7458c11769028ea9a07cea43c0e8023feb40278e Mon Sep 17 00:00:00 2001 From: Uday Sagar Date: Wed, 4 Dec 2024 04:20:15 +0530 Subject: [PATCH] fix scroll position issues and incorrect data issues on switching thread --- .../Facility/PatientConsultationNotesList.tsx | 34 ++++++++-------- src/components/Facility/PatientNoteCard.tsx | 2 +- .../Facility/PatientNotesDetailedView.tsx | 15 +++++-- src/components/Facility/PatientNotesList.tsx | 40 +++++++++---------- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/components/Facility/PatientConsultationNotesList.tsx b/src/components/Facility/PatientConsultationNotesList.tsx index 49b76d4674e..36b09b443a8 100644 --- a/src/components/Facility/PatientConsultationNotesList.tsx +++ b/src/components/Facility/PatientConsultationNotesList.tsx @@ -44,7 +44,7 @@ const PatientConsultationNotesList = (props: PatientNotesProps) => { const [isLoading, setIsLoading] = useState(true); - const fetchNotes = async () => { + const fetchNotes = async (currentPage: number) => { setIsLoading(true); const { data } = await request(routes.getPatientNotes, { @@ -54,24 +54,19 @@ const PatientConsultationNotesList = (props: PatientNotesProps) => { query: { consultation: consultationId, thread, - offset: (state.cPage - 1) * RESULTS_PER_PAGE_LIMIT, + offset: (currentPage - 1) * RESULTS_PER_PAGE_LIMIT, }, }); if (data) { - if (state.cPage === 1) { - setState((prevState) => ({ - ...prevState, - notes: data.results, - totalPages: Math.ceil(data.count / pageSize), - })); - } else { - setState((prevState) => ({ - ...prevState, - notes: [...prevState.notes, ...data.results], - totalPages: Math.ceil(data.count / pageSize), - })); - } + setState((prevState) => ({ + ...prevState, + notes: + currentPage === 1 + ? data.results + : [...prevState.notes, ...data.results], + totalPages: Math.ceil(data.count / pageSize), + })); } setIsLoading(false); setReload?.(false); @@ -79,12 +74,14 @@ const PatientConsultationNotesList = (props: PatientNotesProps) => { useEffect(() => { if (reload) { - fetchNotes(); + fetchNotes(state.cPage); } }, [reload]); useEffect(() => { - fetchNotes(); + setState((prev) => ({ ...prev, notes: [], cPage: 1 })); + // Fetch notes for the first page when thread changes and prevent loading a different page when changing threads + fetchNotes(1); }, [thread]); useEffect(() => { @@ -105,7 +102,8 @@ const PatientConsultationNotesList = (props: PatientNotesProps) => { } }; - if (isLoading) { + // only show during initial fetch, to prevent scroll position from being reset + if (isLoading && state.cPage === 1) { return (
diff --git a/src/components/Facility/PatientNoteCard.tsx b/src/components/Facility/PatientNoteCard.tsx index 329365e547d..ddd9e6907e3 100644 --- a/src/components/Facility/PatientNoteCard.tsx +++ b/src/components/Facility/PatientNoteCard.tsx @@ -128,7 +128,7 @@ const PatientNoteCard = ({