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

Fix: Correct relationship display on workspace forms #291

Merged
merged 6 commits into from
Aug 1, 2024
Merged
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 @@ -14,9 +14,11 @@ interface CaseManagerResponse {
results: CaseManager[];
}

interface RelationshipType {
export interface RelationshipType {
uuid: string;
display: string;
displayAIsToB: string;
displayBIsToA: string;
}

export interface RelationshipTypeResponse {
Expand Down Expand Up @@ -50,11 +52,31 @@ export const useCaseManagers = () => {
};

export const useRelationshipType = () => {
const customRepresentation = 'custom:(uuid,display)&q=Case manager';
const customRepresentation = 'custom:(uuid,display,displayAIsToB,displayBIsToA)&q=Case manager';
const url = `/ws/rest/v1/relationshiptype?v=${customRepresentation}`;
const { data, error } = useSWRImmutable<{ data: RelationshipTypeResponse }>(url, openmrsFetch);

return { data, error };
const tmp: {
uuid: string;
display: string;
direction: string;
}[] = [];
amosmachora marked this conversation as resolved.
Show resolved Hide resolved

data?.data.results.forEach((type) => {
const aIsToB = {
display: type.displayAIsToB ? type.displayAIsToB : type.displayBIsToA,
Copy link
Contributor

@ojwanganto ojwanganto Jul 30, 2024

Choose a reason for hiding this comment

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

What is exactly do we want to achieve with this? Should we switch the displays if one is missing? It may not be a good approach

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That is how they have it implemented in the community. So I figured they probably had a good reason why.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do you think I should remove it ?

uuid: type.uuid,
direction: 'aIsToB',
};
const bIsToA = {
display: type.displayBIsToA ? type.displayBIsToA : type.displayAIsToB,
uuid: type.uuid,
direction: 'bIsToA',
};
aIsToB.display === bIsToA.display ? tmp.push(aIsToB) : tmp.push(aIsToB, bIsToA);
});

return { data: tmp, error };
};

export const saveRelationship = (payload) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const schema = z.object({
caseManager: z.string().nonempty({ message: 'Case Manager is required' }),
relationship: z.string().nonempty({ message: 'Relationship is required' }),
startDate: z.date({ required_error: 'Start Date is required' }),
// reasons: z.string().nonempty({ message: 'At least one reason is required' }),
endDate: z.date().optional(),
notes: z.string().optional(),
});
Expand All @@ -30,8 +29,15 @@ const CaseManagementForm: React.FC<CaseManagementProp> = ({ closeWorkspace }) =>
const [patientUuid, setPatientUuid] = useState('');
const [patientSelected, setPatientSelected] = useState(false);

const { data, error } = useCaseManagers();
const { data } = useCaseManagers();
const { data: relationshipTypesData } = useRelationshipType();

const caseManagerRlshipType =
relationshipTypesData?.map((relationship) => ({
id: relationship.uuid,
text: relationship.display,
})) || [];

const caseManagerUuid = user?.person.uuid;
const { mutate: fetchCases } = useActivecases(caseManagerUuid);

Expand All @@ -41,12 +47,6 @@ const CaseManagementForm: React.FC<CaseManagementProp> = ({ closeWorkspace }) =>
text: manager.display,
})) || [];

const caseManagerRlshipType =
relationshipTypesData?.data.results.map((relationship) => ({
id: relationship.uuid,
text: relationship.display,
})) || [];

const conceptReasons = useMemo(() => {
return Object.keys(caseManagementConceptMap.answers).map((key) => ({
id: key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const FamilyHistory: React.FC<FamilyHistoryProps> = ({ patientUuid }) => {
const layout = useLayoutType();
const [pageSize, setPageSize] = useState(10);
const { relationships, error, isLoading, isValidating } = useRelationships(patientUuid);

const familyRelationshipTypeUUIDs = new Set(familyRelationshipsTypeList.map((type) => type.uuid));
const familyRelationships = relationships.filter((r) => familyRelationshipTypeUUIDs.has(r.relationshipTypeUUID));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { uppercaseText } from '../utils/expression-helper';
import { saveRelationship } from '../case-management/workspace/case-management.resource';
import PatientInfo from '../case-management/workspace/patient-info.component';
import { mutate } from 'swr';
import { useAllRelationshipTypes, useRelationships } from './relationships.resource';
import { useAllRelationshipTypes, useFamilyRelationshipTypes, useRelationships } from './relationships.resource';
import { ConfigObject } from '../config-schema';

const schema = z.object({
Expand All @@ -30,9 +30,9 @@ const FamilyRelationshipForm: React.FC<RelationshipFormProps> = ({ closeWorkspac
const { t } = useTranslation();
const [relatedPersonUuid, setRelatedPersonUuid] = useState<string | undefined>(undefined);
const { relationshipsUrl } = useRelationships(rootPersonUuid);
const { familyRelationshipsTypeList } = useConfig<ConfigObject>();
const { data: familyRelationshipsTypes } = useFamilyRelationshipTypes();

const relationshipTypes = familyRelationshipsTypeList.map((relationship) => ({
const relationshipTypes = familyRelationshipsTypes.map((relationship) => ({
id: relationship.uuid,
text: relationship.display,
}));
Expand Down Expand Up @@ -107,7 +107,7 @@ const FamilyRelationshipForm: React.FC<RelationshipFormProps> = ({ closeWorkspac
<ComboBox
id="relationship_name"
titleText={t('relationship', 'Relationship')}
placeholder="Select Relationship"
placeholder="Relationship to patient"
items={relationshipTypes}
itemToString={(item) => (item ? uppercaseText(item.text) : '')}
onChange={(e) => field.onChange(e.selectedItem?.id)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useMemo } from 'react';
import useSWR from 'swr';
import { type FetchResponse, openmrsFetch, FHIRResource, restBaseUrl } from '@openmrs/esm-framework';
import { type FetchResponse, openmrsFetch, FHIRResource, restBaseUrl, useConfig } from '@openmrs/esm-framework';
import useSWRImmutable from 'swr/immutable';
import { RelationshipTypeResponse } from '../case-management/workspace/case-management.resource';
import { ConfigObject } from '../config-schema';

interface RelationshipsResponse {
results: Array<Relationship>;
Expand Down Expand Up @@ -48,6 +49,12 @@ type FHIRResourceResponse = {
entry: Array<FHIRResource>;
};

interface RelationshipType {
uuid: string;
display: string;
direction: string;
}

export const useCodedConceptObservations = (patientUuid: string, conceptUuid: string) => {
const url = `/ws/fhir2/R4/Observation?subject:Patient=${patientUuid}&code=${conceptUuid}&_summary=data&_sort=-date&_count=100`;

Expand Down Expand Up @@ -79,13 +86,75 @@ function mapObservations(obsData) {
}

export const useAllRelationshipTypes = () => {
const customRepresentation = 'custom:(uuid,display)';
const url = `${restBaseUrl}/relationshiptype?v=${customRepresentation}`;
const url = `${restBaseUrl}/relationshiptype?v=default`;
const { data, error } = useSWRImmutable<{ data: RelationshipTypeResponse }>(url, openmrsFetch);

return { data, error };
};

export const useFamilyRelationshipTypes = () => {
amosmachora marked this conversation as resolved.
Show resolved Hide resolved
const url = `${restBaseUrl}/relationshiptype?v=default`;
const { data, error } = useSWRImmutable<{ data?: RelationshipTypeResponse }>(url, openmrsFetch);
const { familyRelationshipsTypeList } = useConfig<ConfigObject>();
const familyRelationshipTypesUUIDs = new Set(familyRelationshipsTypeList.map((r) => r.uuid));

const tmp: RelationshipType[] = [];

data?.data.results
.filter((type) => familyRelationshipTypesUUIDs.has(type.uuid))
.forEach((type) => {
const aIsToB = {
display: type.displayAIsToB ? type.displayAIsToB : type.displayBIsToA,
uuid: type.uuid,
direction: 'aIsToB',
};
const bIsToA = {
display: type.displayBIsToA ? type.displayBIsToA : type.displayAIsToB,
uuid: type.uuid,
direction: 'bIsToA',
};
aIsToB.display === bIsToA.display
? tmp.push(aIsToB)
: bIsToA.display === 'Patient'
? tmp.push(aIsToB, { display: `Patient (${aIsToB.display})`, uuid: type.uuid, direction: 'bIsToA' })
: tmp.push(aIsToB, bIsToA);
});

return { data: tmp, error };
};

// Hook for getting all other relationship types other than "family types";
export const useOtherRelationshipTypes = () => {
const url = `${restBaseUrl}/relationshiptype?v=default`;
const { data, error } = useSWRImmutable<{ data?: RelationshipTypeResponse }>(url, openmrsFetch);
const { familyRelationshipsTypeList } = useConfig<ConfigObject>();
const familyRelationshipTypesUUIDs = new Set(familyRelationshipsTypeList.map((r) => r.uuid));

const tmp: RelationshipType[] = [];
amosmachora marked this conversation as resolved.
Show resolved Hide resolved

data?.data.results
.filter((type) => !familyRelationshipTypesUUIDs.has(type.uuid))
.forEach((type) => {
const aIsToB = {
display: type.displayAIsToB ? type.displayAIsToB : type.displayBIsToA,
uuid: type.uuid,
direction: 'aIsToB',
};
const bIsToA = {
display: type.displayBIsToA ? type.displayBIsToA : type.displayAIsToB,
uuid: type.uuid,
direction: 'bIsToA',
};
aIsToB.display === bIsToA.display
? tmp.push(aIsToB)
: bIsToA.display === 'Patient'
? tmp.push(aIsToB, { display: `Patient (${aIsToB.display})`, uuid: type.uuid, direction: 'bIsToA' })
: tmp.push(aIsToB, bIsToA);
});

return { data: tmp, error };
};

export function useRelationships(patientUuid: string) {
const customRepresentation =
'custom:(display,uuid,personA:(uuid,age,display,dead,causeOfDeath),personB:(uuid,age,display,dead,causeOfDeath),relationshipType:(uuid,display,description,aIsToB,bIsToA))';
Expand Down Expand Up @@ -145,6 +214,8 @@ function extractRelationshipData(
relativeUuid: r.personA.uuid,
dead: r.personA.dead,
relationshipType: r.relationshipType.aIsToB,
relationshipTypeDisplay: r.relationshipType.display,
relationshipTypeUUID: r.relationshipType.uuid,
patientUuid: r.personA.uuid,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { uppercaseText } from '../utils/expression-helper';
import { saveRelationship } from '../case-management/workspace/case-management.resource';
import PatientInfo from '../case-management/workspace/patient-info.component';
import { mutate } from 'swr';
import { useAllRelationshipTypes, useRelationships } from '../family-partner-history/relationships.resource';
import {
useAllRelationshipTypes,
useOtherRelationshipTypes,
useRelationships,
} from '../family-partner-history/relationships.resource';
import { ConfigObject } from '../config-schema';

const schema = z.object({
Expand All @@ -30,17 +34,12 @@ export const OtherRelationshipsForm: React.FC<OtherRelationshipsFormProps> = ({
const { t } = useTranslation();
const [relatedPersonUuid, setRelatedPersonUuid] = useState<string | undefined>(undefined);
const { relationshipsUrl } = useRelationships(rootPersonUuid);
const { data: relationshipTypesData } = useAllRelationshipTypes();
const { familyRelationshipsTypeList } = useConfig<ConfigObject>();
const familyRelationshipTypeUUIDs = new Set(familyRelationshipsTypeList.map((type) => type.uuid));
const { data: relationshipTypesData } = useOtherRelationshipTypes();

const relationshipTypes =
relationshipTypesData?.data.results
.map((relationship) => ({
id: relationship.uuid,
text: relationship.display,
}))
.filter((r) => !familyRelationshipTypeUUIDs.has(r.id)) || [];
const relationshipTypes = relationshipTypesData.map((relationship) => ({
id: relationship.uuid,
text: relationship.display,
}));

const {
control,
Expand Down
Loading