Skip to content

Commit

Permalink
code cleanup: use arena-core node value formatter (#3077)
Browse files Browse the repository at this point in the history
* code cleanup: use arena-core node value formatter

* use latest version of arena-core

* avoid throwing errors when node defs are missing

* fixing tests

* fixing tests

---------

Co-authored-by: Stefano Ricci <[email protected]>
  • Loading branch information
SteRiccio and SteRiccio authored Oct 17, 2023
1 parent 60b3cf6 commit 4f1c88d
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 47 deletions.
3 changes: 2 additions & 1 deletion core/survey/_survey/surveyNodeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const getNodeDefByUuid = (uuid) => R.pipe(getNodeDefs, R.propOr(null, uui
export const getNodeDefsByUuids =
(uuids = []) =>
(survey) =>
Surveys.getNodeDefsByUuids({ survey, uuids })
// do not throw error if node defs are missing
Surveys.findNodeDefsByUuids({ survey, uuids })

export const getNodeDefSource = (nodeDef) =>
NodeDef.isVirtual(nodeDef) ? getNodeDefByUuid(NodeDef.getParentUuid(nodeDef)) : null
Expand Down
8 changes: 4 additions & 4 deletions core/survey/categoryItem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as R from 'ramda'
import { uuidv4 } from '@core/uuid'

import { CategoryItems } from '@openforis/arena-core'

import { uuidv4 } from '@core/uuid'
import * as ObjectUtils from '@core/objectUtils'

export const keys = {
Expand Down Expand Up @@ -34,9 +36,7 @@ export const getLevelUuid = R.prop(keys.levelUuid)
export const getParentUuid = R.prop(keys.parentUuid)
export const getCode = ObjectUtils.getProp(keysProps.code, '')
export const getLabel = (language) => (item) => ObjectUtils.getLabel(language, getCode(item))(item)

export const getLabelWithCode = (language) => (item) =>
`(${getCode(item)}) ${ObjectUtils.getLabel(language, getCode(item))(item)}`
export const getLabelWithCode = (language) => (item) => CategoryItems.getLabelWithCode(item, language)

// ====== READ - Extra Props
export const getExtra = ObjectUtils.getProp(keysProps.extra)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"@mui/material": "^5.14.10",
"@mui/x-data-grid": "^6.14.0",
"@mui/x-date-pickers": "^6.14.0",
"@openforis/arena-core": "^0.0.171",
"@openforis/arena-core": "^0.0.172",
"@openforis/arena-server": "^0.1.29",
"@sendgrid/mail": "^7.7.0",
"@shopify/draggable": "^1.0.0-beta.8",
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/mock/recordPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export const recordPreview = {
},
[cluster_boolean.name]: 'false',
[cluster_coordinate.name]: { x: '342.432', y: '3424.231', srs: '4326', srsLabel: 'WGS 1984 (EPSG:4326)' },
[cluster_country.name]: '(0) country 0',
[cluster_region.name]: '(01) region 01',
[cluster_province.name]: '(012) province 012',
[cluster_country.name]: 'country 0 (0)',
[cluster_region.name]: 'region 01 (01)',
[cluster_province.name]: 'province 012 (012)',
// plot
[plot_id.name]: '1',
[plot_text.name]: 'This is a plot text',
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/mock/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ const _createRecord = (idx) => {
srs: '4326',
srsLabel: 'WGS 1984 (EPSG:4326)',
},
[cluster_country.name]: '(0) country 0',
[cluster_region.name]: `(${clusterRegion}) region ${clusterRegion}`,
[cluster_province.name]: `(${clusterProvince}) province ${clusterProvince}`,
[cluster_country.name]: 'country 0 (0)',
[cluster_region.name]: `region ${clusterRegion} (${clusterRegion})`,
[cluster_province.name]: `province ${clusterProvince} (${clusterProvince})`,
// plot
[plot_id.name]: getRandomInRange(1, 10),
[plot_text.name]: 'This is a plot text',
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/tests/exportCsvData.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const { plot_id, plot_text } = plot.children
const { tree_dec_1, tree_dec_2, tree_species } = tree.children

const getCodeAndLabel = (value) => {
const code = value.match(new RegExp(/\([0-9]+\)/))[0].replace(/\D/g, '')
const label = value.replace(/\([0-9]+\) /, '')
const code = value.match(new RegExp(/\([0-9]+\)/))[0].replace(/\D/g, '') // extract code in parenthesis
const label = value.replace(/ \([0-9]+\)/, '') // remove " (code)" suffix
return { code, label }
}
export default () =>
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/tests/surveyFormPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default () =>
verifyAttribute(cluster_id, '')
verifyAttribute(cluster_boolean, 'true')
verifyAttribute(cluster_decimal, '')
verifyAttribute(cluster_country, '(0) country 0')
verifyAttribute(cluster_country, 'country 0 (0)')
verifyAttribute(cluster_region, '')
verifyAttribute(cluster_province, '')
verifyAttribute(cluster_coordinate, { x: '', y: '', srs: '4326', srsLabel: 'WGS 1984 (EPSG:4326)' })
Expand Down
40 changes: 12 additions & 28 deletions webapp/components/survey/SurveyForm/FormHeader/usePath.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
import * as StringUtils from '@core/stringUtils'
import { NodeValueFormatter } from '@openforis/arena-core'

import * as Survey from '@core/survey/survey'
import * as NodeDef from '@core/survey/nodeDef'
import * as NodeDefLayout from '@core/survey/nodeDefLayout'
import * as CategoryItem from '@core/survey/categoryItem'
import * as Taxon from '@core/survey/taxon'
import * as Record from '@core/record/record'
import * as Node from '@core/record/node'
import * as NodeRefData from '@core/record/nodeRefData'

import { useSurvey, useSurveyCycleKey, useSurveyPreferredLang } from '@webapp/store/survey'
import { useNodeDefLabelType, useNodeDefPage, usePagesUuidMap } from '@webapp/store/ui/surveyForm'
import { useRecord } from '@webapp/store/ui/record'

const getNodeValue = ({ survey, cycle, nodeDef, node, lang }) => {
if (Node.isValueBlank(node)) return null

if (NodeDef.isCode(nodeDef)) {
const categoryItem =
NodeRefData.getCategoryItem(node) ?? Survey.getCategoryItemByUuid(Node.getCategoryItemUuid(node))(survey)
if (!categoryItem) return null

const result = NodeDefLayout.isCodeShown(cycle)(nodeDef)
? CategoryItem.getLabelWithCode(lang)(categoryItem)
: CategoryItem.getLabel(lang)(categoryItem)
return result ? StringUtils.quote(result) : CategoryItem.getCode(categoryItem)
}
if (NodeDef.isTaxon(nodeDef)) {
const taxon = NodeRefData.getTaxon(node)
return Taxon.getCode(taxon)
}
const value = Node.getValue(node, null)
if (NodeDef.isText(nodeDef)) {
return StringUtils.quote(value)
}
return value
}
const getNodeValue = ({ survey, cycle, nodeDef, node, lang }) =>
NodeValueFormatter.format({
survey,
cycle,
nodeDef,
node,
value: Node.getValue(node),
showLabel: true,
quoteLabels: true,
lang,
})

export const usePath = (entry) => {
const survey = useSurvey()
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2529,10 +2529,10 @@
proj4 "^2.9.0"
uuid "^9.0.0"

"@openforis/arena-core@^0.0.171":
version "0.0.171"
resolved "https://npm.pkg.github.com/download/@openforis/arena-core/0.0.171/64ef31c177d290163a9098a47bc3530580d64c92#64ef31c177d290163a9098a47bc3530580d64c92"
integrity sha512-viQztU9jesu07Q8rs4W6z3ZM3WLL6w70o+WrKKiElNWY9X9iz1OQGmDOIWWTqALcxlXipUHqSmtLotKTKU7TIQ==
"@openforis/arena-core@^0.0.172":
version "0.0.172"
resolved "https://npm.pkg.github.com/download/@openforis/arena-core/0.0.172/cf0628bd5617bc0985ef4016fa6bfbe90caefb40#cf0628bd5617bc0985ef4016fa6bfbe90caefb40"
integrity sha512-jt+w5jfHIU5jle5butL6O1NFZKKFn5hIAGGn3ICpwf9Q79AAZoIt5+7KBjZ8VWXGsXQgiiskpa9oeDwGz7vflA==
dependencies:
"@jsep-plugin/regex" "^1.0.3"
bignumber.js "^9.1.2"
Expand Down

0 comments on commit 4f1c88d

Please sign in to comment.