Skip to content

Commit

Permalink
fix: use dedicated component for DE operands
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardo committed Nov 21, 2024
1 parent 0b1df70 commit 4eb35ee
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 10 deletions.
13 changes: 11 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-19T10:22:29.647Z\n"
"PO-Revision-Date: 2024-11-19T10:22:29.648Z\n"
"POT-Creation-Date: 2024-11-21T09:05:21.858Z\n"
"PO-Revision-Date: 2024-11-21T09:05:21.859Z\n"

msgid "view only"
msgstr "view only"
Expand Down Expand Up @@ -204,6 +204,15 @@ msgstr "Group membership"
msgid "Legend set(s)"
msgstr "Legend set(s)"

msgid "Category option name"
msgstr "Category option name"

msgid "Category combo name"
msgstr "Category combo name"

msgid "Categories name"
msgstr "Categories name"

msgid "Period type"
msgstr "Period type"

Expand Down
232 changes: 232 additions & 0 deletions src/components/DataDimension/Info/DataElementOperandInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
import { useConfig, useDataEngine } from '@dhis2/app-runtime'
import PropTypes from 'prop-types'
import React, { useCallback, useEffect, useState } from 'react'
import i18n from '../../../locales/index.js'
import { capitalizeWord, getCommonFields, InfoTable } from './InfoTable.js'
import styles from './styles/InfoPopover.style.js'

const dataElementOperandsQuery = {
dataElementOperands: {
resource: 'dataElementOperands',
params: ({ displayNameProp, id }) => ({
filter: `id:eq:${id}`,
fields: [
`${getCommonFields(displayNameProp)}`,
'categoryOptionCombo[categoryCombo[categories[displayName,id],displayName],displayName]',
`dataElement[${getCommonFields(
displayNameProp
)},aggregationType,categoryCombo[displayName,categories[id,displayName]],dataElementGroups[id,displayName],dataSetElements[dataSet[id,displayName]],legendSets[id,displayName],optionSet[displayName],valueType,zeroIsSignificant]`,
'displayName,id',
],
}),
},
}

export const DataElementOperandInfo = ({ id, displayNameProp }) => {
const [data, setData] = useState()
const [error, setError] = useState()
const [loading, setLoading] = useState(true)

const { baseUrl, apiVersion } = useConfig()

const engine = useDataEngine()

const fetchData = useCallback(async () => {
const { dataElementOperands } = await engine.query(
dataElementOperandsQuery,
{
variables: { id, displayNameProp },
onError: setError,
}
)

const dataElementOperand = dataElementOperands.dataElementOperands[0]

// copy some common fields from dataElement
;[
'code',
'created',
'createdBy',
'displayDescription',
'lastUpdated',
].forEach(
(key) =>
(dataElementOperand[key] = dataElementOperand.dataElement[key])
)

// inject href as it is not returned from the API
dataElementOperand.href = new URL(
`${
dataElementOperandsQuery.dataElementOperands.resource
}?${new URLSearchParams({ filter: `id:eq:${id}` })}`,
new URL(`api/${apiVersion}/`, `${baseUrl}/`)
).href

setData({ dataElementOperand })
setLoading(false)
}, [displayNameProp, engine, id])

Check warning on line 67 in src/components/DataDimension/Info/DataElementOperandInfo.js

View workflow job for this annotation

GitHub Actions / unit

React Hook useCallback has missing dependencies: 'apiVersion' and 'baseUrl'. Either include them or remove the dependency array

useEffect(() => {
fetchData()
}, [fetchData])

return (
<>
<InfoTable
data={data?.dataElementOperand}
loading={loading}
error={error}
>
<tr>
<th>{i18n.t('Data set(s)')}</th>
<td>
{data?.dataElementOperand.dataElement.dataSetElements
.length === 1 ? (
data.dataElementOperand.dataElement
.dataSetElements[0].dataSet.displayName
) : (
<ul>
{data?.dataElementOperand.dataElement.dataSetElements.map(
({ dataSet }) => (
<li key={dataSet.id}>
{dataSet.displayName}
</li>
)
)}
</ul>
)}
</td>
</tr>
<tr>
<th>{i18n.t('Zero is significant')}</th>
<td>
{data?.dataElementOperand.dataElement.zeroIsSignificant
? i18n.t('True')
: i18n.t('False')}
</td>
</tr>
<tr>
<th>{i18n.t('Value type')}</th>
<td>
{capitalizeWord(
data?.dataElementOperand.dataElement.valueType
)}
</td>
</tr>
<tr>
<th>{i18n.t('Aggregation type')}</th>
<td>
{data?.dataElementOperand.dataElement.aggregationType}
</td>
</tr>
<tr>
<th>{i18n.t('Category combo')}</th>
<td>
<details>
<summary>
{
data?.dataElementOperand.dataElement
.categoryCombo.displayName
}
</summary>
<ul>
{data?.dataElementOperand.dataElement.categoryCombo.categories.map(
({ id, displayName }) => (
<li key={id}>{displayName}</li>
)
)}
</ul>
</details>
</td>
</tr>
{data?.dataElementOperand.dataElement.optionSet && (
<tr>
<td>{i18n.t('Option set')}</td>
<td>
{
data.dataElementOperand.dataElement.optionSet
.displayName
}
</td>
</tr>
)}
<tr>
<th>{i18n.t('Group membership')}</th>
<td>
{data?.dataElementOperand.dataElement.dataElementGroups
.length === 1 ? (
data.dataElementOperand.dataElement
.dataElementGroups[0].displayName
) : (
<ul>
{data?.dataElementOperand.dataElement.dataElementGroups.map(
({ id, displayName }) => (
<li key={id}>{displayName}</li>
)
)}
</ul>
)}
</td>
</tr>
{Boolean(
data?.dataElementOperand.dataElement.legendSets.length
) && (
<tr>
<th>{i18n.t('Legend set(s)')}</th>
<td>
{data.dataElementOperand.dataElement.legendSets
.length === 1 ? (
data.dataElementOperand.dataElement
.legendSets[0].displayName
) : (
<ul>
{data.dataElementOperand.dataElement.legendSets.map(
({ id, displayName }) => (
<li key={id}>{displayName}</li>
)
)}
</ul>
)}
</td>
</tr>
)}
<tr>
<th>{i18n.t('Category option name')}</th>
<td>
{
data?.dataElementOperand.categoryOptionCombo
.displayName
}
</td>
</tr>
<tr>
<th>{i18n.t('Category combo name')}</th>
<td>
{
data?.dataElementOperand.categoryOptionCombo
.categoryCombo.displayName
}
</td>
</tr>
<tr>
<th>{i18n.t('Categories name')}</th>
<td>
<ul>
{data?.dataElementOperand.categoryOptionCombo.categoryCombo.categories.map(
({ id, displayName }) => (
<li key={id}>{displayName}</li>
)
)}
</ul>
</td>
</tr>
</InfoTable>
<style jsx>{styles}</style>
</>
)
}

DataElementOperandInfo.propTypes = {
displayNameProp: PropTypes.string,
id: PropTypes.string,
}
18 changes: 10 additions & 8 deletions src/components/DataDimension/Info/InfoPopover.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Popover } from '@dhis2/ui'
import PropTypes from 'prop-types'
import React from 'react'
import { REPORTING_RATE } from '../../../modules/dataSets.js'
import { REPORTING_RATE } from '../../../modules/dataSets.js' // data sets
import {
DIMENSION_TYPE_DATA_ELEMENT,
DIMENSION_TYPE_DATA_ELEMENT_OPERAND,
//DIMENSION_TYPE_DATA_SET,
DIMENSION_TYPE_EXPRESSION_DIMENSION_ITEM, // calculation
DIMENSION_TYPE_INDICATOR,
DIMENSION_TYPE_PROGRAM_ATTRIBUTE, // event data item
DIMENSION_TYPE_PROGRAM_DATA_ELEMENT, // event data item
DIMENSION_TYPE_PROGRAM_ATTRIBUTE, // event data items
DIMENSION_TYPE_PROGRAM_DATA_ELEMENT, // event data items
DIMENSION_TYPE_PROGRAM_INDICATOR,
} from '../../../modules/dataTypes.js'
import { CalculationInfo } from './CalculationInfo.js'
import { DataElementInfo } from './DataElementInfo.js'
import { DataElementOperandInfo } from './DataElementOperandInfo.js'
import { DataSetInfo } from './DataSetInfo.js'
import { EventDataItemInfo } from './EventDataItemInfo.js'
import { IndicatorInfo } from './IndicatorInfo.js'
Expand All @@ -40,10 +40,12 @@ export const InfoPopover = ({ reference, onClose, ...props }) => {
elevation="rgba(0, 0, 0, 0.1) 0px 1px 5px, rgba(0, 0, 0, 0.07) 0px 3.6px 13px, rgba(0, 0, 0, 0.06) 0px 8.4px 23px, rgba(0, 0, 0, 0.05) 0px 23px 35px"
>
<div className="popover">
{[
DIMENSION_TYPE_DATA_ELEMENT,
DIMENSION_TYPE_DATA_ELEMENT_OPERAND,
].includes(type) && <DataElementInfo {...infoProps} />}
{type === DIMENSION_TYPE_DATA_ELEMENT && (
<DataElementInfo {...infoProps} />
)}
{type === DIMENSION_TYPE_DATA_ELEMENT_OPERAND && (
<DataElementOperandInfo {...infoProps} />
)}
{type === DIMENSION_TYPE_EXPRESSION_DIMENSION_ITEM && (
<CalculationInfo {...infoProps} />
)}
Expand Down

0 comments on commit 4eb35ee

Please sign in to comment.