Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added "distance" function builder #3679

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading