-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infant's name to display on the Mother banner
- Loading branch information
1 parent
3e450ea
commit ef348a2
Showing
3 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/esm-commons-lib/src/components/banner-tags/mother-infant-tag.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useState, useEffect, useCallback } from 'react'; | ||
import { fetchPatientRelationships } from '@ohri/openmrs-esm-ohri-commons-lib'; | ||
|
||
export const usePatientFamilyNames = (patientUuid: string) => { | ||
const [childrenNames, setChildrenNames] = useState<string[]>([]); | ||
const [motherName, setMotherName] = useState<string | null>(null); | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
const [isError, setIsError] = useState<boolean>(false); | ||
|
||
const fetchFamilyData = useCallback(async () => { | ||
try { | ||
const relationships = await fetchPatientRelationships(patientUuid); | ||
|
||
if (!relationships || !Array.isArray(relationships)) { | ||
console.warn('No valid relationships data:', relationships); | ||
setChildrenNames([]); | ||
setMotherName(null); | ||
setIsLoading(false); | ||
return; | ||
} | ||
|
||
// Fetch child relationships | ||
const childRelationships = relationships | ||
.filter((relationship) => relationship.relationshipType?.displayBIsToA === 'Child') | ||
.map((relationship) => relationship.personB?.display); | ||
|
||
setChildrenNames(childRelationships); | ||
|
||
// Fetch mother's relationship | ||
const motherRelationship = relationships.find( | ||
(relationship) => | ||
relationship.relationshipType?.displayAIsToB === 'Mother' || | ||
relationship.relationshipType?.displayBIsToA === 'Child', | ||
); | ||
|
||
setMotherName(motherRelationship?.personA?.display || 'Mother not found'); | ||
|
||
setIsLoading(false); | ||
} catch (error) { | ||
console.error('Error fetching family names:', error); | ||
setIsError(true); | ||
setIsLoading(false); | ||
} | ||
}, [patientUuid]); | ||
|
||
useEffect(() => { | ||
if (patientUuid) { | ||
fetchFamilyData(); | ||
} | ||
}, [fetchFamilyData, patientUuid]); | ||
|
||
return { childrenNames, motherName, isLoading, isError }; | ||
}; |
22 changes: 21 additions & 1 deletion
22
packages/esm-commons-lib/src/components/banner-tags/patient-status-tag.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/esm-commons-lib/src/components/banner-tags/usePatientFamilyNames.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useState, useEffect, useCallback } from 'react'; | ||
import { fetchPatientRelationships } from '@ohri/openmrs-esm-ohri-commons-lib'; | ||
|
||
export const usePatientFamilyNames = (patientUuid: string) => { | ||
const [childrenNames, setChildrenNames] = useState<string[]>([]); | ||
const [motherName, setMotherName] = useState<string | null>(null); | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
const [isError, setIsError] = useState<boolean>(false); | ||
const [patientAge, setPatientAge] = useState<number | null>(null); | ||
const [patientGender, setPatientGender] = useState<string | null>(null); | ||
|
||
const fetchFamilyData = useCallback(async () => { | ||
try { | ||
// Fetch patient demographics (age and gender) | ||
const response = await fetch(`/openmrs/ws/rest/v1/patient/${patientUuid}?v=full`); | ||
const patient = await response.json(); | ||
setPatientAge(patient.person.age); | ||
setPatientGender(patient.person.gender); | ||
|
||
// Fetch relationships | ||
const relationships = await fetchPatientRelationships(patientUuid); | ||
|
||
if (!relationships || !Array.isArray(relationships)) { | ||
console.warn('No valid relationships data:', relationships); | ||
setChildrenNames([]); | ||
setMotherName(null); | ||
setIsLoading(false); | ||
return; | ||
} | ||
|
||
const childRelationships = relationships | ||
.filter((relationship) => relationship.relationshipType?.displayBIsToA === 'Child') | ||
.map((relationship) => relationship.personB?.display); | ||
|
||
setChildrenNames(childRelationships); | ||
|
||
const motherRelationship = relationships.find( | ||
(relationship) => | ||
relationship.relationshipType?.displayAIsToB === 'Mother' || | ||
relationship.relationshipType?.displayBIsToA === 'Child', | ||
); | ||
|
||
setMotherName(motherRelationship?.personA?.display || 'Mother not found'); | ||
|
||
setIsLoading(false); | ||
} catch (error) { | ||
console.error('Error fetching family names:', error); | ||
setIsError(true); | ||
setIsLoading(false); | ||
} | ||
}, [patientUuid]); | ||
|
||
useEffect(() => { | ||
if (patientUuid) { | ||
fetchFamilyData(); | ||
} | ||
}, [fetchFamilyData, patientUuid]); | ||
|
||
return { childrenNames, motherName, patientAge, patientGender, isLoading, isError }; | ||
}; |