-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OHRI-2295 Update OHRI endpoints to use SWR
- Loading branch information
1 parent
41f1f66
commit 800d513
Showing
9 changed files
with
103 additions
and
148 deletions.
There are no files selected for viewing
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
36 changes: 0 additions & 36 deletions
36
packages/esm-commons-lib/src/components/banner-tags/.patient-status-tag.test.tsx
This file was deleted.
Oops, something went wrong.
24 changes: 15 additions & 9 deletions
24
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import React from 'react'; | ||
import { Tag } from '@carbon/react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { isPatientHivPositive } from './patientHivStatus'; | ||
import { finalHIVCodeConcept, finalPositiveHIVValueConcept } from '../../constants'; | ||
import { usePatientsFinalHIVStatus } from './usePatientHivStatus'; | ||
|
||
export function PatientStatusBannerTag({ patientUuid }) { | ||
const { t } = useTranslation(); | ||
const [hivPositive, setHivPositive] = useState(false); | ||
const { isLoading, hivStatus, error } = usePatientsFinalHIVStatus( | ||
patientUuid, | ||
finalHIVCodeConcept, | ||
finalPositiveHIVValueConcept, | ||
); | ||
|
||
useEffect(() => { | ||
isPatientHivPositive(patientUuid).then((result) => setHivPositive(result)); | ||
}, [hivPositive, patientUuid]); | ||
if (isLoading) { | ||
return <p>{t('loading', 'Loading...')}</p>; | ||
} | ||
|
||
//TODO: Improve refresh time | ||
// forceRerender(); | ||
if (error) { | ||
return <p>{t('error', 'Error...')}</p>; | ||
} | ||
|
||
return <>{hivPositive && <Tag type="red">{t('hivPositive', 'HIV Positive')}</Tag>}</>; | ||
return <>{hivStatus && <Tag type="red">{t('hivPositive', 'HIV Positive')}</Tag>}</>; | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/esm-commons-lib/src/components/banner-tags/patient-status-tag.test.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,38 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { PatientStatusBannerTag } from './patient-status-tag.component'; | ||
import { usePatientsFinalHIVStatus } from './usePatientHivStatus'; | ||
|
||
const mockedUsePatientsFinalHIVStatus = jest.mocked(usePatientsFinalHIVStatus); | ||
|
||
jest.mock('./usePatientHivStatus', () => { | ||
const originalModule = jest.requireActual('./usePatientHivStatus'); | ||
|
||
return { | ||
...originalModule, | ||
usePatientsFinalHIVStatus: jest.fn().mockImplementation(() => ({ | ||
hivStatus: true, | ||
isLoading: false, | ||
})), | ||
}; | ||
}); | ||
|
||
describe('PatientStatusBannerTag', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const samplePatientUuid = '703AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; | ||
|
||
it('renders red tag when patient is HIV positive', async () => { | ||
render(<PatientStatusBannerTag patientUuid={samplePatientUuid} />); | ||
expect(screen.getByText(/HIV Positive/i)).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not render red tag when patient is not HIV positive', async () => { | ||
mockedUsePatientsFinalHIVStatus.mockReturnValue({ hivStatus: false, isLoading: false, error: null }); | ||
render(<PatientStatusBannerTag patientUuid={samplePatientUuid} />); | ||
expect(screen.queryByText('HIV Positive')).not.toBeInTheDocument(); | ||
}); | ||
}); |
44 changes: 0 additions & 44 deletions
44
packages/esm-commons-lib/src/components/banner-tags/patientHivStatus.ts
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
packages/esm-commons-lib/src/components/banner-tags/patientStatus.test.ts
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
packages/esm-commons-lib/src/components/banner-tags/usePatientHivStatus.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,16 @@ | ||
import { fhirBaseUrl, openmrsFetch } from '@openmrs/esm-framework'; | ||
import useSWR from 'swr'; | ||
|
||
export function usePatientsFinalHIVStatus( | ||
patientUuid: string, | ||
finalHIVCodeConcept: string, | ||
finalPositiveHIVValueConcept: string, | ||
) { | ||
const url = `${fhirBaseUrl}/Observation?code=${finalHIVCodeConcept}&value-concept=${finalPositiveHIVValueConcept}&patient=${patientUuid}&_sort=-date&_count=1`; | ||
const { data, error, isLoading, mutate } = useSWR<{ data: any }, Error>(url, openmrsFetch); | ||
|
||
const hivStatusResult = data?.data?.entry[0].resource.valueCodeableConcept.coding[0].display; | ||
const hivStatus = hivStatusResult.toLowerCase().includes('positive') ? true : false; | ||
|
||
return { hivStatus: hivStatus, isLoading: isLoading, error: error }; | ||
} |
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
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