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

Structured Response View Cleanup #9797

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import { DiagnosisTable } from "@/components/Patient/diagnosis/DiagnosisTable";
import { SymptomTable } from "@/components/Patient/symptoms/SymptomTable";

import query from "@/Utils/request/query";
import { AllergyIntolerance } from "@/types/emr/allergyIntolerance/allergyIntolerance";
import allergyApi from "@/types/emr/allergyIntolerance/allergyIntoleranceApi";
import { Diagnosis } from "@/types/emr/diagnosis/diagnosis";
import diagnosisApi from "@/types/emr/diagnosis/diagnosisApi";
import { Symptom } from "@/types/emr/symptom/symptom";
import symptomApi from "@/types/emr/symptom/symptomApi";

interface Props {
type: string;
type: "symptom" | "diagnosis" | "allergy_intolerance";
id: string;
patientId: string;
encounterId: string;
Expand All @@ -25,73 +22,57 @@ export function StructuredResponseView({
patientId,
encounterId,
}: Props) {
const getRouteAndParams = () => {
const params: Record<string, string> = { patientId };
switch (type) {
case "symptom":
return {
route: symptomApi.retrieveSymptom,
pathParams: { ...params, symptomId: id },
queryParams: { encounter: encounterId },
};
case "diagnosis":
return {
route: diagnosisApi.retrieveDiagnosis,
pathParams: { ...params, diagnosisId: id },
queryParams: { encounter: encounterId },
};
case "allergy_intolerance":
return {
route: allergyApi.retrieveAllergy,
pathParams: { ...params, allergyId: id },
queryParams: { encounter: encounterId },
};
}
};
const basePathParams = { patientId };
const queryParams = { encounter: encounterId };

const routeConfig = getRouteAndParams();
const getParams = (idKey: string) => ({
pathParams: { ...basePathParams, [idKey]: id },
queryParams,
});

const { data, isLoading, error } = useQuery({
queryKey: [type, id],
queryFn: query(routeConfig?.route as any, {
pathParams: routeConfig?.pathParams,
queryParams: routeConfig?.queryParams,
}),
enabled: !!id && !!routeConfig,
const symptomQuery = useQuery({
queryKey: ["symptom"],
queryFn: query(symptomApi.retrieveSymptom, getParams("symptomId")),
enabled: type === "symptom" && !!id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve query key to differentiate requests with unique IDs.
When fetching data keyed only by "symptom", React Query may not refetch properly if id changes. Consider including the id in the query key for correct caching and refetching behavior.

- queryKey: ["symptom"],
+ queryKey: ["symptom", id],
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const symptomQuery = useQuery({
queryKey: ["symptom"],
queryFn: query(symptomApi.retrieveSymptom, getParams("symptomId")),
enabled: type === "symptom" && !!id,
const symptomQuery = useQuery({
queryKey: ["symptom", id],
queryFn: query(symptomApi.retrieveSymptom, getParams("symptomId")),
enabled: type === "symptom" && !!id,

});

if (!routeConfig) return null;
const diagnosisQuery = useQuery({
queryKey: ["diagnosis"],
queryFn: query(diagnosisApi.retrieveDiagnosis, getParams("diagnosisId")),
enabled: type === "diagnosis" && !!id,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Ensure each diagnosis fetch is uniquely identified.
Similar to the symptom query, updating the query key to include the id would prevent stale or incorrectly cached diagnosis data.

- queryKey: ["diagnosis"],
+ queryKey: ["diagnosis", id],
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const diagnosisQuery = useQuery({
queryKey: ["diagnosis"],
queryFn: query(diagnosisApi.retrieveDiagnosis, getParams("diagnosisId")),
enabled: type === "diagnosis" && !!id,
});
const diagnosisQuery = useQuery({
queryKey: ["diagnosis", id],
queryFn: query(diagnosisApi.retrieveDiagnosis, getParams("diagnosisId")),
enabled: type === "diagnosis" && !!id,
});


if (isLoading) {
return <div className="animate-pulse h-20 bg-muted rounded-md" />;
}
const allergyQuery = useQuery({
queryKey: ["allergy_intolerance"],
queryFn: query(allergyApi.retrieveAllergy, getParams("allergyId")),
enabled: type === "allergy_intolerance" && !!id,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Distinguish allergy requests with the specific ID.
Including the id in the query key would help React Query accurately track and control fetches for different allergy records.

- queryKey: ["allergy_intolerance"],
+ queryKey: ["allergy_intolerance", id],
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const allergyQuery = useQuery({
queryKey: ["allergy_intolerance"],
queryFn: query(allergyApi.retrieveAllergy, getParams("allergyId")),
enabled: type === "allergy_intolerance" && !!id,
});
const allergyQuery = useQuery({
queryKey: ["allergy_intolerance", id],
queryFn: query(allergyApi.retrieveAllergy, getParams("allergyId")),
enabled: type === "allergy_intolerance" && !!id,
});


const currentQuery = {
symptom: symptomQuery,
diagnosis: diagnosisQuery,
allergy_intolerance: allergyQuery,
}[type];

if (error) {
console.error(`Error loading ${type}:`, error);
if (currentQuery.error) {
console.error(`Error loading ${type}:`, currentQuery.error);
return <div>Error loading {type}</div>;
}

switch (type) {
case "symptom":
return (
<SymptomTable
symptoms={[data as unknown as Symptom]}
showHeader={true}
/>
symptomQuery.data && <SymptomTable symptoms={[symptomQuery.data]} />
);
case "diagnosis":
return (
<DiagnosisTable
diagnoses={[data as unknown as Diagnosis]}
showHeader={true}
/>
diagnosisQuery.data && (
<DiagnosisTable diagnoses={[diagnosisQuery.data]} />
)
);
case "allergy_intolerance":
return (
<AllergyTable
allergies={[data as unknown as AllergyIntolerance]}
showHeader={true}
/>
allergyQuery.data && <AllergyTable allergies={[allergyQuery.data]} />
);
default:
return null;
Expand Down
1 change: 0 additions & 1 deletion src/types/notes/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ export interface Message {
created_by: UserBase;
updated_by: UserBase;
}

Loading