From 1340d04cd15ddd3b556e1d75f95592ba7d0cbd8f Mon Sep 17 00:00:00 2001 From: Shyam prakash Date: Sun, 15 Oct 2023 17:21:16 +0530 Subject: [PATCH 1/5] Replaced useDispatch with useQuery --- src/Components/DeathReport/DeathReport.tsx | 75 ++++++++++------------ src/Redux/actions.tsx | 3 - src/Redux/api.tsx | 2 + 3 files changed, 36 insertions(+), 44 deletions(-) diff --git a/src/Components/DeathReport/DeathReport.tsx b/src/Components/DeathReport/DeathReport.tsx index be725eb3b6d..512413d697d 100644 --- a/src/Components/DeathReport/DeathReport.tsx +++ b/src/Components/DeathReport/DeathReport.tsx @@ -1,6 +1,4 @@ -import { useState, useCallback } from "react"; -import { useDispatch } from "react-redux"; -import { getPatient } from "../../Redux/actions"; +import { useState } from "react"; import { statusType, useAbortableEffect } from "../../Common/utils"; import { GENDER_TYPES } from "../../Common/constants"; import TextFormField from "../Form/FormFields/TextFormField"; @@ -13,6 +11,8 @@ import Form from "../Form/Form"; import { useTranslation } from "react-i18next"; import { navigate } from "raviger"; import dayjs from "dayjs"; +import useQuery from "../../Utils/request/useQuery"; +import routes from "../../Redux/api"; type DeathReport = { name: string; @@ -71,10 +71,8 @@ export default function PrintDeathReport(props: { id: string }) { const [patientData, setPatientData] = useState(initialState); const [patientName, setPatientName] = useState(""); - const [_isLoading, setIsLoading] = useState(true); const [isPrintMode, setIsPrintMode] = useState(false); const { id } = props; - const dispatch: any = useDispatch(); const { t } = useTranslation(); const getPatientGender = (patientData: any) => @@ -96,57 +94,52 @@ export default function PrintDeathReport(props: { id: string }) { } }; - const fetchpatient = useCallback( - async (status: statusType) => { - setIsLoading(true); - const patientRes = await dispatch(getPatient({ id })); + const { + res, + data: Rpatient, + loading: _isLoading, + refetch, + } = useQuery(routes.getPatient, { pathParams: { id } }); + + useAbortableEffect( + (status: statusType) => { + refetch(); if (!status.aborted) { - if (patientRes && patientRes.data) { - setPatientName(patientRes.data.name); - const patientGender = getPatientGender(patientRes.data); - const patientAddress = getPatientAddress(patientRes.data); - const patientComorbidities = getPatientComorbidities(patientRes.data); + if (res && Rpatient) { + setPatientName(Rpatient.name); + const patientGender = getPatientGender(Rpatient); + const patientAddress = getPatientAddress(Rpatient); + const patientComorbidities = getPatientComorbidities(Rpatient); const data = { - ...patientRes.data, + ...Rpatient, gender: patientGender, address: patientAddress, comorbidities: patientComorbidities, - is_declared_positive: patientRes.data.is_declared_positive - ? "Yes" - : "No", - is_vaccinated: patientData.is_vaccinated ? "Yes" : "No", - cause_of_death: - patientRes.data.last_consultation?.discharge_notes || "", - hospital_died_in: patientRes.data.last_consultation.facility_name, - date_declared_positive: patientRes.data.date_declared_positive - ? dayjs(patientRes.data.date_declared_positive).toDate() + is_declared_positive: Rpatient.is_declared_positive ? "Yes" : "No", + is_vaccinated: Rpatient.is_vaccinated ? "Yes" : "No", + cause_of_death: Rpatient.last_consultation?.discharge_notes || "", + hospital_died_in: Rpatient.last_consultation.facility_name, + date_declared_positive: Rpatient.date_declared_positive + ? dayjs(Rpatient.date_declared_positive).toDate() : "", - date_of_admission: patientRes.data.last_consultation.admission_date - ? dayjs(patientRes.data.last_consultation.admission_date).toDate() + date_of_admission: Rpatient.last_consultation.admission_date + ? dayjs(Rpatient.last_consultation.admission_date).toDate() : "", - date_of_test: patientRes.data.date_of_test - ? dayjs(patientRes.data.date_of_test).toDate() + date_of_test: Rpatient.date_of_test + ? dayjs(Rpatient.date_of_test).toDate() : "", - date_of_result: patientRes.data.date_of_result - ? dayjs(patientRes.data.date_of_result).toDate() + date_of_result: Rpatient.date_of_result + ? dayjs(Rpatient.date_of_result).toDate() : "", - date_of_death: patientRes.data.last_consultation.death_datetime - ? dayjs(patientRes.data.last_consultation.death_datetime).toDate() + date_of_death: Rpatient.last_consultation.death_datetime + ? dayjs(Rpatient.last_consultation.death_datetime).toDate() : "", }; setPatientData(data); } - setIsLoading(false); } }, - [dispatch, id] - ); - - useAbortableEffect( - (status: statusType) => { - fetchpatient(status); - }, - [dispatch, fetchpatient] + [Rpatient, refetch] ); const previewData = () => ( diff --git a/src/Redux/actions.tsx b/src/Redux/actions.tsx index 26e006f4cea..7b53652c9b6 100644 --- a/src/Redux/actions.tsx +++ b/src/Redux/actions.tsx @@ -414,9 +414,6 @@ export const getAllPatient = (params: object, altKey: string) => { export const createPatient = (params: object) => { return fireRequest("addPatient", [], params); }; -export const getPatient = (pathParam: object) => { - return fireRequest("getPatient", [], {}, pathParam); -}; export const updatePatient = (params: object, pathParam: object) => { return fireRequest("updatePatient", [], params, pathParam); }; diff --git a/src/Redux/api.tsx b/src/Redux/api.tsx index 8a4ca5cf1df..dd1a12503aa 100644 --- a/src/Redux/api.tsx +++ b/src/Redux/api.tsx @@ -466,6 +466,8 @@ const routes = { }, getPatient: { path: "/api/v1/patient/{id}/", + TRes: Type(), + TBody: Type<{ id: string }>(), }, updatePatient: { path: "/api/v1/patient/{id}/", From 6a7e007b492ff8392727bac6b7d54f704acff8ce Mon Sep 17 00:00:00 2001 From: Shyam prakash Date: Sun, 15 Oct 2023 17:49:26 +0530 Subject: [PATCH 2/5] revert change in actions.tsx --- src/Redux/actions.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Redux/actions.tsx b/src/Redux/actions.tsx index 7b53652c9b6..26e006f4cea 100644 --- a/src/Redux/actions.tsx +++ b/src/Redux/actions.tsx @@ -414,6 +414,9 @@ export const getAllPatient = (params: object, altKey: string) => { export const createPatient = (params: object) => { return fireRequest("addPatient", [], params); }; +export const getPatient = (pathParam: object) => { + return fireRequest("getPatient", [], {}, pathParam); +}; export const updatePatient = (params: object, pathParam: object) => { return fireRequest("updatePatient", [], params, pathParam); }; From 0c707bab092ac2e599db42eeae54b1013be15245 Mon Sep 17 00:00:00 2001 From: Shyam prakash Date: Sun, 15 Oct 2023 18:20:57 +0530 Subject: [PATCH 3/5] modify DeathReport func --- src/Components/DeathReport/DeathReport.tsx | 89 ++++++++++++---------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/src/Components/DeathReport/DeathReport.tsx b/src/Components/DeathReport/DeathReport.tsx index 512413d697d..d506ced2299 100644 --- a/src/Components/DeathReport/DeathReport.tsx +++ b/src/Components/DeathReport/DeathReport.tsx @@ -1,4 +1,5 @@ -import { useState } from "react"; +/* eslint-disable @typescript-eslint/no-unused-vars */ +import { useEffect, useState } from "react"; import { statusType, useAbortableEffect } from "../../Common/utils"; import { GENDER_TYPES } from "../../Common/constants"; import TextFormField from "../Form/FormFields/TextFormField"; @@ -71,6 +72,7 @@ export default function PrintDeathReport(props: { id: string }) { const [patientData, setPatientData] = useState(initialState); const [patientName, setPatientName] = useState(""); + const [abortState, SetAbortState] = useState(false); const [isPrintMode, setIsPrintMode] = useState(false); const { id } = props; const { t } = useTranslation(); @@ -101,46 +103,53 @@ export default function PrintDeathReport(props: { id: string }) { refetch, } = useQuery(routes.getPatient, { pathParams: { id } }); - useAbortableEffect( - (status: statusType) => { - refetch(); - if (!status.aborted) { - if (res && Rpatient) { - setPatientName(Rpatient.name); - const patientGender = getPatientGender(Rpatient); - const patientAddress = getPatientAddress(Rpatient); - const patientComorbidities = getPatientComorbidities(Rpatient); - const data = { - ...Rpatient, - gender: patientGender, - address: patientAddress, - comorbidities: patientComorbidities, - is_declared_positive: Rpatient.is_declared_positive ? "Yes" : "No", - is_vaccinated: Rpatient.is_vaccinated ? "Yes" : "No", - cause_of_death: Rpatient.last_consultation?.discharge_notes || "", - hospital_died_in: Rpatient.last_consultation.facility_name, - date_declared_positive: Rpatient.date_declared_positive - ? dayjs(Rpatient.date_declared_positive).toDate() - : "", - date_of_admission: Rpatient.last_consultation.admission_date - ? dayjs(Rpatient.last_consultation.admission_date).toDate() - : "", - date_of_test: Rpatient.date_of_test - ? dayjs(Rpatient.date_of_test).toDate() - : "", - date_of_result: Rpatient.date_of_result - ? dayjs(Rpatient.date_of_result).toDate() - : "", - date_of_death: Rpatient.last_consultation.death_datetime - ? dayjs(Rpatient.last_consultation.death_datetime).toDate() - : "", - }; - setPatientData(data); - } + useEffect(() => { + if (!abortState) { + if (res && Rpatient) { + setPatientName(Rpatient.name); + const patientGender = getPatientGender(Rpatient); + const patientAddress = getPatientAddress(Rpatient); + const patientComorbidities = getPatientComorbidities(Rpatient); + const data = { + ...Rpatient, + gender: patientGender, + address: patientAddress, + comorbidities: patientComorbidities, + is_declared_positive: Rpatient.is_declared_positive ? "Yes" : "No", + is_vaccinated: Rpatient.is_vaccinated ? "Yes" : "No", + cause_of_death: Rpatient.last_consultation?.discharge_notes || "", + hospital_died_in: Rpatient.last_consultation.facility_name, + date_declared_positive: Rpatient.date_declared_positive + ? dayjs(Rpatient.date_declared_positive).toDate() + : "", + date_of_admission: Rpatient.last_consultation.admission_date + ? dayjs(Rpatient.last_consultation.admission_date).toDate() + : "", + date_of_test: Rpatient.date_of_test + ? dayjs(Rpatient.date_of_test).toDate() + : "", + date_of_result: Rpatient.date_of_result + ? dayjs(Rpatient.date_of_result).toDate() + : "", + date_of_death: Rpatient.last_consultation.death_datetime + ? dayjs(Rpatient.last_consultation.death_datetime).toDate() + : "", + }; + setPatientData(data); } - }, - [Rpatient, refetch] - ); + } + }, [Rpatient, abortState]); + // useAbortableEffect( + // (status: statusType) => { + // refetch(); + // SetAbortState(status.aborted!); + // }, + // [refetch] + // ); + + useEffect(() => { + refetch(); + }, [refetch]); const previewData = () => (
From 949d8095e7257663a7ae702790026975ad3422e0 Mon Sep 17 00:00:00 2001 From: Shyam prakash Date: Sun, 15 Oct 2023 19:45:43 +0530 Subject: [PATCH 4/5] Resolved all errors and working as expected --- src/Components/DeathReport/DeathReport.tsx | 85 ++++++++++------------ 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/src/Components/DeathReport/DeathReport.tsx b/src/Components/DeathReport/DeathReport.tsx index d506ced2299..6cec19c9b88 100644 --- a/src/Components/DeathReport/DeathReport.tsx +++ b/src/Components/DeathReport/DeathReport.tsx @@ -72,7 +72,6 @@ export default function PrintDeathReport(props: { id: string }) { const [patientData, setPatientData] = useState(initialState); const [patientName, setPatientName] = useState(""); - const [abortState, SetAbortState] = useState(false); const [isPrintMode, setIsPrintMode] = useState(false); const { id } = props; const { t } = useTranslation(); @@ -104,52 +103,48 @@ export default function PrintDeathReport(props: { id: string }) { } = useQuery(routes.getPatient, { pathParams: { id } }); useEffect(() => { - if (!abortState) { - if (res && Rpatient) { - setPatientName(Rpatient.name); - const patientGender = getPatientGender(Rpatient); - const patientAddress = getPatientAddress(Rpatient); - const patientComorbidities = getPatientComorbidities(Rpatient); - const data = { - ...Rpatient, - gender: patientGender, - address: patientAddress, - comorbidities: patientComorbidities, - is_declared_positive: Rpatient.is_declared_positive ? "Yes" : "No", - is_vaccinated: Rpatient.is_vaccinated ? "Yes" : "No", - cause_of_death: Rpatient.last_consultation?.discharge_notes || "", - hospital_died_in: Rpatient.last_consultation.facility_name, - date_declared_positive: Rpatient.date_declared_positive - ? dayjs(Rpatient.date_declared_positive).toDate() - : "", - date_of_admission: Rpatient.last_consultation.admission_date - ? dayjs(Rpatient.last_consultation.admission_date).toDate() - : "", - date_of_test: Rpatient.date_of_test - ? dayjs(Rpatient.date_of_test).toDate() - : "", - date_of_result: Rpatient.date_of_result - ? dayjs(Rpatient.date_of_result).toDate() - : "", - date_of_death: Rpatient.last_consultation.death_datetime - ? dayjs(Rpatient.last_consultation.death_datetime).toDate() - : "", - }; - setPatientData(data); - } + if (res?.ok && Rpatient) { + setPatientName(Rpatient.name); + const patientGender = getPatientGender(Rpatient); + const patientAddress = getPatientAddress(Rpatient); + const patientComorbidities = getPatientComorbidities(Rpatient); + const data = { + ...Rpatient, + gender: patientGender, + address: patientAddress, + comorbidities: patientComorbidities, + is_declared_positive: Rpatient.is_declared_positive ? "Yes" : "No", + is_vaccinated: patientData.is_vaccinated ? "Yes" : "No", + cause_of_death: Rpatient.last_consultation?.discharge_notes || "", + hospital_died_in: Rpatient.last_consultation?.facility_name, + date_declared_positive: Rpatient.date_declared_positive + ? dayjs(Rpatient.date_declared_positive).toDate() + : "", + date_of_admission: Rpatient.last_consultation?.admission_date + ? dayjs(Rpatient.last_consultation?.admission_date).toDate() + : "", + date_of_test: Rpatient.date_of_test + ? dayjs(Rpatient.date_of_test).toDate() + : "", + date_of_result: Rpatient.date_of_result + ? dayjs(Rpatient.date_of_result).toDate() + : "", + date_of_death: Rpatient.last_consultation?.death_datetime + ? dayjs(Rpatient.last_consultation?.death_datetime).toDate() + : "", + }; + setPatientData(data); } - }, [Rpatient, abortState]); - // useAbortableEffect( - // (status: statusType) => { - // refetch(); - // SetAbortState(status.aborted!); - // }, - // [refetch] - // ); + }, [Rpatient]); - useEffect(() => { - refetch(); - }, [refetch]); + useAbortableEffect( + (status: statusType) => { + if (!status.aborted) { + refetch(); + } + }, + [refetch] + ); const previewData = () => (
From 9b6f3ab66e25964d8a1dc9233f03d7335dd1e3a4 Mon Sep 17 00:00:00 2001 From: Shyam prakash Date: Wed, 18 Oct 2023 00:20:19 +0530 Subject: [PATCH 5/5] resolved all requested changes --- src/Components/DeathReport/DeathReport.tsx | 131 ++++++++++----------- src/Redux/api.tsx | 4 +- 2 files changed, 61 insertions(+), 74 deletions(-) diff --git a/src/Components/DeathReport/DeathReport.tsx b/src/Components/DeathReport/DeathReport.tsx index 6cec19c9b88..9908b4fa852 100644 --- a/src/Components/DeathReport/DeathReport.tsx +++ b/src/Components/DeathReport/DeathReport.tsx @@ -16,30 +16,30 @@ import useQuery from "../../Utils/request/useQuery"; import routes from "../../Redux/api"; type DeathReport = { - name: string; - age: string; - gender: string; - address: string; - phone_number: string; - is_declared_positive: string; - date_declared_positive: Date | ""; - test_type: string; - date_of_test: Date | ""; - date_of_result: Date | ""; - srf_id: string; - hospital_tested_in: string; - hospital_died_in: string; - date_of_admission: Date | ""; - date_of_death: Date | ""; - comorbidities: string; - history_clinical_course: string; - brought_dead: string; - home_or_cfltc: string; - is_vaccinated: string; - kottayam_confirmation_sent: string; - kottayam_sample_date: Date | ""; - cause_of_death: string; - facility: string; + name?: string; + age?: string | number; + gender?: string; + address?: string; + phone_number?: string; + is_declared_positive?: string; + date_declared_positive: Date | string; + test_type?: string; + date_of_test?: Date | string; + date_of_result?: Date | string; + srf_id?: string; + hospital_tested_in?: string; + hospital_died_in?: string; + date_of_admission?: Date | string; + date_of_death?: Date | string; + comorbidities?: string; + history_clinical_course?: string; + brought_dead?: string; + home_or_cfltc?: string; + is_vaccinated?: string; + kottayam_confirmation_sent?: string; + kottayam_sample_date?: Date | string; + cause_of_death?: string; + facility?: string; }; export default function PrintDeathReport(props: { id: string }) { @@ -95,56 +95,43 @@ export default function PrintDeathReport(props: { id: string }) { } }; - const { - res, - data: Rpatient, - loading: _isLoading, - refetch, - } = useQuery(routes.getPatient, { pathParams: { id } }); - - useEffect(() => { - if (res?.ok && Rpatient) { - setPatientName(Rpatient.name); - const patientGender = getPatientGender(Rpatient); - const patientAddress = getPatientAddress(Rpatient); - const patientComorbidities = getPatientComorbidities(Rpatient); - const data = { - ...Rpatient, - gender: patientGender, - address: patientAddress, - comorbidities: patientComorbidities, - is_declared_positive: Rpatient.is_declared_positive ? "Yes" : "No", - is_vaccinated: patientData.is_vaccinated ? "Yes" : "No", - cause_of_death: Rpatient.last_consultation?.discharge_notes || "", - hospital_died_in: Rpatient.last_consultation?.facility_name, - date_declared_positive: Rpatient.date_declared_positive - ? dayjs(Rpatient.date_declared_positive).toDate() - : "", - date_of_admission: Rpatient.last_consultation?.admission_date - ? dayjs(Rpatient.last_consultation?.admission_date).toDate() - : "", - date_of_test: Rpatient.date_of_test - ? dayjs(Rpatient.date_of_test).toDate() - : "", - date_of_result: Rpatient.date_of_result - ? dayjs(Rpatient.date_of_result).toDate() - : "", - date_of_death: Rpatient.last_consultation?.death_datetime - ? dayjs(Rpatient.last_consultation?.death_datetime).toDate() - : "", - }; - setPatientData(data); - } - }, [Rpatient]); - - useAbortableEffect( - (status: statusType) => { - if (!status.aborted) { - refetch(); + const { loading: _isLoading } = useQuery(routes.getPatient, { + pathParams: { id }, + onResponse(res) { + if (res.res?.ok && res) { + setPatientName(res.data?.name ?? ""); + const patientGender = getPatientGender(res.data); + const patientAddress = getPatientAddress(res.data); + const patientComorbidities = getPatientComorbidities(res.data); + const data = { + ...res.data, + gender: patientGender, + address: patientAddress, + comorbidities: patientComorbidities, + is_declared_positive: res.data?.is_declared_positive ? "Yes" : "No", + is_vaccinated: res.data?.is_vaccinated ? "Yes" : "No", + cause_of_death: res.data?.last_consultation?.discharge_notes || "", + hospital_died_in: res.data?.last_consultation?.facility_name, + date_declared_positive: res.data?.date_declared_positive + ? dayjs(res.data?.date_declared_positive).toDate() + : "", + date_of_admission: res.data?.last_consultation?.admission_date + ? dayjs(res.data?.last_consultation?.admission_date).toDate() + : "", + date_of_test: res.data?.date_of_test + ? dayjs(res.data?.date_of_test).toDate() + : "", + date_of_result: res.data?.date_of_result + ? dayjs(res.data?.date_of_result).toDate() + : "", + date_of_death: res.data?.last_consultation?.death_datetime + ? dayjs(res.data?.last_consultation?.death_datetime).toDate() + : "", + }; + setPatientData(data); } }, - [refetch] - ); + }); const previewData = () => (
diff --git a/src/Redux/api.tsx b/src/Redux/api.tsx index 4e7f7fc669a..02f74577e69 100644 --- a/src/Redux/api.tsx +++ b/src/Redux/api.tsx @@ -10,6 +10,7 @@ import { } from "../Components/ExternalResult/models"; import { LocationModel, WardModel } from "../Components/Facility/models"; import { Prescription } from "../Components/Medicine/models"; +import { PatientModel } from "../Components/Patient/models"; import { UserModel } from "../Components/Users/models"; import { PaginatedResponse } from "../Utils/request/types"; @@ -474,8 +475,7 @@ const routes = { }, getPatient: { path: "/api/v1/patient/{id}/", - TRes: Type(), - TBody: Type<{ id: string }>(), + TRes: Type(), }, updatePatient: { path: "/api/v1/patient/{id}/",