Skip to content

Commit

Permalink
Merge branch 'master' into fix/unary-expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Nov 22, 2024
2 parents ea65026 + f686ce8 commit de3a3aa
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 38 deletions.
22 changes: 10 additions & 12 deletions core/survey/_survey/surveyNodeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)))
Expand All @@ -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
Expand Down
18 changes: 14 additions & 4 deletions core/survey/_survey/surveyNodeDefsIndex.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Surveys } from '@openforis/arena-core'
import { Objects, Surveys } from '@openforis/arena-core'

// ==== READ

Expand All @@ -7,28 +7,38 @@ 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

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)
11 changes: 8 additions & 3 deletions core/survey/survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ export const {
hasNodeDefChildrenEntities,
getNodeDefChildrenSorted,
getNodeDefChildByName,
getNodeDefSiblingByName,
getNodeDefByName,
getNodeDefsByCategoryUuid,
getNodeDefsByTaxonomyUuid,
Expand All @@ -173,6 +172,7 @@ export const {
getNodeDefKeysSorted,
isNodeDefRootKey,
findNodeDef,
findNodeDefByName,
getNodeDefAreaBasedEstimate,
getAreaBasedEstimatedOfNodeDef,
getNodeDefMaxDecimalDigits,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 14 additions & 6 deletions webapp/components/expression/nodes/literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ export const useSetProp = ({ setState }) => {

nodeDefUpdated = NodeDef.clearNotApplicableProps(surveyCycleKey)(nodeDefUpdated)

validateNodeDef({ nodeDefUpdated })
validateNodeDef({ nodeDef, nodeDefUpdated })
}, [])
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion webapp/store/survey/nodeDefs/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 15 additions & 4 deletions webapp/store/survey/nodeDefsIndex/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit de3a3aa

Please sign in to comment.