diff --git a/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/hooks/getSubValueForTei.js b/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/hooks/getSubValueForTei.js index b8df8130ed..eab8d88a89 100644 --- a/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/hooks/getSubValueForTei.js +++ b/src/core_modules/capture-core/components/DataEntries/EnrollmentRegistrationEntry/hooks/getSubValueForTei.js @@ -1,6 +1,8 @@ // @flow import type { QuerySingleResource } from 'capture-core/utils/api'; import { dataElementTypes } from '../../../../metaData'; +import { getOrgUnitNames } from '../../../../metadataRetrieval/orgUnitName'; + const getImageOrFileResourceSubvalue = async (key: string, querySingleResource: QuerySingleResource) => { const { id, displayName: name } = await querySingleResource({ resource: 'fileResources', id: key }); @@ -11,14 +13,8 @@ const getImageOrFileResourceSubvalue = async (key: string, querySingleResource: }; const getOrganisationUnitSubvalue = async (key: string, querySingleResource: QuerySingleResource) => { - const organisationUnit = await querySingleResource({ - resource: 'organisationUnits', - id: key, - params: { - fields: 'id,name', - }, - }); - return { ...organisationUnit }; + const organisationUnit = await getOrgUnitNames([key], querySingleResource); + return organisationUnit[key]; }; export const subValueGetterByElementType = { diff --git a/src/core_modules/capture-core/components/Tooltips/TooltipOrgUnit/TooltipOrgUnit.component.js b/src/core_modules/capture-core/components/Tooltips/TooltipOrgUnit/TooltipOrgUnit.component.js index 8b756f7e9c..7173df8144 100644 --- a/src/core_modules/capture-core/components/Tooltips/TooltipOrgUnit/TooltipOrgUnit.component.js +++ b/src/core_modules/capture-core/components/Tooltips/TooltipOrgUnit/TooltipOrgUnit.component.js @@ -1,18 +1,19 @@ // @flow import React from 'react'; import { Tooltip } from '@dhis2/ui'; +import { useOrgUnitNameWithAncestors } from '../../../metadataRetrieval/orgUnitName'; type Props = { - orgUnitName: string, - ancestors?: Array, + orgUnitId: string, }; -export const TooltipOrgUnit = ({ orgUnitName, ancestors = [] }: Props) => { - const fullPath = [...ancestors, orgUnitName].join(' / '); +export const TooltipOrgUnit = ({ orgUnitId }: Props) => { + const { displayName, ancestors = [] } = useOrgUnitNameWithAncestors(orgUnitId); + const fullPath = [...ancestors, displayName].join(' / '); return ( - - {orgUnitName} + + {displayName} ); }; diff --git a/src/core_modules/capture-core/components/WidgetEnrollment/WidgetEnrollment.component.js b/src/core_modules/capture-core/components/WidgetEnrollment/WidgetEnrollment.component.js index 3bec583e85..00c44c0a6e 100644 --- a/src/core_modules/capture-core/components/WidgetEnrollment/WidgetEnrollment.component.js +++ b/src/core_modules/capture-core/components/WidgetEnrollment/WidgetEnrollment.component.js @@ -78,8 +78,8 @@ export const WidgetEnrollmentPlain = ({ const { displayName: orgUnitName, ancestors } = useOrgUnitNameWithAncestors(enrollment?.orgUnit); const { displayName: ownerOrgUnitName, ancestors: ownerAncestors } = useOrgUnitNameWithAncestors(ownerOrgUnit?.id); - const orgUnitClientValue = { name: orgUnitName, ancestors }; - const ownerOrgUnitClientValue = { name: ownerOrgUnitName, ancestors: ownerAncestors }; + const orgUnitClientValue = { id: enrollment?.orgUnit, name: orgUnitName, ancestors }; + const ownerOrgUnitClientValue = { id: ownerOrgUnit?.id, name: ownerOrgUnitName, ancestors: ownerAncestors }; return (
diff --git a/src/core_modules/capture-core/components/WidgetProfile/hooks/getSubValueForTei.js b/src/core_modules/capture-core/components/WidgetProfile/hooks/getSubValueForTei.js index af3c5de5ec..18891f0578 100644 --- a/src/core_modules/capture-core/components/WidgetProfile/hooks/getSubValueForTei.js +++ b/src/core_modules/capture-core/components/WidgetProfile/hooks/getSubValueForTei.js @@ -2,6 +2,7 @@ import type { QuerySingleResource } from 'capture-core/utils/api'; import { dataElementTypes } from '../../../metaData'; import { FEATURES, hasAPISupportForFeature } from '../../../../capture-core-utils'; +import { getOrgUnitNames } from '../../../metadataRetrieval/orgUnitName'; type Attribute = { id: string, @@ -59,22 +60,9 @@ const getImageResourceSubvalue = async ({ attribute, minorServerVersion }: SubVa }; }; -const getOrganisationUnitSubvalue = async ({ attribute, querySingleResource }: SubValueFunctionParams) => { - const organisationUnit = await querySingleResource({ - resource: 'organisationUnits', - id: attribute.value, - params: { - fields: 'id,name,ancestors[displayName]', - }, - }); - - const orgUnitClientValue = { - id: organisationUnit.id, - name: organisationUnit.name, - ancestors: organisationUnit.ancestors.map(ancestor => ancestor.displayName), - }; - - return orgUnitClientValue; +const getOrganisationUnitSubvalue = async ({ attribute: { value }, querySingleResource }: SubValueFunctionParams) => { + const organisationUnits = await getOrgUnitNames([value], querySingleResource); + return organisationUnits[value]; }; export const subValueGetterByElementType = { diff --git a/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/StageDetail/hooks/helpers.js b/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/StageDetail/hooks/helpers.js index d4f2d6b2a6..74ae039f19 100644 --- a/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/StageDetail/hooks/helpers.js +++ b/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/StageDetail/hooks/helpers.js @@ -8,7 +8,7 @@ import type { StageDataElement } from '../../../../types/common.types'; import { Notes } from '../Notes.component'; import type { QuerySingleResource } from '../../../../../../utils/api/api.types'; import { isEventOverdue } from '../../../../../../utils/isEventOverdue'; -import { getCachedOrgUnitName } from '../../../../../../metadataRetrieval/orgUnitName'; +import { TooltipOrgUnit } from '../../../../../Tooltips/TooltipOrgUnit/TooltipOrgUnit.component'; const getEventStatus = (event: ApiEnrollmentEvent) => { const today = moment().startOf('day'); @@ -58,7 +58,7 @@ const convertStatusForView = (event: ApiEnrollmentEvent) => { }; }; -const convertOrgUnitForView = (event: ApiEnrollmentEvent) => getCachedOrgUnitName(event.orgUnit); +const convertOrgUnitForView = (event: ApiEnrollmentEvent) => ; const convertNoteForView = (event: ApiEnrollmentEvent) => ; diff --git a/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/StageDetail/hooks/useEventList.js b/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/StageDetail/hooks/useEventList.js index 2199216154..f160eb00e3 100644 --- a/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/StageDetail/hooks/useEventList.js +++ b/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/StageDetail/hooks/useEventList.js @@ -22,7 +22,7 @@ import { } from '../../../../../../metaDataMemoryStoreBuilders/common/helpers/dataElement/unsupportedMultiText'; import { useOrgUnitNames } from '../../../../../../metadataRetrieval/orgUnitName'; -const baseKeys = [{ id: 'status' }, { id: 'occurredAt' }, { id: 'assignedUser' }, { id: 'orgUnitName' }, { id: 'scheduledAt' }, { id: 'notes' }]; +const baseKeys = [{ id: 'status' }, { id: 'occurredAt' }, { id: 'assignedUser' }, { id: 'orgUnit' }, { id: 'scheduledAt' }, { id: 'notes' }]; const basedFieldTypes = [ { type: dataElementTypes.STATUS, resolveValue: convertStatusForView }, { type: dataElementTypes.DATE }, diff --git a/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/getEventDataWithSubValue.js b/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/getEventDataWithSubValue.js index a5493d19fd..d0aa4cc038 100644 --- a/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/getEventDataWithSubValue.js +++ b/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stage/getEventDataWithSubValue.js @@ -2,6 +2,7 @@ import { featureAvailable, FEATURES } from 'capture-core-utils'; import { dataElementTypes } from '../../../../metaData'; import type { QuerySingleResource } from '../../../../utils/api/api.types'; +import { getOrgUnitNames } from '../../../../metadataRetrieval/orgUnitName'; const getFileResourceSubvalue = async (keys: Object, querySingleResource: QuerySingleResource, eventId: string, absoluteApiPath: string) => { const promises = Object.keys(keys) @@ -57,19 +58,9 @@ const getImageSubvalue = (keys: Object, querySingleResource: QuerySingleResource ); const getOrganisationUnitSubvalue = async (keys: Object, querySingleResource: QuerySingleResource) => { - const ids = Object.values(keys) - .join(','); - - const { organisationUnits = [] } = await querySingleResource({ - resource: 'organisationUnits', - params: { filter: `id:in:[${ids}]` }, - }); - - return organisationUnits - .reduce((acc, { id, displayName: name }) => { - acc[id] = { id, name }; - return acc; - }, {}); + const ids = Object.values(keys).map(value => String(value)); + const orgUnitNames = await getOrgUnitNames(ids, querySingleResource); + return orgUnitNames; }; const subValueGetterByElementType = { diff --git a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/FlatListOrgUnitField/FlatListOrgUnitField.js b/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/FlatListOrgUnitField/FlatListOrgUnitField.js deleted file mode 100644 index aaf3ffdf3c..0000000000 --- a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/FlatListOrgUnitField/FlatListOrgUnitField.js +++ /dev/null @@ -1,19 +0,0 @@ -// @flow -import React from 'react'; -import { useOrgUnitNameWithAncestors } from '../../../metadataRetrieval/orgUnitName'; - -type Props = { - orgUnitId: string, -} - -export const FlatListOrgUnitField = ({ - orgUnitId, -}: Props) => { - const { displayName } = useOrgUnitNameWithAncestors(orgUnitId); - - return ( - - {displayName} - - ); -}; diff --git a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/FlatListOrgUnitField/index.js b/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/FlatListOrgUnitField/index.js deleted file mode 100644 index cf8e8c20b7..0000000000 --- a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/FlatListOrgUnitField/index.js +++ /dev/null @@ -1,3 +0,0 @@ -// @flow - -export { FlatListOrgUnitField } from './FlatListOrgUnitField'; diff --git a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/utils/getDataEntryDetails.js b/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/utils/getDataEntryDetails.js index 3e42d171de..a76d895a77 100644 --- a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/utils/getDataEntryDetails.js +++ b/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/utils/getDataEntryDetails.js @@ -5,7 +5,7 @@ import i18n from '@dhis2/d2-i18n'; import { dataElementTypes, type RenderFoundation } from '../../../metaData'; import { convertClientToView, convertServerToClient } from '../../../converters'; import type { LinkedEvent } from '../WidgetTwoEventWorkspace.types'; -import { FlatListOrgUnitField } from '../FlatListOrgUnitField'; +import { TooltipOrgUnit } from '../../Tooltips/TooltipOrgUnit'; const convertFn = pipe(convertServerToClient, convertClientToView); @@ -37,7 +37,7 @@ const DataEntryFieldsToInclude = { type: dataElementTypes.ORGANISATION_UNIT, placement: Placements.TOP, label: i18n.t('Organisation unit'), - convertFn: orgUnitId => , + convertFn: orgUnitId => , }, status: { apiKey: 'status', diff --git a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/utils/getSubValueForDataValue.js b/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/utils/getSubValueForDataValue.js index 5de5037e21..2a54bc5f8c 100644 --- a/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/utils/getSubValueForDataValue.js +++ b/src/core_modules/capture-core/components/WidgetTwoEventWorkspace/utils/getSubValueForDataValue.js @@ -2,6 +2,7 @@ import { dataElementTypes } from '../../../metaData'; import type { QuerySingleResource } from '../../../utils/api'; import { featureAvailable, FEATURES } from '../../../../capture-core-utils'; +import { getOrgUnitNames } from '../../../metadataRetrieval/orgUnitName'; type SubValueFunctionProps = { dataElement: Object, @@ -44,15 +45,9 @@ const getImageSubvalue = async ({ dataElement, querySingleResource, eventId, abs }; }; -const getOrganisationUnitSubvalue = async ({ dataElement, querySingleResource }: SubValueFunctionProps) => { - const organisationUnit = await querySingleResource({ - resource: 'organisationUnits', - id: dataElement.value, - params: { - fields: 'id,name', - }, - }); - return { ...organisationUnit }; +const getOrganisationUnitSubvalue = async ({ dataElement: { value }, querySingleResource }: SubValueFunctionProps) => { + const organisationUnits = await getOrgUnitNames([value], querySingleResource); + return organisationUnits[value]; }; export const subValueGetterByElementType = { diff --git a/src/core_modules/capture-core/converters/clientToList.js b/src/core_modules/capture-core/converters/clientToList.js index a5f39b4105..870d0d7981 100644 --- a/src/core_modules/capture-core/converters/clientToList.js +++ b/src/core_modules/capture-core/converters/clientToList.js @@ -8,6 +8,7 @@ import { dataElementTypes, type DataElement } from '../metaData'; import { convertMomentToDateFormatString } from '../utils/converters/date'; import { stringifyNumber } from './common/stringifyNumber'; import { MinimalCoordinates } from '../components/MinimalCoordinates'; +import { TooltipOrgUnit } from '../components/Tooltips/TooltipOrgUnit'; function convertDateForListDisplay(rawValue: string): string { const momentDate = moment(rawValue); @@ -37,6 +38,7 @@ type ImageClientValue = { previewUrl: string, }; + function convertFileForDisplay(clientValue: FileClientValue) { // Fallback until https://dhis2.atlassian.net/browse/DHIS2-16994 is implemented if (typeof clientValue === 'string' || clientValue instanceof String) { @@ -86,10 +88,13 @@ function convertStatusForDisplay(clientValue: Object) { ); } -function convertOrgUnitForDisplay(rawValue: string | Object) { - return (typeof rawValue === 'string' ? rawValue : rawValue.name); +function convertOrgUnitForDisplay(clientValue: { id: string }) { + return ( + + ); } + const valueConvertersForType = { [dataElementTypes.NUMBER]: stringifyNumber, [dataElementTypes.INTEGER]: stringifyNumber, diff --git a/src/core_modules/capture-core/converters/clientToView.js b/src/core_modules/capture-core/converters/clientToView.js index 115bcbeb69..2849a02b01 100644 --- a/src/core_modules/capture-core/converters/clientToView.js +++ b/src/core_modules/capture-core/converters/clientToView.js @@ -34,11 +34,6 @@ type ImageClientValue = { previewUrl: string, }; -type OrgUnitClientValue = { - name: string, - ancestors?: Array, - tooltip?: string, -}; function convertFileForDisplay(clientValue: FileClientValue) { return ( @@ -57,16 +52,13 @@ function convertImageForDisplay(clientValue: ImageClientValue) { return ; } -function convertOrgUnitForDisplay(clientValue: OrgUnitClientValue) { +function convertOrgUnitForDisplay(clientValue: { id: string }) { return ( - + ); } + const valueConvertersForType = { [dataElementTypes.NUMBER]: stringifyNumber, [dataElementTypes.INTEGER]: stringifyNumber, diff --git a/src/core_modules/capture-core/metadataRetrieval/orgUnitName/index.js b/src/core_modules/capture-core/metadataRetrieval/orgUnitName/index.js index 21e8b84212..204c983009 100644 --- a/src/core_modules/capture-core/metadataRetrieval/orgUnitName/index.js +++ b/src/core_modules/capture-core/metadataRetrieval/orgUnitName/index.js @@ -3,5 +3,4 @@ export { useOrgUnitNameWithAncestors, useOrgUnitNames, getOrgUnitNames, - getCachedOrgUnitName, } from './orgUnitName'; diff --git a/src/core_modules/capture-core/metadataRetrieval/orgUnitName/orgUnitName.js b/src/core_modules/capture-core/metadataRetrieval/orgUnitName/orgUnitName.js index 56d222c119..c86c4d7c14 100644 --- a/src/core_modules/capture-core/metadataRetrieval/orgUnitName/orgUnitName.js +++ b/src/core_modules/capture-core/metadataRetrieval/orgUnitName/orgUnitName.js @@ -209,5 +209,3 @@ export const useOrgUnitNameWithAncestors = (orgUnitId: ?string): { return { error }; }; - -export const getCachedOrgUnitName = (orgUnitId: string): ?string => displayNameCache[orgUnitId]?.displayName;