diff --git a/packages/esm-ohri-pmtct-app/src/api/api.ts b/packages/esm-ohri-pmtct-app/src/api/api.ts index 59e1cf6cc..88371f5ce 100644 --- a/packages/esm-ohri-pmtct-app/src/api/api.ts +++ b/packages/esm-ohri-pmtct-app/src/api/api.ts @@ -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; - } -} +} \ No newline at end of file diff --git a/packages/esm-ohri-pmtct-app/src/views/summary-tabs/maternal-child-summary-tiles.component.tsx b/packages/esm-ohri-pmtct-app/src/views/summary-tabs/maternal-child-summary-tiles.component.tsx index 4ca7e5ecb..f5e0999db 100644 --- a/packages/esm-ohri-pmtct-app/src/views/summary-tabs/maternal-child-summary-tiles.component.tsx +++ b/packages/esm-ohri-pmtct-app/src/views/summary-tabs/maternal-child-summary-tiles.component.tsx @@ -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 ; }