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

Integrates useInfiniteQuery for data fetching and resolves Infinite Load issue in Notes. #9190

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
af6ff98
Fix Inifinite Load issue in notes
JavidSumra Nov 23, 2024
a3abceb
Merge branch 'develop' of https://github.com/JavidSumra/care_fe into …
JavidSumra Nov 27, 2024
78873f6
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Nov 27, 2024
df1633e
Add useInfiniteQWuery Hook
JavidSumra Nov 28, 2024
25c7bb6
Fix useInfiniteQuery hook on thread change
JavidSumra Nov 28, 2024
0b5ec28
Fix Notes duplication by hashMap
JavidSumra Nov 29, 2024
79f32db
Merge branch 'develop' of https://github.com/JavidSumra/care_fe into …
JavidSumra Nov 30, 2024
575089f
Added hashmap in hook itself
JavidSumra Nov 30, 2024
c9e3b69
Implemented useInfiniteQuery hook
JavidSumra Dec 5, 2024
f6cd7f4
Merge branch 'develop' of https://github.com/JavidSumra/care_fe into …
JavidSumra Dec 5, 2024
523579c
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 5, 2024
0425d0c
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of htt…
JavidSumra Dec 5, 2024
077cb49
Remove Comments
JavidSumra Dec 5, 2024
6e9d5f1
Remove Unnecessary Export
JavidSumra Dec 5, 2024
9ef24dc
Add Requested Changes
JavidSumra Dec 5, 2024
19572c3
simplify generic types
rithviknishad Dec 5, 2024
a322aa9
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of git…
rithviknishad Dec 5, 2024
92c235b
Simplified useInfiniteQuery Implementation
JavidSumra Dec 5, 2024
4384cbf
Fix Merge Conflicts
JavidSumra Dec 5, 2024
f072d98
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 5, 2024
ef66678
Removed Unnecessary types and state
JavidSumra Dec 5, 2024
7b27e7d
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 5, 2024
51ac3ec
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of htt…
JavidSumra Dec 5, 2024
ef7e121
Remove unnecessary state updates
JavidSumra Dec 5, 2024
f2f4a1e
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 5, 2024
58159bf
Mandate duplicate function
JavidSumra Dec 7, 2024
9a47b26
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of htt…
JavidSumra Dec 7, 2024
ba8f074
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 7, 2024
c2f745b
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 9, 2024
0e1f0a2
Fix Note add issue on side notes
JavidSumra Dec 10, 2024
22f6df5
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of htt…
JavidSumra Dec 10, 2024
22ec07d
Remove reload state
JavidSumra Dec 10, 2024
4317b96
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 10, 2024
f3d063c
Fix Message Add
JavidSumra Dec 10, 2024
605f572
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of htt…
JavidSumra Dec 10, 2024
5bb1d27
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 10, 2024
28f4a09
Fix Notes Add Issue
JavidSumra Dec 10, 2024
f1054b2
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of htt…
JavidSumra Dec 10, 2024
d7bf823
Fix Notes Add Issue
JavidSumra Dec 10, 2024
01b05cf
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 11, 2024
d3e6bd1
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 11, 2024
b32f074
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 13, 2024
867c86b
Merge branch 'develop' of https://github.com/JavidSumra/care_fe into …
JavidSumra Dec 13, 2024
a152f04
Move over to useInfiniteQuery
JavidSumra Dec 13, 2024
4f9457d
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of htt…
JavidSumra Dec 13, 2024
9798653
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 13, 2024
7dd7e40
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 13, 2024
9c901d2
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 14, 2024
0fb6ddb
Merge branch 'develop' of https://github.com/JavidSumra/care_fe into …
JavidSumra Dec 14, 2024
a3f8c9e
Fix Direct Use of inbuilt hook
JavidSumra Dec 14, 2024
c604aa0
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of htt…
JavidSumra Dec 14, 2024
a68c960
Fix Deduplication of notes
JavidSumra Dec 14, 2024
79a0e0e
Add Support of useMutation
JavidSumra Dec 16, 2024
60d601f
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 16, 2024
122920d
Add Changes
JavidSumra Dec 16, 2024
be04b21
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of htt…
JavidSumra Dec 16, 2024
b750d39
Add Util hook for reusability
JavidSumra Dec 16, 2024
ee5894b
Fix type of thread
JavidSumra Dec 16, 2024
4a52dee
Remove error throw line
JavidSumra Dec 16, 2024
f78d806
Fix Merge conflict and replcae request with mutate
JavidSumra Dec 19, 2024
e6b20af
remove console
JavidSumra Dec 19, 2024
a28eda2
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 19, 2024
0e393f9
Fix Mutation function
JavidSumra Dec 19, 2024
10c8a1a
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of git…
JavidSumra Dec 19, 2024
f1946bc
Merge branch 'develop' of github.com:JavidSumra/care_fe into issues/9…
JavidSumra Dec 20, 2024
a9b78cb
Move towards using callAppi
JavidSumra Dec 20, 2024
c11fd5e
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 20, 2024
d9bd820
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 21, 2024
d137359
Add Requested Changes
JavidSumra Dec 23, 2024
bfad1af
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 23, 2024
ec89e31
Add Requested Changes
JavidSumra Dec 23, 2024
862f56b
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of git…
JavidSumra Dec 23, 2024
29e1a1c
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 23, 2024
6db0dc1
Fix Test According to changes
JavidSumra Dec 23, 2024
d7e15a5
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of git…
JavidSumra Dec 23, 2024
2472373
Add Requested Changes
JavidSumra Dec 23, 2024
25a2b5f
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 23, 2024
d3d5012
Re-run Test Suit
JavidSumra Dec 24, 2024
4d075d6
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of git…
JavidSumra Dec 24, 2024
549e012
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 24, 2024
dada00a
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 24, 2024
957cd60
Add Requested Changes
JavidSumra Dec 27, 2024
c362db5
Remove queryParam
JavidSumra Dec 27, 2024
dae6c52
Fix Path Name
JavidSumra Dec 27, 2024
89e2013
Fix queryKey
JavidSumra Dec 27, 2024
2886a6a
Merge branch 'develop' into issues/9188/discussion-note-infinite-scro…
JavidSumra Dec 27, 2024
706d79b
Re-run Test Suit
JavidSumra Dec 27, 2024
b967c97
Merge branch 'issues/9188/discussion-note-infinite-scroll-bug' of git…
JavidSumra Dec 27, 2024
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
3 changes: 0 additions & 3 deletions cypress/e2e/patient_spec/PatientDoctorNotes.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe("Patient Discussion notes in the consultation page", () => {
const patientNurseReplyNote = "Test nurse reply Notes";
const discussionNotesSubscribeWarning =
"Please subscribe to notifications to get live updates on discussion notes.";
const discussionNotesSuccessMessage = "Note added successfully";

before(() => {
loginPage.loginByRole("districtAdmin");
Expand All @@ -34,7 +33,6 @@ describe("Patient Discussion notes in the consultation page", () => {
patientDoctorNotes.selectNurseDiscussion();
patientDoctorNotes.addDiscussionNotes(patientNurseNote);
patientDoctorNotes.postDiscussionNotes();
cy.verifyNotification(discussionNotesSuccessMessage);
// verify the auto-switching of tab to nurse notes if the user is a nurse
patientDoctorNotes.signout();
loginPage.loginManuallyAsNurse();
Expand All @@ -49,7 +47,6 @@ describe("Patient Discussion notes in the consultation page", () => {
// Post a reply comment to the message
patientDoctorNotes.addDiscussionNotes(patientNurseReplyNote);
patientDoctorNotes.postDiscussionNotes();
cy.verifyNotification(discussionNotesSuccessMessage);
patientDoctorNotes.verifyDiscussionMessage(patientNurseReplyNote);
});

Expand Down
73 changes: 38 additions & 35 deletions src/components/Facility/ConsultationDoctorNotes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useQuery } from "@tanstack/react-query";
import { t } from "i18next";
import { useState } from "react";
import { useEffect, useState } from "react";
import useKeyboardShortcut from "use-keyboard-shortcut";

import CareIcon from "@/CAREUI/icons/CareIcon";
Expand All @@ -13,6 +14,7 @@ import {
PatientNoteStateType,
} from "@/components/Facility/models";
import AutoExpandingTextInputFormField from "@/components/Form/FormFields/AutoExpandingTextInputFormField";
import { useAddPatientNote } from "@/components/Patient/Utils";

import useAuthUser from "@/hooks/useAuthUser";
import { useMessageListener } from "@/hooks/useMessageListener";
Expand All @@ -22,8 +24,7 @@ import { PATIENT_NOTES_THREADS } from "@/common/constants";
import { NonReadOnlyUsers } from "@/Utils/AuthorizeFor";
import * as Notification from "@/Utils/Notifications";
import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import query from "@/Utils/request/query";
import { classNames, isAppleDevice, keysOf } from "@/Utils/utils";

interface ConsultationDoctorNotesProps {
Expand Down Expand Up @@ -54,53 +55,47 @@ const ConsultationDoctorNotes = (props: ConsultationDoctorNotesProps) => {

const initialData: PatientNoteStateType = {
notes: [],
cPage: 1,
totalPages: 1,
facilityId: facilityId,
patientId: patientId,
};
const [state, setState] = useState(initialData);

const onAddNote = async () => {
const { mutate: addNote } = useAddPatientNote({
patientId,
thread,
consultationId,
});

JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
const onAddNote = () => {
if (!/\S+/.test(noteField)) {
Notification.Error({
msg: "Note Should Contain At Least 1 Character",
});
return;
}

const { res } = await request(routes.addPatientNote, {
pathParams: {
patientId: patientId,
},
body: {
note: noteField,
thread,
consultation: consultationId,
reply_to: reply_to?.id,
},
setReplyTo(undefined);
setNoteField("");
addNote({
note: noteField,
reply_to: reply_to?.id,
thread,
consultation: consultationId,
});

if (res?.status === 201) {
Notification.Success({ msg: "Note added successfully" });
setState({ ...state, cPage: 1 });
setNoteField("");
setReload(true);
setReplyTo(undefined);
}
};

useTanStackQueryInstead(routes.getPatient, {
pathParams: { id: patientId },
onResponse: ({ data }) => {
if (data) {
setPatientActive(data.is_active ?? true);
setPatientName(data.name ?? "");
setFacilityName(data.facility_object?.name ?? "");
}
},
const { data } = useQuery({
queryKey: ["patient", patientId],
queryFn: query(routes.getPatient, {
pathParams: { patientId },
}),
});

useEffect(() => {
setPatientActive(data?.is_active ?? true);
setPatientName(data?.name ?? "");
setFacilityName(data?.facility_object?.name ?? "");
}, [data]);

JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
useMessageListener((data) => {
const message = data?.message;
if (
Expand Down Expand Up @@ -147,13 +142,21 @@ const ConsultationDoctorNotes = (props: ConsultationDoctorNotesProps) => {
? "border-primary-500 font-bold text-secondary-800"
: "border-secondary-300 text-secondary-800",
)}
onClick={() => setThread(PATIENT_NOTES_THREADS[current])}
onClick={() => {
if (thread !== PATIENT_NOTES_THREADS[current]) {
setThread(PATIENT_NOTES_THREADS[current]);
setState(initialData);
setReplyTo(undefined);
setNoteField("");
}
}}
>
{t(`patient_notes_thread__${current}`)}
</button>
))}
</div>
<PatientConsultationNotesList
key={`patient-notes-${patientId}-${thread}`}
state={state}
setState={setState}
reload={reload}
Expand Down
6 changes: 4 additions & 2 deletions src/components/Facility/DoctorNote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ interface DoctorNoteProps {
handleNext: () => void;
disableEdit?: boolean;
setReplyTo?: (reply_to: PatientNotesModel | undefined) => void;
hasMore: boolean;
}

const DoctorNote = (props: DoctorNoteProps) => {
const { state, handleNext, setReload, disableEdit, setReplyTo } = props;
const { state, handleNext, setReload, disableEdit, setReplyTo, hasMore } =
props;

return (
<div
Expand All @@ -27,7 +29,7 @@ const DoctorNote = (props: DoctorNoteProps) => {
{state.notes.length ? (
<InfiniteScroll
next={handleNext}
hasMore={state.cPage < state.totalPages}
hasMore={hasMore}
loader={
<div className="flex items-center justify-center">
<CircularProgress />
Expand Down
95 changes: 40 additions & 55 deletions src/components/Facility/PatientConsultationNotesList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
import { useInfiniteQuery } from "@tanstack/react-query";
import { Dispatch, SetStateAction, useEffect } from "react";

import CircularProgress from "@/components/Common/CircularProgress";
import DoctorNote from "@/components/Facility/DoctorNote";
Expand All @@ -12,7 +13,7 @@ import useSlug from "@/hooks/useSlug";
import { RESULTS_PER_PAGE_LIMIT } from "@/common/constants";

import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import { callApi } from "@/Utils/request/query";

interface PatientNotesProps {
state: PatientNoteStateType;
Expand All @@ -24,80 +25,63 @@ interface PatientNotesProps {
setReplyTo?: (value: PatientNotesModel | undefined) => void;
}

const pageSize = RESULTS_PER_PAGE_LIMIT;

const PatientConsultationNotesList = (props: PatientNotesProps) => {
const {
state,
setState,
reload,
setReload,
disableEdit,
thread,
setReplyTo,
reload,
} = props;
const consultationId = useSlug("consultation") ?? "";

const [isLoading, setIsLoading] = useState(true);

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

const { data } = await request(routes.getPatientNotes, {
pathParams: {
patientId: props.state.patientId || "",
},
query: {
consultation: consultationId,
thread,
offset: (state.cPage - 1) * RESULTS_PER_PAGE_LIMIT,
},
});
const consultationId = useSlug("consultation") ?? "";

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),
}));
const { data, isLoading, fetchNextPage, hasNextPage } = useInfiniteQuery({
queryKey: ["notes", state.patientId, thread, consultationId],
queryFn: async ({ pageParam = 0, signal }) => {
const response = await callApi(routes.getPatientNotes, {
pathParams: { patientId: state.patientId! },
queryParams: {
thread,
offset: pageParam,
consultation: consultationId,
},
signal,
});
return {
results: response?.results ?? [],
nextPage: pageParam + RESULTS_PER_PAGE_LIMIT,
totalResults: response?.count ?? 0,
};
},
getNextPageParam: (lastPage, allPages) => {
const currentResults = allPages.flatMap((page) => page.results).length;
if (currentResults < lastPage.totalResults) {
return lastPage.nextPage;
}
}
setIsLoading(false);
setReload?.(false);
};
return undefined;
},
initialPageParam: 0,
});

useEffect(() => {
if (reload) {
fetchNotes();
}
}, [reload]);
if (data?.pages) {
const allNotes = data.pages.flatMap((page) => page.results);

useEffect(() => {
fetchNotes();
}, [thread]);
const notesMap = new Map(allNotes.map((note) => [note.id, note]));

useEffect(() => {
setReload?.(true);
}, []);
const deduplicatedNotes = Array.from(notesMap.values());

const handleNext = () => {
if (state.cPage < state.totalPages) {
setState((prevState) => ({
...prevState,
cPage: prevState.cPage + 1,
notes: deduplicatedNotes,
}));
setReload?.(true);
}
};
}, [data]);

if (isLoading) {
if (isLoading || reload) {
return (
<div className="flex h-full w-full items-center justify-center bg-white">
<CircularProgress />
Expand All @@ -108,10 +92,11 @@ const PatientConsultationNotesList = (props: PatientNotesProps) => {
return (
<DoctorNote
state={state}
handleNext={handleNext}
handleNext={fetchNextPage}
setReload={setReload}
disableEdit={disableEdit}
setReplyTo={setReplyTo}
hasMore={hasNextPage}
/>
);
};
Expand Down
Loading
Loading