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

U4X-837: Using the HIV Viral Load Test Set Concept #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
52 changes: 13 additions & 39 deletions packages/esm-care-panel-app/src/vl-history/vl-history.component.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -26,27 +25,7 @@ interface ViralLoadProps {
const ViralLoadList: React.FC<ViralLoadProps> = ({ 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);
Expand All @@ -58,22 +37,17 @@ const ViralLoadList: React.FC<ViralLoadProps> = ({ 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);

Expand Down Expand Up @@ -118,7 +92,7 @@ const ViralLoadList: React.FC<ViralLoadProps> = ({ patientUuid }) => {
page={currentPage}
pageSize={currentPageSize}
pageSizes={pageSizes}
totalItems={tableRows.length}
totalItems={tableRows?.length}
className={styles.pagination}
onChange={({ pageSize, page }) => {
if (pageSize !== currentPageSize) {
Expand Down
71 changes: 25 additions & 46 deletions packages/esm-care-panel-app/src/vl-history/vl-history.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '',
};
}
Loading