Skip to content

Commit

Permalink
To display HIV Status badge appear on the Infant banner
Browse files Browse the repository at this point in the history
  • Loading branch information
lucyjemutai committed Oct 18, 2024
1 parent cb86c63 commit 9d1efa3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { PatientStatusBannerTag } from './patient-status-tag.component';
import { usePatientHivStatus } from './patientHivStatus';
import { usePatientOutcome } from './useInfantFinalOutcome';
import { usePatientFamilyNames } from './usePatientFamilyNames';

jest.mock('./patientHivStatus', () => ({
usePatientHivStatus: jest.fn(),
}));

jest.mock('./useInfantFinalOutcome', () => ({
usePatientOutcome: jest.fn(),
}));

jest.mock('./usePatientFamilyNames', () => ({
usePatientFamilyNames: jest.fn(),
}));
Expand All @@ -27,6 +32,10 @@ describe('PatientStatusBannerTag', () => {
isError: false,
});

(usePatientOutcome as jest.Mock).mockReturnValue({
patientOutcome: null,
});

(usePatientFamilyNames as jest.Mock).mockReturnValue({
childrenNames: [],
motherName: null,
Expand All @@ -37,7 +46,6 @@ describe('PatientStatusBannerTag', () => {
});

const { container } = render(<PatientStatusBannerTag patientUuid={hivPositiveSampleUuid} />);

expect(container.firstChild).toBeNull();
});

Expand All @@ -48,6 +56,10 @@ describe('PatientStatusBannerTag', () => {
isError: false,
});

(usePatientOutcome as jest.Mock).mockReturnValue({
patientOutcome: 'Still in Care',
});

(usePatientFamilyNames as jest.Mock).mockReturnValue({
childrenNames: [],
motherName: null,
Expand All @@ -59,6 +71,7 @@ describe('PatientStatusBannerTag', () => {

render(<PatientStatusBannerTag patientUuid={hivPositiveSampleUuid} />);
expect(screen.getByText('HIV Positive')).toBeInTheDocument();
expect(screen.getByText('Still in Care')).toBeInTheDocument();
});

it('should display the correct tag for HIV negative status', () => {
Expand All @@ -68,6 +81,10 @@ describe('PatientStatusBannerTag', () => {
isError: false,
});

(usePatientOutcome as jest.Mock).mockReturnValue({
patientOutcome: 'Confirmed HIV negative infant (discharged from PMTCT)',
});

(usePatientFamilyNames as jest.Mock).mockReturnValue({
childrenNames: [],
motherName: null,
Expand All @@ -79,6 +96,7 @@ describe('PatientStatusBannerTag', () => {

render(<PatientStatusBannerTag patientUuid={hivPositiveSampleUuid} />);
expect(screen.getByText('HIV Negative')).toBeInTheDocument();
expect(screen.getByText('Confirmed HIV negative infant (discharged from PMTCT)')).toBeInTheDocument();
});

it('should display mother’s name on the Infant banner', () => {
Expand All @@ -88,6 +106,10 @@ describe('PatientStatusBannerTag', () => {
isError: false,
});

(usePatientOutcome as jest.Mock).mockReturnValue({
patientOutcome: 'Still in Care',
});

(usePatientFamilyNames as jest.Mock).mockReturnValue({
childrenNames: [],
motherName: 'Jane Doe',
Expand All @@ -108,6 +130,10 @@ describe('PatientStatusBannerTag', () => {
isError: false,
});

(usePatientOutcome as jest.Mock).mockReturnValue({
patientOutcome: null,
});

(usePatientFamilyNames as jest.Mock).mockReturnValue({
childrenNames: [],
motherName: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ import React from 'react';
import { Tag } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { usePatientHivStatus } from './patientHivStatus';
import { usePatientOutcome } from './useInfantFinalOutcome';
import { usePatientFamilyNames } from './usePatientFamilyNames';

export function PatientStatusBannerTag({ patientUuid }) {
const { t } = useTranslation();

const { hivStatus } = usePatientHivStatus(patientUuid);

const { patientOutcome } = usePatientOutcome(patientUuid);

const greenOutcomes = ['Still in Care', 'Confirmed HIV negative infant (discharged from PMTCT)', 'Missing'];
const redOutcomes = ['Confirmed HIV Positive', 'Lost to Follow Up', 'Dead', 'Transferred Out'];

let outcomeTagColor = '';
if (greenOutcomes.includes(patientOutcome)) {
outcomeTagColor = 'green';
} else if (redOutcomes.includes(patientOutcome)) {
outcomeTagColor = 'red';
}

const { childrenNames, motherName, patientAge, patientGender, isLoading, isError } =
usePatientFamilyNames(patientUuid);

Expand All @@ -17,22 +32,18 @@ export function PatientStatusBannerTag({ patientUuid }) {
if (isError) {
return <div>Error fetching family information</div>;
}

return (
<>
{/* HIV Status Display */}
{hivStatus === 'positive' && <Tag type="red">{t('hivPositive', 'HIV Positive')}</Tag>}
{hivStatus === 'negative' && <Tag type="green">{t('hivNegative', 'HIV Negative')}</Tag>}

{/* Mother Name Display (if patient is under 10) */}
{patientOutcome && outcomeTagColor && <Tag type={outcomeTagColor}>{patientOutcome}</Tag>}

{patientAge !== null && patientAge <= 14 && motherName && <Tag type="purple">Mother: {motherName}</Tag>}

{/* Children Names Display (if patient is female and over 10) */}
{patientAge !== null && patientAge > 14 && patientGender === 'F' && childrenNames.length > 0 && (
<Tag type="purple">Children: {childrenNames.join(' || ')}</Tag>
)}
</>
);
}

export default PatientStatusBannerTag;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useState, useEffect } from 'react';
import { openmrsFetch, useConfig } from '@openmrs/esm-framework';

export const usePatientOutcome = (patientUuid: string) => {
const [patientOutcome, setPatientOutcome] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [isError, setIsError] = useState<boolean>(false);
const config = useConfig();

useEffect(() => {
const fetchPatientOutcome = async () => {
try {
const response = await fetch(
`/openmrs/ws/rest/v1/obs?patient=${patientUuid}&concept=${config.obsConcepts.outcomeStatus}&v=full`,
);
const data = await response.json();

if (data.results.length > 0) {
const outcome = data.results[0].value;
setPatientOutcome(outcome.display ?? null);
} else {
setPatientOutcome(null);
}
} catch (error) {
console.error('Failed to fetch patient outcome:', error);
setIsError(true);
} finally {
setIsLoading(false);
}
};

fetchPatientOutcome();
}, [patientUuid, config.obsConcepts.outcomeStatus]);

return { patientOutcome, isLoading, isError };
};

0 comments on commit 9d1efa3

Please sign in to comment.