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

Adds useInfiniteQuery data fetching hook + fixes Inifinite Load issue in notes #9190

Open
wants to merge 52 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
52 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
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
62 changes: 62 additions & 0 deletions src/Utils/request/useInfiniteQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useCallback, useState } from "react";

import { RESULTS_PER_PAGE_LIMIT } from "@/common/constants";

import { PaginatedResponse, QueryRoute } from "@/Utils/request/types";
import useQuery, { QueryOptions } from "@/Utils/request/useQuery";

export interface InfiniteQueryOptions<TItem>
extends QueryOptions<PaginatedResponse<TItem>> {
deduplicateBy?: (item: TItem) => string | number;
}
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved

export function useInfiniteQuery<TItem>(
route: QueryRoute<PaginatedResponse<TItem>>,
options?: InfiniteQueryOptions<TItem>,
) {
const [items, setItems] = useState<TItem[]>([]);
const [totalCount, setTotalCount] = useState<number>();
const [offset, setOffset] = useState(0);

const { refetch, loading, ...queryResponse } = useQuery(route, {
...options,
query: {
...(options?.query ?? {}),
offset,
},
onResponse: ({ data }) => {
if (!data) return;
const allItems = items.concat(data.results);

const deduplicatedItems = options?.deduplicateBy
? Array.from(
allItems
.reduce(
(map, item) => map.set(options.deduplicateBy!(item), item),
new Map<string | number, TItem>(),
)
.values(),
)
: allItems;
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved

setItems(deduplicatedItems);
setTotalCount(data.count);
},
});

const fetchNextPage = useCallback(async () => {
if (loading) return;

setOffset((prevOffset) => prevOffset + RESULTS_PER_PAGE_LIMIT);
}, [offset, loading]);

return {
items,
loading,
fetchNextPage,
refetch,
totalCount,
hasMore: items.length < (totalCount ?? 0),
...queryResponse,
};
}
10 changes: 6 additions & 4 deletions src/components/Facility/ConsultationDoctorNotes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ const ConsultationDoctorNotes = (props: ConsultationDoctorNotesProps) => {

const initialData: PatientNoteStateType = {
notes: [],
cPage: 1,
totalPages: 1,
facilityId: facilityId,
patientId: patientId,
};
Expand Down Expand Up @@ -83,7 +81,7 @@ const ConsultationDoctorNotes = (props: ConsultationDoctorNotesProps) => {

if (res?.status === 201) {
Notification.Success({ msg: "Note added successfully" });
setState({ ...state, cPage: 1 });
setState({ ...state });
setNoteField("");
setReload(true);
setReplyTo(undefined);
Expand Down Expand Up @@ -147,13 +145,17 @@ 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={() => {
setThread(PATIENT_NOTES_THREADS[current]);
setState(initialData);
}}
>
{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
90 changes: 29 additions & 61 deletions src/components/Facility/PatientConsultationNotesList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
import { Dispatch, SetStateAction, useEffect } from "react";

import CircularProgress from "@/components/Common/CircularProgress";
import DoctorNote from "@/components/Facility/DoctorNote";
Expand All @@ -9,10 +9,8 @@ import {

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 { useInfiniteQuery } from "@/Utils/request/useInfiniteQuery";

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

const pageSize = RESULTS_PER_PAGE_LIMIT;

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

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

const fetchNotes = async () => {
setIsLoading(true);
const consultationId = useSlug("consultation") ?? "";

const { data } = await request(routes.getPatientNotes, {
pathParams: {
patientId: props.state.patientId || "",
},
query: {
consultation: consultationId,
thread,
offset: (state.cPage - 1) * RESULTS_PER_PAGE_LIMIT,
},
});
const {
items: notes,
loading,
fetchNextPage,
hasMore,
} = useInfiniteQuery<PatientNotesModel>(routes.getPatientNotes, {
pathParams: {
patientId: props.state.patientId || "",
},
query: {
consultation: consultationId,
thread,
},
prefetch: true,
});

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),
}));
}
}
setIsLoading(false);
setReload?.(false);
};
useEffect(() => {
setState((prevState: any) => ({
...prevState,
notes,
}));
}, [notes, setState]);

useEffect(() => {
if (reload) {
fetchNotes();
fetchNextPage().then(() => setReload?.(false));
}
}, [reload]);

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

useEffect(() => {
setReload?.(true);
}, []);

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

if (isLoading) {
if (loading && !state.notes.length) {
return (
<div className="flex h-full w-full items-center justify-center bg-white">
<CircularProgress />
Expand All @@ -108,10 +75,11 @@ const PatientConsultationNotesList = (props: PatientNotesProps) => {
return (
<DoctorNote
state={state}
handleNext={handleNext}
handleNext={fetchNextPage}
setReload={setReload}
disableEdit={disableEdit}
setReplyTo={setReplyTo}
hasMore={hasMore}
/>
);
};
Expand Down
1 change: 0 additions & 1 deletion src/components/Facility/PatientNoteCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ const PatientNoteCard = ({

return (
<>
{" "}
<div
className={classNames(
"mt-4 flex w-full flex-col rounded-lg border border-secondary-300 bg-white p-3 text-secondary-800",
Expand Down
91 changes: 29 additions & 62 deletions src/components/Facility/PatientNotesList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect } from "react";

import CircularProgress from "@/components/Common/CircularProgress";
import DoctorNote from "@/components/Facility/DoctorNote";
Expand All @@ -7,10 +7,8 @@ import {
PatientNotesModel,
} from "@/components/Facility/models";

import { RESULTS_PER_PAGE_LIMIT } from "@/common/constants";

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

interface PatientNotesProps {
state: PatientNoteStateType;
Expand All @@ -22,66 +20,34 @@ interface PatientNotesProps {
thread: PatientNotesModel["thread"];
setReplyTo?: (reply_to: PatientNotesModel | undefined) => void;
}

const pageSize = RESULTS_PER_PAGE_LIMIT;

const PatientNotesList = (props: PatientNotesProps) => {
const { state, setState, reload, setReload, thread, setReplyTo } = props;

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

const fetchNotes = async () => {
setIsLoading(true);
const { data }: any = await request(routes.getPatientNotes, {
pathParams: { patientId: props.patientId },
query: {
offset: (state.cPage - 1) * RESULTS_PER_PAGE_LIMIT,
thread,
},
});

if (state.cPage === 1) {
setState((prevState: any) => ({
...prevState,
notes: data.results,
totalPages: Math.ceil(data.count / pageSize),
}));
} else {
setState((prevState: any) => ({
...prevState,
notes: [...prevState.notes, ...data.results],
totalPages: Math.ceil(data.count / pageSize),
}));
}
setIsLoading(false);
setReload(false);
};
const { state, setState, thread, setReplyTo } = props;

const {
items: notes,
loading,
fetchNextPage,
refetch,
hasMore,
} = useInfiniteQuery<PatientNotesModel>(routes.getPatientNotes, {
deduplicateBy: (note) => note.id,
query: {
thread,
offset: 0,
},
pathParams: {
patientId: props.patientId,
},
});

JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
if (reload) {
fetchNotes();
}
}, [reload]);

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

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

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

if (isLoading) {
if (loading && !state.notes.length) {
return (
<div className="flex h-full w-full items-center justify-center bg-white">
<CircularProgress />
Expand All @@ -92,9 +58,10 @@ const PatientNotesList = (props: PatientNotesProps) => {
return (
<DoctorNote
state={state}
handleNext={handleNext}
setReload={setReload}
handleNext={fetchNextPage}
setReload={refetch}
setReplyTo={setReplyTo}
hasMore={hasMore}
/>
);
};
Expand Down
Loading
Loading