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 all 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 @@ -10,7 +10,10 @@ import { Card } from "@/components/ui/card";
import routes from "@/Utils/request/api";
import { formatDateTime, properCase } from "@/Utils/utils";
import { Encounter } from "@/types/emr/encounter";
import { Question } from "@/types/questionnaire/question";
import {
Question,
StructuredQuestionType,
} from "@/types/questionnaire/question";
import { QuestionnaireResponse } from "@/types/questionnaire/questionnaireResponse";

import { StructuredResponseView } from "./StructuredResponseView";
Expand Down Expand Up @@ -263,7 +266,7 @@ export default function QuestionnaireResponsesList({ encounter }: Props) {
return (
<StructuredResponseView
key={response.id}
type={type}
type={type as StructuredQuestionType}
id={response.id}
patientId={encounter.patient.id}
encounterId={encounter.id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,82 @@ 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";
import { StructuredQuestionType } from "@/types/questionnaire/question";

interface Props {
type: string;
type SupportedType = "symptom" | "diagnosis" | "allergy_intolerance";
type Props = {
type: StructuredQuestionType;
id: string;
patientId: string;
encounterId: string;
}
};

export function StructuredResponseView({
type,
id,
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 isSupportedType = (t: StructuredQuestionType): t is SupportedType => {
return ["symptom", "diagnosis", "allergy_intolerance"].includes(t);
};

const routeConfig = getRouteAndParams();
const basePathParams = { patientId };
const queryParams = { encounter: encounterId };

const { data, isLoading, error } = useQuery({
queryKey: [type, id],
queryFn: query(routeConfig?.route as any, {
pathParams: routeConfig?.pathParams,
queryParams: routeConfig?.queryParams,
}),
enabled: !!id && !!routeConfig,
const getParams = (idKey: string) => ({
pathParams: { ...basePathParams, [idKey]: id },
queryParams,
});

if (!routeConfig) return null;
const symptomQuery = useQuery({
queryKey: ["symptom", id],
queryFn: query(symptomApi.retrieveSymptom, getParams("symptomId")),
enabled: type === "symptom" && !!id,
});

if (isLoading) {
return <div className="animate-pulse h-20 bg-muted rounded-md" />;
}
const diagnosisQuery = useQuery({
queryKey: ["diagnosis", id],
queryFn: query(diagnosisApi.retrieveDiagnosis, getParams("diagnosisId")),
enabled: type === "diagnosis" && !!id,
});

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

if (!isSupportedType(type)) return null;

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