Skip to content

Commit

Permalink
auto fetch on adding new note
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranshu1902 committed Dec 15, 2023
1 parent 849df0a commit aa160a0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
11 changes: 11 additions & 0 deletions src/Components/Facility/ConsultationDoctorNotes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CareIcon from "../../../CAREUI/icons/CareIcon";
import { NonReadOnlyUsers } from "../../../Utils/AuthorizeFor";
import { useMessageListener } from "../../../Common/hooks/useMessageListener";
import PatientConsultationNotesList from "../PatientConsultationNotesList.js";
import { StateType } from "../models.js";

interface ConsultationDoctorNotesProps {
patientId: string;
Expand All @@ -27,6 +28,13 @@ const ConsultationDoctorNotes = (props: ConsultationDoctorNotesProps) => {

const dispatch = useDispatch();

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

const onAddNote = () => {
const payload = {
note: noteField,
Expand All @@ -42,6 +50,7 @@ const ConsultationDoctorNotes = (props: ConsultationDoctorNotesProps) => {
Notification.Success({ msg: "Note added successfully" });
setNoteField("");
setReload(!reload);
setState({ ...state, cPage: 1 });
});
};

Expand Down Expand Up @@ -83,6 +92,8 @@ const ConsultationDoctorNotes = (props: ConsultationDoctorNotesProps) => {
>
<div className="mx-3 my-2 flex grow flex-col rounded-lg bg-white p-2 sm:mx-10 sm:my-5 sm:p-5">
<PatientConsultationNotesList
state={state}
setState={setState}
patientId={patientId}
facilityId={facilityId}
reload={reload}
Expand Down
32 changes: 19 additions & 13 deletions src/Components/Facility/PatientConsultationNotesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ import useSlug from "../../Common/hooks/useSlug";
import DoctorNote from "./DoctorNote";

interface PatientNotesProps {
state: StateType;
setState: any;
patientId: string;
facilityId: string;
reload?: boolean;
setReload?: any;
}

// TODO:
// 1. Adding new note isn't refetching the notes (fix needed in PatientNotesList.tsx as)

const pageSize = RESULTS_PER_PAGE_LIMIT;

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

const initialData: StateType = { notes: [], cPage: 0, totalPages: 1 };
const [state, setState] = useState(initialData);
const [isLoading, setIsLoading] = useState(true);

useQuery(routes.getPatientNotes, {
Expand All @@ -33,18 +30,27 @@ const PatientConsultationNotesList = (props: PatientNotesProps) => {
},
query: {
consultation: consultationId,
offset: state.cPage * RESULTS_PER_PAGE_LIMIT,
offset: (state.cPage - 1) * RESULTS_PER_PAGE_LIMIT,
},
prefetch: reload && state.cPage < state.totalPages,
prefetch: reload,
onResponse: ({ res, data }) => {
setIsLoading(true);
console.log(data);
console.log(state);
if (res?.status === 200 && data) {
setState((prevState: any) => ({
cPage: prevState.cPage + 1,
notes: [...prevState.notes, ...data.results],
totalPages: Math.ceil(data.count / pageSize),
}));
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),
}));
}
}
setReload(false);
setIsLoading(false);
Expand Down
30 changes: 20 additions & 10 deletions src/Components/Facility/PatientNotesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import useQuery from "../../Utils/request/useQuery";
import routes from "../../Redux/api";

interface PatientNotesProps {
state: StateType;
setState: any;
patientId: string;
facilityId: string;
reload?: boolean;
Expand All @@ -16,28 +18,36 @@ interface PatientNotesProps {
const pageSize = RESULTS_PER_PAGE_LIMIT;

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

const initialData: StateType = { notes: [], cPage: 0, totalPages: 1 };
const [state, setState] = useState(initialData);
const [isLoading, setIsLoading] = useState(true);

useQuery(routes.getPatientNotes, {
pathParams: {
patientId: props.patientId,
},
query: {
offset: state.cPage * RESULTS_PER_PAGE_LIMIT,
offset: (state.cPage - 1) * RESULTS_PER_PAGE_LIMIT,
},
prefetch: reload && state.cPage < state.totalPages,
prefetch: reload,
onResponse: ({ res, data }) => {
setIsLoading(true);
console.log(data);
console.log(state);
if (res?.status === 200 && data) {
setState((prevState: any) => ({
cPage: prevState.cPage + 1,
notes: [...prevState.notes, ...data.results],
totalPages: Math.ceil(data.count / pageSize),
}));
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),
}));
}
}
setReload(false);
setIsLoading(false);
Expand Down
7 changes: 7 additions & 0 deletions src/Components/Patient/PatientNotes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { NonReadOnlyUsers } from "../../Utils/AuthorizeFor";
import PatientNotesList from "../Facility/PatientNotesList";
import Page from "../Common/components/Page";
import { useMessageListener } from "../../Common/hooks/useMessageListener";
import { StateType } from "../Facility/models";

interface PatientNotesProps {
patientId: any;
Expand All @@ -26,6 +27,9 @@ const PatientNotes = (props: PatientNotesProps) => {

const dispatch = useDispatch();

const initialData: StateType = { notes: [], cPage: 0, totalPages: 1 };
const [state, setState] = useState(initialData);

const onAddNote = () => {
const payload = {
note: noteField,
Expand All @@ -40,6 +44,7 @@ const PatientNotes = (props: PatientNotesProps) => {
Notification.Success({ msg: "Note added successfully" });
setNoteField("");
setReload(!reload);
setState({ ...state, cPage: 1 });
});
};

Expand Down Expand Up @@ -81,6 +86,8 @@ const PatientNotes = (props: PatientNotesProps) => {
>
<div className="mx-3 my-2 flex grow flex-col rounded-lg bg-white p-2 sm:mx-10 sm:my-5 sm:p-5">
<PatientNotesList
state={state}
setState={setState}
patientId={patientId}
facilityId={facilityId}
reload={reload}
Expand Down
2 changes: 1 addition & 1 deletion src/Redux/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ const routes = {
path: "/api/v1/patient/{patientId}/notes/",
method: "GET",
TBody: Type<PatientNotesModel[]>(),
TRes: Type<PaginatedResponse<PatientNotesModel[]>>(),
TRes: Type<PaginatedResponse<PatientNotesModel>>(),
},
addPatientNote: {
path: "/api/v1/patient/{patientId}/notes/",
Expand Down

0 comments on commit aa160a0

Please sign in to comment.