From 3ef5c4f23d473ab84bf7c5c1c9b55b5ec0a2ef01 Mon Sep 17 00:00:00 2001 From: Edoardo Sabadelli Date: Tue, 26 Nov 2024 16:43:33 +0100 Subject: [PATCH] fix: format custom expressions for period boundaries --- i18n/en.pot | 7 +- .../Info/ProgramIndicatorInfo.js | 90 ++++++++++++++++--- 2 files changed, 81 insertions(+), 16 deletions(-) diff --git a/i18n/en.pot b/i18n/en.pot index dd259f632..3211b5b00 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -5,8 +5,8 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2024-11-22T11:00:48.850Z\n" -"PO-Revision-Date: 2024-11-22T11:00:48.851Z\n" +"POT-Creation-Date: 2024-11-26T15:37:51.441Z\n" +"PO-Revision-Date: 2024-11-26T15:37:51.442Z\n" msgid "view only" msgstr "view only" @@ -288,6 +288,9 @@ msgstr "Open in API" msgid "Custom attributes" msgstr "Custom attributes" +msgid "Event in {{ stageName }}" +msgstr "Event in {{ stageName }}" + msgid "Program" msgstr "Program" diff --git a/src/components/DataDimension/Info/ProgramIndicatorInfo.js b/src/components/DataDimension/Info/ProgramIndicatorInfo.js index 4dc9988ef..f4e44f3f0 100644 --- a/src/components/DataDimension/Info/ProgramIndicatorInfo.js +++ b/src/components/DataDimension/Info/ProgramIndicatorInfo.js @@ -6,6 +6,16 @@ import i18n from '../../../locales/index.js' import { getCommonFields, sentenceCaseText, InfoTable } from './InfoTable.js' import styles from './styles/InfoPopover.style.js' +const dataElementQuery = { + dataElement: { + resource: 'dataElements', + id: ({ id }) => id, + params: ({ displayNameProp }) => ({ + fields: `${displayNameProp}~rename(displayName)`, + }), + }, +} + const programIndicatorQuery = { programIndicator: { resource: 'programIndicators', @@ -13,7 +23,17 @@ const programIndicatorQuery = { params: ({ displayNameProp }) => ({ fields: `${getCommonFields( displayNameProp - )},aggregationType,analyticsPeriodBoundaries[analyticsPeriodBoundaryType,boundaryTarget,id,offsetPeriodType,offsetPeriods],analyticsType,decimals,expression,filter,legendSets[id,displayName],program[displayName]`, + )},aggregationType,analyticsPeriodBoundaries[analyticsPeriodBoundaryType,boundaryTarget,id,offsetPeriodType,offsetPeriods],analyticsType,decimals,expression,filter,legendSets[id,displayName],program[displayName,programStages[id,displayName]]`, + }), + }, +} + +const trackedEntityAttributeQuery = { + trackedEntityAttribute: { + resource: 'trackedEntityAttributes', + id: ({ id }) => id, + params: ({ displayNameProp }) => ({ + fields: `${displayNameProp}~rename(displayName)`, }), }, } @@ -57,6 +77,60 @@ export const ProgramIndicatorInfo = ({ id, displayNameProp }) => { } } + // this loop need to work with await (forEach does not) + for ( + let i = 0; + i < programIndicator.analyticsPeriodBoundaries.length; + i++ + ) { + const { boundaryTarget } = + programIndicator.analyticsPeriodBoundaries[i] + + let match + let formattedBoundaryTarget = boundaryTarget + + if ( + ['ENROLLMENT_DATE', 'EVENT_DATE', 'INCIDENT_DATE'].includes( + boundaryTarget + ) + ) { + formattedBoundaryTarget = sentenceCaseText(boundaryTarget) + } else if ((match = boundaryTarget.match(/^PS_EVENTDATE:(\w+)$/))) { + console.log('PS_EVENTDATE', match[1]) + formattedBoundaryTarget = i18n.t('Event in {{ stageName }}', { + stageName: programIndicator.program.programStages.find( + ({ id }) => id === match[1] + ).displayName, + }) + } else if ((match = boundaryTarget.match(/^A{(\w+)}$/))) { + console.log('A', match[1]) + const { trackedEntityAttribute } = await engine.query( + trackedEntityAttributeQuery, + { + variables: { id: match[1], displayNameProp }, + onError: setError, + } + ) + formattedBoundaryTarget = trackedEntityAttribute.displayName + } else if ((match = boundaryTarget.match(/^#{(\w+)\.(\w+)}$/))) { + console.log('id', match[1], match[2]) + const { dataElement } = await engine.query(dataElementQuery, { + variables: { id: match[2], displayNameProp }, + onError: setError, + }) + formattedBoundaryTarget = `${ + programIndicator.program.programStages.find( + ({ id }) => id === match[1] + ).displayName + }, ${dataElement.displayName}` + } + + console.log('formatted', formattedBoundaryTarget) + + programIndicator.analyticsPeriodBoundaries[i].boundaryTarget = + formattedBoundaryTarget + } + setData({ programIndicator }) setLoading(false) }, [displayNameProp, engine, id, getHumanReadableExpression]) @@ -65,16 +139,6 @@ export const ProgramIndicatorInfo = ({ id, displayNameProp }) => { fetchData() }, [fetchData]) - const formatBoundaryTarget = (target) => { - if ( - ['ENROLLMENT_DATE', 'EVENT_DATE', 'INCIDENT_DATE'].includes(target) - ) { - return sentenceCaseText(target) - } - - return target - } - return ( <> { {i18n.t('Target:')}  - {formatBoundaryTarget( - boundaryTarget - )} + {boundaryTarget} {Boolean(offsetPeriods) && Boolean(offsetPeriodType) && (