Skip to content

Commit

Permalink
added "distance" function builder (#3679)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefano Ricci <[email protected]>
  • Loading branch information
SteRiccio and SteRiccio authored Dec 10, 2024
1 parent d79bfab commit d344983
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/i18n/resources/en/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,8 @@ Levels will be renamed into level_1, level_2... level_N and an extra 'area' prop
call: 'Function',
operator: 'Operator',

coordinateAttributeWithPosition: 'Coordinate attribute {{position}}',

error: {
selectOneVariable: 'Please select one variable',
},
Expand Down
81 changes: 81 additions & 0 deletions webapp/components/expression/nodes/call/callDistanceEditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { useCallback, useState } from 'react'

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

import * as NodeDef from '@core/survey/nodeDef'
import * as Expression from '@core/expressionParser/expression'

import { Button } from '@webapp/components/buttons'
import { FormItem } from '@webapp/components/form/Input'

import Identifier from '../identifier'
import { CallEditorPropTypes } from './callEditorPropTypes'

const variablesFilterFn = (variable) => variable.root || variable.nodeDefType === NodeDef.nodeDefType.coordinate

export const CallDistanceEditor = (props) => {
const { expressionNode, onConfirm: onConfirmProp, variables } = props

const identifierArguments = expressionNode?.arguments ?? []

const [state, setState] = useState({
dirty: !identifierArguments?.[0] || !identifierArguments?.[1],
identifierParams: identifierArguments,
})

const { dirty, identifierParams } = state

const onIdentifierParamChange = useCallback(
(paramIndex) => (identifierUpdated) =>
setState((statePrev) => {
const identifierParamsUpdated = [...statePrev.identifierParams]
const identifierParamUpdated = identifierUpdated?.name
? { type: Expression.types.Identifier, ...identifierUpdated }
: null
identifierParamsUpdated[paramIndex] = identifierParamUpdated
return {
...statePrev,
dirty: true,
identifierParams: identifierParamsUpdated,
}
}),
[]
)

const buildFunctionCall = useCallback(() => {
const params = identifierParams
return Expression.newCall({ callee: Expression.functionNames.distance, params })
}, [identifierParams])

const onConfirm = useCallback(() => {
onConfirmProp(buildFunctionCall())
setState((statePrev) => ({ ...statePrev, dirty: false }))
}, [buildFunctionCall, onConfirmProp])

return (
<div className="function-editor">
{Arrays.fromNumberOfElements(2).map((v, index) => (
<FormItem
key={String(v)}
label="expressionEditor.coordinateAttributeWithPosition"
labelParams={{ position: index + 1 }}
>
<Identifier
node={identifierParams[index]}
onChange={onIdentifierParamChange(index)}
variables={variables}
variablesFilterFn={variablesFilterFn}
/>
</FormItem>
))}

<Button
disabled={!dirty || !identifierParams[0] || !identifierParams[1]}
label="common.apply"
onClick={onConfirm}
/>
</div>
)
}

CallDistanceEditor.propTypes = CallEditorPropTypes
5 changes: 5 additions & 0 deletions webapp/components/expression/nodes/call/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Expression from '@core/expressionParser/expression'

import { CallCategoryItemPropEditor } from './callCategoryItemPropEditor'
import { CallCountEditor } from './callCountEditor'
import { CallDistanceEditor } from './callDistanceEditor'
import { CallIncludesEditor } from './callIncludesEditor'
import { CallIsEmptyEditor } from './callIsEmptyEditor'
import { CallIsNotEmptyEditor } from './callIsNotEmptyEditor'
Expand Down Expand Up @@ -30,6 +31,10 @@ export const functions = {
label: 'count(...)',
component: CallCountEditor,
},
[functionNames.distance]: {
label: 'distance(...)',
component: CallDistanceEditor,
},
[functionNames.includes]: {
label: 'includes(...)',
component: CallIncludesEditor,
Expand Down

0 comments on commit d344983

Please sign in to comment.