Skip to content

Commit

Permalink
fix: format custom expressions for period boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardo committed Nov 26, 2024
1 parent ad2e2a5 commit 3ef5c4f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
7 changes: 5 additions & 2 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down
90 changes: 76 additions & 14 deletions src/components/DataDimension/Info/ProgramIndicatorInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,34 @@ 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',
id: ({ id }) => id,
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)`,
}),
},
}
Expand Down Expand Up @@ -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])
Expand All @@ -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 (
<>
<InfoTable
Expand Down Expand Up @@ -121,9 +185,7 @@ export const ProgramIndicatorInfo = ({ id, displayNameProp }) => {
<span className="label">
{i18n.t('Target:')}&nbsp;
</span>
{formatBoundaryTarget(
boundaryTarget
)}
{boundaryTarget}
</span>
{Boolean(offsetPeriods) &&
Boolean(offsetPeriodType) && (
Expand Down

0 comments on commit 3ef5c4f

Please sign in to comment.