Skip to content

Commit

Permalink
replace with usemutation
Browse files Browse the repository at this point in the history
  • Loading branch information
UdaySagar-Git committed Dec 19, 2024
1 parent 5bb768f commit 542a615
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 89 deletions.
8 changes: 2 additions & 6 deletions src/Utils/request/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
MinimumQuantityItemResponse,
PatientNotesEditModel,
PatientNotesModel,
PatientNotesRequest,
PatientTransferResponse,
ResourceModel,
ShiftingModel,
Expand Down Expand Up @@ -658,12 +659,7 @@ const routes = {
path: "/api/v1/patient/{patientId}/notes/",
method: "POST",
TRes: Type<PatientNotesModel>(),
TBody: Type<
Pick<PatientNotesModel, "note" | "thread"> & {
consultation?: string;
reply_to?: string;
}
>(),
TBody: Type<PatientNotesRequest>(),
},
updatePatientNote: {
path: "/api/v1/patient/{patientId}/notes/{noteId}/",
Expand Down
56 changes: 28 additions & 28 deletions src/components/Facility/PatientNotesDetailedView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import { t } from "i18next";
import { useEffect, useRef, useState } from "react";

Expand All @@ -13,6 +13,7 @@ import PatientNoteCard from "@/components/Facility/PatientNoteCard";
import {
PatientNotesModel,
PatientNotesReplyModel,
PatientNotesRequest,
} from "@/components/Facility/models";

import * as Notification from "@/Utils/Notifications";
Expand Down Expand Up @@ -60,6 +61,7 @@ const PatientNotesDetailedView = (props: Props) => {
thread,
},
}),
enabled: !!noteId && !!patientId,
});

const scrollToBottom = () => {
Expand All @@ -74,6 +76,24 @@ const PatientNotesDetailedView = (props: Props) => {
}
}, [state]);

const addNoteMutation = useMutation({
mutationFn: (noteData: PatientNotesRequest) =>
request(routes.addPatientNote, {
pathParams: { patientId },
body: noteData,
}),
onSuccess: () => {
Notification.Success({ msg: "Note added successfully" });
setNoteField("");
setReplyTo(undefined);
setTimeout(scrollToBottom, 100);
refetch();
},
onError: () => {
Notification.Error({ msg: "An error occurred while adding the note." });
},
});

const onAddNote = async () => {
if (!/\S+/.test(noteField)) {
Notification.Error({
Expand All @@ -82,34 +102,14 @@ const PatientNotesDetailedView = (props: Props) => {
return;
}

try {
const { res, data } = await request(routes.addPatientNote, {
pathParams: {
patientId: patientId,
},
body: {
note: noteField,
thread,
consultation: consultationId,
reply_to: reply_to?.id || noteId,
},
});
const result = await addNoteMutation.mutateAsync({
note: noteField,
thread: thread,
consultation: consultationId,
reply_to: reply_to?.id ?? noteId,
});

setReplyTo(undefined);

if (res?.status === 201) {
Notification.Success({ msg: "Note added successfully" });
setNoteField("");
setTimeout(scrollToBottom, 100);
} else {
Notification.Error({ msg: "Failed to add note. Please try again." });
}

return data?.id;
} catch (error) {
Notification.Error({ msg: "An error occurred while adding the note." });
return undefined;
}
return result.data?.id;
};

if (isLoading || isRefetching) {
Expand Down
60 changes: 31 additions & 29 deletions src/components/Facility/PatientNotesListComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";

Expand All @@ -12,6 +12,7 @@ import PatientNotesList from "@/components/Facility/PatientNotesList";
import {
PatientNoteStateType,
PatientNotesModel,
PatientNotesRequest,
} from "@/components/Facility/models";

import useAuthUser from "@/hooks/useAuthUser";
Expand Down Expand Up @@ -92,6 +93,7 @@ const PatientNotesListComponent = (props: PatientNotesProps) => {
thread,
},
}),
enabled: !!patientId,
});

useEffect(() => {
Expand All @@ -105,11 +107,12 @@ const PatientNotesListComponent = (props: PatientNotesProps) => {
totalPages: Math.ceil(notesData.count / RESULTS_PER_PAGE_LIMIT),
}));
}
}, [notesData]);
}, [notesData, isRefetching]);

useEffect(() => {
setThreadViewNote("");
setState((prev) => ({ ...prev, notes: [], cPage: 1 }));
setState(initialData);
refetchNotes();
}, [thread]);

const { data: patientData } = useQuery({
Expand All @@ -125,6 +128,24 @@ const PatientNotesListComponent = (props: PatientNotesProps) => {
}
}, [patientData]);

const addNoteMutation = useMutation({
mutationFn: (noteData: PatientNotesRequest) =>
request(routes.addPatientNote, {
pathParams: { patientId },
body: noteData,
}),
onSuccess: () => {
Notification.Success({ msg: "Note added successfully" });
setState((prev) => ({ ...prev, cPage: 1 }));
setNoteField("");
setReplyTo(undefined);
refetchNotes();
},
onError: () => {
Notification.Error({ msg: "An error occurred while adding the note." });
},
});

const onAddNote = async () => {
if (!/\S+/.test(noteField)) {
Notification.Error({
Expand All @@ -133,33 +154,14 @@ const PatientNotesListComponent = (props: PatientNotesProps) => {
return;
}

try {
const { res, data } = await request(routes.addPatientNote, {
pathParams: {
patientId: patientId,
},
body: {
note: noteField,
thread,
consultation: consultationId,
reply_to: reply_to?.id,
},
});

if (res?.status === 201) {
Notification.Success({ msg: "Note added successfully" });
setState({ ...state, cPage: 1 });
setNoteField("");
setReplyTo(undefined);
} else {
Notification.Error({ msg: "Failed to add note. Please try again." });
}
const result = await addNoteMutation.mutateAsync({
note: noteField,
thread: thread,
consultation: consultationId,
reply_to: reply_to?.id,
});

return data?.id;
} catch (error) {
Notification.Error({ msg: "An error occurred while adding the note." });
return undefined;
}
return result.data?.id;
};

useMessageListener((data) => {
Expand Down
58 changes: 33 additions & 25 deletions src/components/Facility/PatientNotesSlideover.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import { Link } from "raviger";
import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
Expand All @@ -12,6 +12,7 @@ import PatientNotesList from "@/components/Facility/PatientNotesList";
import {
PatientNoteStateType,
PatientNotesReplyModel,
PatientNotesRequest,
} from "@/components/Facility/models";

import useAuthUser from "@/hooks/useAuthUser";
Expand Down Expand Up @@ -98,7 +99,7 @@ export default function PatientNotesSlideover(props: PatientNotesProps) {
totalPages: Math.ceil(notesData.count / RESULTS_PER_PAGE_LIMIT),
}));
}
}, [notesData, state.cPage]);
}, [notesData, isRefetching]);

const { data: patientData } = useQuery({
queryKey: [routes.getPatient.path, patientId],
Expand Down Expand Up @@ -126,6 +127,24 @@ export default function PatientNotesSlideover(props: PatientNotesProps) {
}
}, [notificationSubscriptionState]);

const addNoteMutation = useMutation({
mutationFn: (noteData: PatientNotesRequest) =>
request(routes.addPatientNote, {
pathParams: { patientId },
body: noteData,
}),
onSuccess: () => {
Notification.Success({ msg: "Note added successfully" });
setNoteField("");
setState((prev) => ({ ...prev, cPage: 1 }));
setReplyTo(undefined);
refetchNotes();
},
onError: () => {
Notification.Error({ msg: "An error occurred while adding the note." });
},
});

const onAddNote = async () => {
if (!/\S+/.test(noteField)) {
Notification.Error({
Expand All @@ -134,30 +153,14 @@ export default function PatientNotesSlideover(props: PatientNotesProps) {
return;
}

try {
const { res, data } = await request(routes.addPatientNote, {
pathParams: { patientId: patientId },
body: {
note: noteField,
consultation: consultationId,
thread,
reply_to: reply_to?.id,
},
});
const result = await addNoteMutation.mutateAsync({
note: noteField,
thread: thread,
consultation: consultationId,
reply_to: reply_to?.id,
});

if (res?.status === 201) {
Notification.Success({ msg: "Note added successfully" });
setNoteField("");
setState({ ...state, cPage: 1 });
setReplyTo(undefined);
} else {
Notification.Error({ msg: "Failed to add note. Please try again." });
}
return data?.id;
} catch (error) {
Notification.Error({ msg: "An error occurred while adding the note." });
return undefined;
}
return result.data?.id;
};

useMessageListener((data) => {
Expand All @@ -176,6 +179,11 @@ export default function PatientNotesSlideover(props: PatientNotesProps) {
localStorage.setItem(localStorageKey, noteField);
}, [noteField, localStorageKey]);

useEffect(() => {
setState(initialData);
refetchNotes();
}, [thread]);

const notesActionIcons = (
<div className="flex gap-1">
{show && (
Expand Down
12 changes: 11 additions & 1 deletion src/components/Facility/models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,16 @@ export interface PatientNotesReplyModel {
reply_to?: string;
}

export type ThreadCategory =
(typeof PATIENT_NOTES_THREADS)[keyof typeof PATIENT_NOTES_THREADS];

export interface PatientNotesModel {
id: string;
note: string;
facility: BaseFacilityModel;
created_by_object: BaseUserModel;
user_type?: UserRole | "RemoteSpecialist";
thread: (typeof PATIENT_NOTES_THREADS)[keyof typeof PATIENT_NOTES_THREADS];
thread: ThreadCategory;
created_date: string;
last_edited_by?: BaseUserModel;
last_edited_date?: string;
Expand All @@ -579,6 +582,13 @@ export interface PatientNotesModel {
mentioned_users: UserBareMinimum[];
}

export interface PatientNotesRequest {
note: string;
thread: ThreadCategory;
consultation?: string;
reply_to?: string;
}

export interface PatientNoteStateType {
notes: PatientNotesModel[];
patientId?: string;
Expand Down

0 comments on commit 542a615

Please sign in to comment.