From f686ce835e4aaf913cb0e53b2c62ff6c85df1f48 Mon Sep 17 00:00:00 2001 From: Stefano Ricci <1219739+SteRiccio@users.noreply.github.com> Date: Fri, 22 Nov 2024 10:03:52 +0100 Subject: [PATCH] use latest arena-core version (survey node defs index by name) (#3664) * use latest arena-core version (survey node defs index by name) * fixed tests * prepare use of latest arena-core * use latest arena-core --------- Co-authored-by: Stefano Ricci --- core/survey/_survey/surveyNodeDefs.js | 22 +++++++++---------- core/survey/_survey/surveyNodeDefsIndex.js | 18 +++++++++++---- core/survey/survey.js | 11 +++++++--- package.json | 2 +- webapp/components/expression/nodes/literal.js | 20 ++++++++++++----- .../store/actions/useSetProp.js | 2 +- .../store/actions/useValidate.js | 4 ++-- webapp/store/survey/nodeDefs/actions.js | 7 +++++- webapp/store/survey/nodeDefsIndex/reducer.js | 19 ++++++++++++---- yarn.lock | 8 +++---- 10 files changed, 75 insertions(+), 38 deletions(-) diff --git a/core/survey/_survey/surveyNodeDefs.js b/core/survey/_survey/surveyNodeDefs.js index 5b47a1c0d8..e933ae3101 100644 --- a/core/survey/_survey/surveyNodeDefs.js +++ b/core/survey/_survey/surveyNodeDefs.js @@ -136,11 +136,13 @@ export const hasNodeDefChildrenEntities = (nodeDef) => (survey) => { return R.pipe(getNodeDefChildren(nodeDef), R.any(NodeDef.isEntity))(survey) } -export const getNodeDefChildByName = (nodeDef, childName) => - R.pipe( - getNodeDefChildren(nodeDef), - R.find((childDef) => childName === NodeDef.getName(childDef)) - ) +export const getNodeDefChildByName = (nodeDef, childName) => (survey) => + SurveyNodeDefsIndex.hasNodeDefsIndexByName(survey) + ? Surveys.getNodeDefByName({ survey, name: childName }) + : R.pipe( + getNodeDefChildren(nodeDef), + R.find((childDef) => childName === NodeDef.getName(childDef)) + )(survey) export const getNodeDefParent = (nodeDef) => (survey) => { if (NodeDef.isRoot(nodeDef)) return null @@ -154,11 +156,6 @@ export const getNodeDefAreaBasedEstimate = (nodeDef) => (survey) => export const getAreaBasedEstimatedOfNodeDef = (nodeDef) => (survey) => getNodeDefByUuid(NodeDef.getAreaBasedEstimatedOf(nodeDef))(survey) -export const getNodeDefSiblingByName = (nodeDef, name) => (survey) => { - const parentDef = getNodeDefParent(nodeDef)(survey) - return getNodeDefChildByName(parentDef, name)(survey) -} - const _nodeDefKeysFilter = (n) => NodeDef.isKey(n) && !NodeDef.isDeleted(n) export const getNodeDefKeys = (nodeDef) => (survey) => getNodeDefChildren(nodeDef)(survey).filter(_nodeDefKeysFilter) @@ -183,8 +180,7 @@ export const getNodeDefsRootUnique = (survey) => { ) } -export const getNodeDefByName = (name) => - R.pipe(getNodeDefsArray, R.find(R.pathEq([NodeDef.keys.props, NodeDef.propKeys.name], name))) +export const getNodeDefByName = (name) => (survey) => Surveys.getNodeDefByName({ survey, name }) export const getNodeDefsByCategoryUuid = (uuid) => R.pipe(getNodeDefsArray, R.filter(R.pathEq([NodeDef.keys.props, NodeDef.propKeys.categoryUuid], uuid))) @@ -194,6 +190,8 @@ export const getNodeDefsByTaxonomyUuid = (uuid) => export const findNodeDef = (predicate) => R.pipe(getNodeDefsArray, R.find(predicate)) +export const findNodeDefByName = (name) => (survey) => Surveys.findNodeDefByName({ survey, name }) + export const getNodeDefMaxDecimalDigits = (nodeDef) => (survey) => { const referenceNodeDef = survey && NodeDef.isAreaBasedEstimatedOf(nodeDef) ? getAreaBasedEstimatedOfNodeDef(nodeDef)(survey) : nodeDef diff --git a/core/survey/_survey/surveyNodeDefsIndex.js b/core/survey/_survey/surveyNodeDefsIndex.js index d1a6827a69..059de8e079 100644 --- a/core/survey/_survey/surveyNodeDefsIndex.js +++ b/core/survey/_survey/surveyNodeDefsIndex.js @@ -1,4 +1,4 @@ -import { Surveys } from '@openforis/arena-core' +import { Objects, Surveys } from '@openforis/arena-core' // ==== READ @@ -7,13 +7,23 @@ export const getNodeDefsIndex = (survey) => { return nodeDefsIndex || {} } +export const hasNodeDefsIndexByName = (survey) => { + const { nodeDefUuidByName } = getNodeDefsIndex(survey) + return Objects.isNotEmpty(nodeDefUuidByName) +} + export const assocNodeDefsIndex = ({ survey, nodeDefsIndex }) => ({ ...survey, nodeDefsIndex }) // ==== UPDATE export const addNodeDefToIndex = ({ nodeDefsIndex, nodeDef }) => { const surveyUpdated = Surveys.addNodeDefToIndex(nodeDef)({ nodeDefsIndex }) - return surveyUpdated.nodeDefsIndex + return getNodeDefsIndex(surveyUpdated) +} + +export const updateNodeDefUuidByNameIndex = ({ nodeDefsIndex, nodeDef, nodeDefPrevious }) => { + const surveyUpdated = Surveys.updateNodeDefUuidByNameIndex(nodeDef, nodeDefPrevious)({ nodeDefsIndex }) + return getNodeDefsIndex(surveyUpdated) } // ==== DELETE @@ -21,14 +31,14 @@ export const addNodeDefToIndex = ({ nodeDefsIndex, nodeDef }) => { export const deleteNodeDefIndex = ({ nodeDefsIndex, nodeDef }) => { const survey = { nodeDefsIndex } const surveyUpdated = Surveys.deleteNodeDefIndex(nodeDef)(survey) - return surveyUpdated.nodeDefsIndex + return getNodeDefsIndex(surveyUpdated) } // ==== CREATE export const initNodeDefsIndex = (survey) => { const surveyUpdated = Surveys.buildAndAssocNodeDefsIndex(survey) - return surveyUpdated.nodeDefsIndex + return getNodeDefsIndex(surveyUpdated) } export const initAndAssocNodeDefsIndex = (survey) => Surveys.buildAndAssocNodeDefsIndex(survey) diff --git a/core/survey/survey.js b/core/survey/survey.js index 4dd9c43b2b..37dc7a2717 100644 --- a/core/survey/survey.js +++ b/core/survey/survey.js @@ -163,7 +163,6 @@ export const { hasNodeDefChildrenEntities, getNodeDefChildrenSorted, getNodeDefChildByName, - getNodeDefSiblingByName, getNodeDefByName, getNodeDefsByCategoryUuid, getNodeDefsByTaxonomyUuid, @@ -173,6 +172,7 @@ export const { getNodeDefKeysSorted, isNodeDefRootKey, findNodeDef, + findNodeDefByName, getNodeDefAreaBasedEstimate, getAreaBasedEstimatedOfNodeDef, getNodeDefMaxDecimalDigits, @@ -249,8 +249,13 @@ export const { assocDependencyGraph } = SurveyDependencies export const buildDependencyGraph = SurveyDependencies.buildGraph export const { buildAndAssocDependencyGraph } = SurveyDependencies -export const { addNodeDefToIndex, deleteNodeDefIndex, initNodeDefsIndex, initAndAssocNodeDefsIndex } = - SurveyNodeDefsIndex +export const { + addNodeDefToIndex, + updateNodeDefUuidByNameIndex, + deleteNodeDefIndex, + initNodeDefsIndex, + initAndAssocNodeDefsIndex, +} = SurveyNodeDefsIndex // ====== NodeDefsValidation export const { getNodeDefsValidation, assocNodeDefsValidation, getNodeDefValidation } = SurveyNodeDefsValidation diff --git a/package.json b/package.json index 48a9744fc6..08e2b482fb 100644 --- a/package.json +++ b/package.json @@ -108,7 +108,7 @@ "@mui/x-data-grid": "^7.14.0", "@mui/x-date-pickers": "^7.14.0", "@mui/x-tree-view": "^7.7.1", - "@openforis/arena-core": "^1.0.2", + "@openforis/arena-core": "^1.0.4", "@openforis/arena-server": "^0.1.36", "@reduxjs/toolkit": "^2.2.5", "@sendgrid/mail": "^8.1.3", diff --git a/webapp/components/expression/nodes/literal.js b/webapp/components/expression/nodes/literal.js index 5fe136e35e..4595037dcc 100644 --- a/webapp/components/expression/nodes/literal.js +++ b/webapp/components/expression/nodes/literal.js @@ -23,6 +23,7 @@ import { TestId } from '@webapp/utils/testId' import { useAsyncGetRequest } from '../../hooks' import * as ExpressionParser from '../expressionParser' import { BinaryOperandType } from './binaryOperand' +import { Objects } from '@openforis/arena-core' const isValueText = (nodeDef, value) => nodeDef @@ -43,6 +44,18 @@ const loadItems = async (params) => { return items } +const _findNodeDefByName = ({ survey, name }) => { + if (Objects.isEmpty(name)) { + return null + } + const nodeDef = Survey.findNodeDefByName(name)(survey) + if (nodeDef) { + return nodeDef + } + const nameWithoutSubfix = name.replace('_label', '') + return Survey.findNodeDefByName(nameWithoutSubfix)(survey) +} + const _getNodeDef = ({ expressionNodeParent, nodeDefCurrent, survey, type }) => { if (!type || BinaryOperandType.isLeft(type)) { return nodeDefCurrent @@ -57,12 +70,7 @@ const _getNodeDef = ({ expressionNodeParent, nodeDefCurrent, survey, type }) => if (identifierName === Expression.thisVariable) { return nodeDefCurrent } - const nodeDef = Survey.getNodeDefByName(identifierName)(survey) - if (!nodeDef) { - const nameWithOutSubfix = identifierName.replace('_label', '') - return Survey.getNodeDefByName(nameWithOutSubfix)(survey) - } - return nodeDef + return _findNodeDefByName({ survey, name: identifierName }) } } return null diff --git a/webapp/components/survey/NodeDefDetails/store/actions/useSetProp.js b/webapp/components/survey/NodeDefDetails/store/actions/useSetProp.js index a11e85ed42..9d48025bd1 100644 --- a/webapp/components/survey/NodeDefDetails/store/actions/useSetProp.js +++ b/webapp/components/survey/NodeDefDetails/store/actions/useSetProp.js @@ -136,6 +136,6 @@ export const useSetProp = ({ setState }) => { nodeDefUpdated = NodeDef.clearNotApplicableProps(surveyCycleKey)(nodeDefUpdated) - validateNodeDef({ nodeDefUpdated }) + validateNodeDef({ nodeDef, nodeDefUpdated }) }, []) } diff --git a/webapp/components/survey/NodeDefDetails/store/actions/useValidate.js b/webapp/components/survey/NodeDefDetails/store/actions/useValidate.js index 0a05a224aa..43b3464875 100644 --- a/webapp/components/survey/NodeDefDetails/store/actions/useValidate.js +++ b/webapp/components/survey/NodeDefDetails/store/actions/useValidate.js @@ -13,7 +13,7 @@ export const useValidate = ({ setState }) => { const dispatch = useDispatch() const survey = useSelector(SurveyState.getSurvey) - return useCallback(async ({ nodeDefUpdated }) => { + return useCallback(async ({ nodeDef, nodeDefUpdated }) => { const surveyUpdated = Survey.assocNodeDef({ nodeDef: nodeDefUpdated })(survey) // Update local state immediately (see issue #3240) @@ -25,7 +25,7 @@ export const useValidate = ({ setState }) => { }) // // Dispatch update action - dispatch(NodeDefsActions.updateNodeDef({ nodeDef: nodeDefUpdated, dirty })) + dispatch(NodeDefsActions.updateNodeDef({ prevNodeDef: nodeDef, nodeDef: nodeDefUpdated, dirty })) // // Validate node def const validation = await SurveyValidator.validateNodeDef(surveyUpdated, nodeDefUpdated) diff --git a/webapp/store/survey/nodeDefs/actions.js b/webapp/store/survey/nodeDefs/actions.js index eafd2b7e5d..8b92093b5b 100644 --- a/webapp/store/survey/nodeDefs/actions.js +++ b/webapp/store/survey/nodeDefs/actions.js @@ -31,7 +31,12 @@ export const nodeDefsValidationUpdate = 'survey/nodeDefsValidation/update' export const nodeDefsUpdate = 'survey/nodeDefs/update' // ==== PLAIN ACTIONS -export const updateNodeDef = ({ nodeDef, dirty = false }) => ({ type: nodeDefUpdate, nodeDef, dirty }) +export const updateNodeDef = ({ nodeDef, prevNodeDef = null, dirty = false }) => ({ + type: nodeDefUpdate, + nodeDef, + prevNodeDef, + dirty, +}) export const saveNodeDef = ({ nodeDef, nodeDefParent, surveyCycleKey, nodeDefValidation }) => ({ type: nodeDefSave, diff --git a/webapp/store/survey/nodeDefsIndex/reducer.js b/webapp/store/survey/nodeDefsIndex/reducer.js index 01e36eaa98..08b5c4f778 100644 --- a/webapp/store/survey/nodeDefsIndex/reducer.js +++ b/webapp/store/survey/nodeDefsIndex/reducer.js @@ -23,11 +23,22 @@ const actionHandlers = { [NodeDefsActions.nodeDefCreate]: (state, { nodeDef }) => Survey.addNodeDefToIndex({ nodeDefsIndex: state, nodeDef }), [NodeDefsActions.nodeDefDelete]: (state, { nodeDef }) => Survey.deleteNodeDefIndex({ nodeDefsIndex: state, nodeDef }), - - [NodeDefsActions.nodeDefPropsUpdateCancel]: (state, { nodeDef, isNodeDefNew }) => + [NodeDefsActions.nodeDefUpdate]: (state, { nodeDef, prevNodeDef = null }) => + Survey.updateNodeDefUuidByNameIndex({ + nodeDefsIndex: state, + nodeDef, + nodeDefPrevious: prevNodeDef, + }), + + [NodeDefsActions.nodeDefPropsUpdateCancel]: (state, { nodeDef, nodeDefOriginal, isNodeDefNew }) => isNodeDefNew - ? Survey.deleteNodeDefIndex({ nodeDefsIndex: state, nodeDef }) // Remove node def from state - : state, + ? Survey.deleteNodeDefIndex({ nodeDefsIndex: state, nodeDef }) + : // restore node defs index using the original node def name + Survey.updateNodeDefUuidByNameIndex({ + nodeDefsIndex: state, + nodeDef: nodeDefOriginal, + nodeDefPrevious: nodeDef, + }), } export default exportReducer(actionHandlers) diff --git a/yarn.lock b/yarn.lock index 47a3d9a16d..c2c6e411a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2777,10 +2777,10 @@ proj4 "^2.11.0" uuid "^10.0.0" -"@openforis/arena-core@^1.0.2": - version "1.0.2" - resolved "https://npm.pkg.github.com/download/@openforis/arena-core/1.0.2/2226a763d2cbe2f30735aaa449f3f796ccf08ba2#2226a763d2cbe2f30735aaa449f3f796ccf08ba2" - integrity sha512-b22MuMmqPbRbKrJu/JMMAYfPyTGmbcVmiBEZ/0BB+CrWKSxQdD1wZ9k73gpCccbsHkQVcKrf37npO78ga8MCcw== +"@openforis/arena-core@^1.0.4": + version "1.0.4" + resolved "https://npm.pkg.github.com/download/@openforis/arena-core/1.0.4/08109df89518dd545ff0d222b00d56ba7f10b256#08109df89518dd545ff0d222b00d56ba7f10b256" + integrity sha512-56GlwLn87xOhi7GGwNkDdsPGEbIHlhWU/oXbP/gsHWCUX7+CVTQEc4h+UEZbvSb0qzxIZhIn+dxD6QmieOTmCg== dependencies: "@jsep-plugin/regex" "^1.0.3" bignumber.js "^9.1.2"