From fe2dc353363df7c0024f94005e2d3f7040a91cc2 Mon Sep 17 00:00:00 2001 From: Daphne210 Date: Thu, 14 Nov 2024 20:27:08 +0300 Subject: [PATCH] Using the HIV Viral Load Test Concept --- .../src/vl-history/vl-history.component.tsx | 52 ++++---------- .../src/vl-history/vl-history.resource.ts | 71 +++++++------------ 2 files changed, 38 insertions(+), 85 deletions(-) diff --git a/packages/esm-care-panel-app/src/vl-history/vl-history.component.tsx b/packages/esm-care-panel-app/src/vl-history/vl-history.component.tsx index d479a966..cfa16d9e 100644 --- a/packages/esm-care-panel-app/src/vl-history/vl-history.component.tsx +++ b/packages/esm-care-panel-app/src/vl-history/vl-history.component.tsx @@ -1,6 +1,5 @@ import React, { useMemo, useState } from 'react'; -import { usePatientObservations } from './vl-history.resource'; -import { configSchema } from '../config-schema'; +import { parseValueString, useViralLoadOservations } from './vl-history.resource'; import { usePagination } from '@openmrs/esm-framework'; import { DataTable, @@ -26,27 +25,7 @@ interface ViralLoadProps { const ViralLoadList: React.FC = ({ patientUuid }) => { const { t } = useTranslation(); - const observationConfig = useMemo( - () => [ - { - key: 'hivViralLoadDate', - uuidConfig: configSchema.hivViralLoadDateUuid._default, - }, - { - key: 'hivViralLoadQualitative', - uuidConfig: configSchema.hivViralLoadQualitativeUuid._default, - }, - { - key: 'hivViralLoad', - uuidConfig: configSchema.hivViralLoadUuid._default, - }, - ], - [], - ); - - const conceptUuids = observationConfig.map((config) => config.uuidConfig); - - const { observations: groupedObservations, isLoading } = usePatientObservations(patientUuid, conceptUuids); + const { data, isLoading } = useViralLoadOservations(patientUuid); const pageSizes = [10, 20, 30, 40, 50]; const [currentPageSize, setPageSize] = useState(10); @@ -58,22 +37,17 @@ const ViralLoadList: React.FC = ({ patientUuid }) => { ]; const tableRows = useMemo(() => { - const rows = []; - - if (groupedObservations) { - Object.keys(groupedObservations)?.forEach((dateTime, index) => { - const group = groupedObservations[dateTime]; - rows.push({ - id: index.toString(), - hivViralLoadDate: group.dateArray?.[0]?.split('T')[0] || '', - hivViralLoadQualitative: group.displayArray?.[0] || '', - hivViralLoad: group?.valuesArray?.[0] || '', - }); - }); - } + return data?.map((item) => { + const { date, qualitative, viralLoad } = parseValueString(item?.valueString || ''); - return rows; - }, [groupedObservations]); + return { + id: item?.id, + hivViralLoadDate: date, + hivViralLoadQualitative: qualitative, + hivViralLoad: viralLoad, + }; + }); + }, [data]); const { goTo, results: paginatedData, currentPage } = usePagination(tableRows, currentPageSize); @@ -118,7 +92,7 @@ const ViralLoadList: React.FC = ({ patientUuid }) => { page={currentPage} pageSize={currentPageSize} pageSizes={pageSizes} - totalItems={tableRows.length} + totalItems={tableRows?.length} className={styles.pagination} onChange={({ pageSize, page }) => { if (pageSize !== currentPageSize) { diff --git a/packages/esm-care-panel-app/src/vl-history/vl-history.resource.ts b/packages/esm-care-panel-app/src/vl-history/vl-history.resource.ts index 77c63ae2..f2cf32a4 100644 --- a/packages/esm-care-panel-app/src/vl-history/vl-history.resource.ts +++ b/packages/esm-care-panel-app/src/vl-history/vl-history.resource.ts @@ -3,59 +3,38 @@ import { useMemo } from 'react'; import useSWR from 'swr'; import { configSchema } from '../config-schema'; -export function usePatientObservations(patientUuid, conceptUuids) { - const conceptsQueryParam = conceptUuids.join('%2C'); - const apiUrl = `${fhirBaseUrl}/Observation?patient=${patientUuid}&code=${conceptsQueryParam}&_summary=data&_sort=-date&_count=12`; +export function useViralLoadOservations(patientUuid) { + const apiUrl = `${fhirBaseUrl}/Observation?patient=${patientUuid}&code=${configSchema.viralLoadSetUuid._default}&_summary=data&_sort=-date`; const { data, error, isValidating, mutate } = useSWR<{ data: any }, Error>(apiUrl, openmrsFetch); - const observationsByDate = useMemo(() => { - if (!data) return {}; + const observations = data?.data?.entry?.map((entry) => entry.resource); - const observations = data?.data?.entry?.map((entry) => entry.resource); - - // Grouped observations by effectiveDateTime - const groupedObservations = observations?.reduce((acc, observation) => { - const { effectiveDateTime } = observation; - if (!acc[effectiveDateTime]) { - acc[effectiveDateTime] = { - observations: [], - valuesArray: [], - dateArray: [], - displayArray: [], - }; - } - acc[effectiveDateTime]?.observations.push(observation); - - const coding = observation?.code?.coding[0]; - if (coding?.code === `${configSchema.hivViralLoadUuid._default}` && observation?.valueQuantity) { - acc[effectiveDateTime].valuesArray.push(observation.valueQuantity.value); - } - - if (coding?.code === `${configSchema.hivViralLoadDateUuid._default}`) { - acc[effectiveDateTime].dateArray.push(observation.effectiveDateTime); - } - - if ( - coding?.code === `${configSchema.hivViralLoadQualitativeUuid._default}` && - observation?.valueCodeableConcept - ) { - acc[effectiveDateTime].displayArray.push(observation.valueCodeableConcept?.coding[0]?.display); - } - - if (acc[effectiveDateTime].dateArray.length === 0) { - acc[effectiveDateTime].dateArray.push(observation.effectiveDateTime); - } - - return acc; - }, {}); - - return groupedObservations; - }, [data]); return { - observations: observationsByDate, + data: observations, isLoading: isValidating, isError: !!error, mutate, }; } + +export function parseValueString(valueString) { + const parts = valueString.split(',').map((part) => part.trim()); + let date, qualitative, viralLoad; + + parts.forEach((part) => { + if (part.match(/^\d{4}-\d{2}-\d{2}$/)) { + date = part; + } else if (part === 'Target Not Detected' || part === 'DETECTED' || part === 'POOR SAMPLE QUALITY') { + qualitative = part; + } else if (!isNaN(parseFloat(part))) { + viralLoad = part; + } + }); + + return { + date: date || '', + qualitative: qualitative || '', + viralLoad: viralLoad || '', + }; +}