Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/data-workspace/custom-form/custom-form-indicator-cell.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import PropTypes from 'prop-types'
import React from 'react'
import { IndicatorTableCell } from '../indicators-table-body/indicator-table-cell.jsx'

export const CustomFormIndicatorCell = ({ indicatorId, metadata }) => {
const {
denominator,
numerator,
indicatorType: { factor },
decimals,
} = metadata.indicators[indicatorId]

return (
<IndicatorTableCell
denominator={denominator}
numerator={numerator}
factor={factor}
decimals={decimals}
/>
)
}
CustomFormIndicatorCell.propTypes = {
indicatorId: PropTypes.string.isRequired,
metadata: PropTypes.object.isRequired,
}

export const replaceIndicatorCell = (indicatorId, metadata) => (
<CustomFormIndicatorCell indicatorId={indicatorId} metadata={metadata} />
)
7 changes: 5 additions & 2 deletions src/data-workspace/custom-form/custom-form-total-cell.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from 'prop-types'
import React, { useState, useEffect } from 'react'
import React, { useMemo, useState, useEffect } from 'react'
import { useValueStore } from '../../shared/index.js'
import { TotalCell } from '../category-combo-table-body/total-cells.jsx'

Expand All @@ -18,7 +18,10 @@ const computeTotalValues = (dataElementValues) => {

export const CustomFormTotalCell = ({ dataElementId }) => {
const dataValues = useValueStore((state) => state.getDataValues())
const dataElementValues = dataValues?.[dataElementId] || {}
const dataElementValues = useMemo(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not related, but is in the same directory, and the linter pointed this error out to me (this just guarantees the object reference is stable, but if it's defined, it wouldn't be an issue)

() => dataValues?.[dataElementId] || {},
[dataValues, dataElementId]
)

const [total, setTotal] = useState(() =>
computeTotalValues(dataElementValues)
Expand Down
45 changes: 45 additions & 0 deletions src/data-workspace/custom-form/parse-html-to-react.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import { DataEntryField } from '../data-entry-cell/index.js'
import { CustomFormIndicatorCell } from './custom-form-indicator-cell.jsx'
import { CustomFormTotalCell } from './custom-form-total-cell.jsx'
import { parseHtmlToReact } from './parse-html-to-react.jsx'

Expand All @@ -24,6 +25,50 @@ describe('parseHtmlToReact', () => {
</div>
)
})
it('replaces indicator cells inside td elements', () => {
const htmlCode =
"<td><input id='indicatorRANDOMuid01' name='indicator' indicatorid='RANDOMuid01'/></td>"
const metadata = {
indicators: {
RANDOMuid01: {
indicatorType: { factor: 1 },
numerator: 'numerator',
denominator: 'denominator',
decimals: 2,
},
},
}
const result = parseHtmlToReact(htmlCode, metadata)
expect(result).toEqual(
<CustomFormIndicatorCell
indicatorId="RANDOMuid01"
metadata={metadata}
/>
)
})
it('replaces indicator cells outside of elements', () => {
const htmlCode =
"<div><input id='indicatorRANDOMuid01' name='indicator' indicatorid='RANDOMuid01'/></div>"
const metadata = {
indicators: {
RANDOMuid01: {
indicatorType: { factor: 1 },
numerator: 'numerator',
denominator: 'denominator',
decimals: 2,
},
},
}
const result = parseHtmlToReact(htmlCode, metadata)
expect(result).toEqual(
<div>
<CustomFormIndicatorCell
indicatorId="RANDOMuid01"
metadata={metadata}
/>
</div>
)
})

it('uses disabled entry field when input has disabled attribute', () => {
const htmlCode =
Expand Down
5 changes: 5 additions & 0 deletions src/data-workspace/custom-form/replace-input-node.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import { selectors } from '../../shared/index.js'
import { DataEntryField } from '../data-entry-cell/index.js'
import { replaceIndicatorCell } from './custom-form-indicator-cell.jsx'
import { replaceTotalCell } from './custom-form-total-cell.jsx'

const INPUT_TYPES = {
Expand Down Expand Up @@ -34,6 +35,10 @@ export const replaceInputNode = (domNode, metadata) => {
return replaceTotalCell(domNode.attribs.dataelementid)
}

if (inputType === INPUT_TYPES.INDICATOR) {
return replaceIndicatorCell(domNode.attribs.indicatorid, metadata)
}

if (inputType !== INPUT_TYPES.ENTRYFIELD) {
return undefined
}
Expand Down
20 changes: 1 addition & 19 deletions src/data-workspace/custom-form/replace-td-node.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
import { attributesToProps } from 'html-react-parser'
import React from 'react'
import { IndicatorTableCell } from '../indicators-table-body/indicator-table-cell.jsx'
import { replaceIndicatorCell } from './custom-form-indicator-cell.jsx'
import { replaceTotalCell } from './custom-form-total-cell.jsx'

const replaceIndicatorCell = (indicatorId, metadata) => {
const {
denominator,
numerator,
indicatorType: { factor },
decimals,
} = metadata.indicators[indicatorId]

return (
<IndicatorTableCell
denominator={denominator}
numerator={numerator}
factor={factor}
decimals={decimals}
/>
)
}

const replaceTextCell = (domNode) => {
const cleanedText = domNode.children[0].nodeValue.trim()
const props = attributesToProps(domNode.attribs)
Expand Down
Loading