Skip to content

Commit

Permalink
Remove code duplications on the PMTCT module for mamba reports data f…
Browse files Browse the repository at this point in the history
…etching (UCSF-IGHS#1740)
  • Loading branch information
lucyjemutai authored Jan 10, 2024
1 parent ebc4940 commit 0042298
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 105 deletions.
66 changes: 1 addition & 65 deletions packages/esm-ohri-pmtct-app/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,68 +118,4 @@ export function fetchChildLatestFinalOutcome(childUuid: string, conceptUuid: str
// Get family relationships from patient uuid
export async function getFamilyRelationships(patientUuid: string) {
return await fetchPatientRelationships(patientUuid);
}

// Get count of pregnant women attending first ANC
export async function getTotalPregnantWomen() {
try {
const response = await openmrsFetch('ws/rest/v1/mamba/report?report_id=total_pregnant_women');
const data = await response.json();

if (data && data.results && data.results.length > 0) {
const record = data.results[0].record;

for (const item of record) {
if (item.column === 'total_pregnant_women') {
return parseInt(item.value);
}
}
}
return 0;
} catch (error) {
console.error('Error fetching data: ', error);
return null;
}
}
// Count the number of unique women who delivered from the start of the fiscal year up to the current date
export async function getTotalDeliveries() {
try {
const response = await openmrsFetch('ws/rest/v1/mamba/report?report_id=total_deliveries');
const data = await response.json();

if (data && data.results && data.results.length > 0) {
const record = data.results[0].record;

for (const item of record) {
if (item.column === 'total_deliveries') {
return parseInt(item.value);
}
}
}
return 0;
} catch (error) {
console.error('Error fetching data: ', error);
return null;
}
}
// Total number of HIV Exposed Children Enrolled in Follow Up Care

export async function getHivExposedInfants() {
try {
const response = await openmrsFetch('ws/rest/v1/mamba/report?report_id=hiv_exposed_infants');
const data = await response.json();

if (data && data.results && data.results.length > 0) {
const record = data.results[0].record;
for (const item of record) {
if (item.column === 'total_hiv_exposed_infants') {
return parseInt(item.value);
}
}
}
return 0;
} catch (error) {
console.error('Error fetching data: ', error);
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { OHRIProgrammeSummaryTiles } from '@ohri/openmrs-esm-ohri-commons-lib';
import { getTotalDeliveries, getHivExposedInfants, getTotalPregnantWomen } from '../../api/api';
import { OHRIProgrammeSummaryTiles, fetchMambaReportData } from '@ohri/openmrs-esm-ohri-commons-lib';

function MaternalChildSummaryTiles({ launchWorkSpace }) {
const { t } = useTranslation();
const [activeClientsCount, setActiveClientsCount] = useState(0);
const [totalPregnantWomen, setTotalPregnantWomen] = useState(0);
const [totalDeliveries, setTotalDeliveries] = useState(0);
const [hivExposedInfants, setHivExposedInfants] = useState(0);
const [activeClientsCount, setActiveClientsCount] = useState(null);
const [totalPregnantWomen, setTotalPregnantWomen] = useState(null);
const [totalDeliveries, setTotalDeliveries] = useState(null);
const [hivExposedInfants, setHivExposedInfants] = useState(null);

useEffect(() => {
getTotalPregnantWomen().then((count) => {
setTotalPregnantWomen(count);
});
}, []);
const fetchData = async () => {
try {
const [totalPregnantWomenCount, totalDeliveriesCount, hivExposedInfantsCount] = await Promise.all([
fetchMambaReportData('total_pregnant_women'),
fetchMambaReportData('total_deliveries'),
fetchMambaReportData('total_hiv_exposed_infants'),
]);

useEffect(() => {
getTotalDeliveries().then((count) => {
setTotalDeliveries(count);
});
}, []);
setTotalPregnantWomen(totalPregnantWomenCount);
setTotalDeliveries(totalDeliveriesCount);
setHivExposedInfants(hivExposedInfantsCount);
} catch (error) {
console.error('Error fetching data:', error);
throw new Error('Error fetching data. Please try again.');
}
};

useEffect(() => {
getHivExposedInfants().then((count) => {
setHivExposedInfants(count);
});
fetchData();
}, []);

const tiles = [
{
title: t('anc', 'ANC'),
linkAddress: '#',
subTitle: t('pregnantWomenAttendingFirstANC', '# Pregnant women attending first ANC'),
value: totalPregnantWomen,
},
{
title: t('labourDelivery', 'Labour & Delivery'),
linkAddress: '#',
subTitle: t('totalDeliveries', '# Total deliveries'),
value: totalDeliveries,
},
{
title: t('children', 'Children'),
linkAddress: '#',
subTitle: t('hivExposedChildrenEnrolledInFollowUpCare', '# HIV Exposed children enrolled in follow up care'),
value: hivExposedInfants,
},
];
const tiles = useMemo(
() => [
{
title: t('anc', 'ANC'),
linkAddress: '#',
subTitle: t('pregnantWomenAttendingFirstANC', '# Pregnant women attending first ANC'),
value: totalPregnantWomen,
},
{
title: t('labourDelivery', 'Labour & Delivery'),
linkAddress: '#',
subTitle: t('totalDeliveries', '# Total deliveries'),
value: totalDeliveries,
},
{
title: t('children', 'Children'),
linkAddress: '#',
subTitle: t('hivExposedChildrenEnrolledInFollowUpCare', '# HIV Exposed children enrolled in follow up care'),
value: hivExposedInfants,
},
],
[t, totalPregnantWomen, totalDeliveries, hivExposedInfants],
);
return <OHRIProgrammeSummaryTiles tiles={tiles} />;
}

Expand Down

0 comments on commit 0042298

Please sign in to comment.