From 4f1c88d290a908402b07d90285f20f48da5838f1 Mon Sep 17 00:00:00 2001 From: Stefano Ricci <1219739+SteRiccio@users.noreply.github.com> Date: Tue, 17 Oct 2023 14:56:42 +0200 Subject: [PATCH] code cleanup: use arena-core node value formatter (#3077) * 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 --- core/survey/_survey/surveyNodeDefs.js | 3 +- core/survey/categoryItem.js | 8 ++-- package.json | 2 +- test/e2e/mock/recordPreview.js | 6 +-- test/e2e/mock/records.js | 6 +-- test/e2e/tests/exportCsvData.js | 4 +- test/e2e/tests/surveyFormPreview.js | 2 +- .../survey/SurveyForm/FormHeader/usePath.js | 40 ++++++------------- yarn.lock | 8 ++-- 9 files changed, 32 insertions(+), 47 deletions(-) diff --git a/core/survey/_survey/surveyNodeDefs.js b/core/survey/_survey/surveyNodeDefs.js index 46ef229ee7..14563f3d0a 100644 --- a/core/survey/_survey/surveyNodeDefs.js +++ b/core/survey/_survey/surveyNodeDefs.js @@ -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 diff --git a/core/survey/categoryItem.js b/core/survey/categoryItem.js index 7750cb3aa3..b9a3e6c826 100644 --- a/core/survey/categoryItem.js +++ b/core/survey/categoryItem.js @@ -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 = { @@ -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) diff --git a/package.json b/package.json index e4779295bb..c0bba2e5b7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/e2e/mock/recordPreview.js b/test/e2e/mock/recordPreview.js index 214fcc28ff..f650f935d9 100644 --- a/test/e2e/mock/recordPreview.js +++ b/test/e2e/mock/recordPreview.js @@ -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', diff --git a/test/e2e/mock/records.js b/test/e2e/mock/records.js index 3a0f787903..e513b684d1 100644 --- a/test/e2e/mock/records.js +++ b/test/e2e/mock/records.js @@ -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', diff --git a/test/e2e/tests/exportCsvData.js b/test/e2e/tests/exportCsvData.js index 842327793c..bc79df8880 100644 --- a/test/e2e/tests/exportCsvData.js +++ b/test/e2e/tests/exportCsvData.js @@ -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 () => diff --git a/test/e2e/tests/surveyFormPreview.js b/test/e2e/tests/surveyFormPreview.js index eab533ef3e..cde3bd0e4c 100644 --- a/test/e2e/tests/surveyFormPreview.js +++ b/test/e2e/tests/surveyFormPreview.js @@ -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)' }) diff --git a/webapp/components/survey/SurveyForm/FormHeader/usePath.js b/webapp/components/survey/SurveyForm/FormHeader/usePath.js index 2c525ecfcb..f714266c97 100644 --- a/webapp/components/survey/SurveyForm/FormHeader/usePath.js +++ b/webapp/components/survey/SurveyForm/FormHeader/usePath.js @@ -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() diff --git a/yarn.lock b/yarn.lock index 6527d791be..1836094c5c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"