Skip to content

Commit

Permalink
fixed using userProp function in items filter expression (#3693)
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 265e0f0 commit 80daddd
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ import { useSelector } from 'react-redux'

import { Objects, RecordExpressionEvaluator } from '@openforis/arena-core'

import * as Survey from '@core/survey/survey'
import * as NodeDef from '@core/survey/nodeDef'

import { RecordState } from '@webapp/store/ui/record'
import { SurveyState } from '@webapp/store/survey'
import { useUser } from '@webapp/store/user'

export const useItemsFilter = ({ nodeDef, parentNode, items, alwaysIncludeItemFunction = null }) =>
useSelector((state) => {
export const useItemsFilter = ({ nodeDef, parentNode, items, alwaysIncludeItemFunction = null }) => {
const user = useUser()
return useSelector((state) => {
const itemsFilter = NodeDef.getItemsFilter(nodeDef)

if (!Array.isArray(items) || items.length === 0 || Objects.isEmpty(itemsFilter)) return items

const survey = SurveyState.getSurvey(state)
const surveyInState = SurveyState.getSurvey(state)

// convert survey in state into the format used in arena-core
const survey = { ...Survey.getSurveyInfo(surveyInState), ...surveyInState }

const record = RecordState.getRecord(state)

const expressionEvaluator = new RecordExpressionEvaluator()
Expand All @@ -22,16 +29,11 @@ export const useItemsFilter = ({ nodeDef, parentNode, items, alwaysIncludeItemFu
if (alwaysIncludeItemFunction?.(item)) return true

try {
return expressionEvaluator.evalExpression({
survey,
record,
node: parentNode,
query: itemsFilter,
item,
})
return expressionEvaluator.evalExpression({ user, survey, record, node: parentNode, query: itemsFilter, item })
} catch (error) {
// TODO throw error?
return false
}
})
}, Objects.isEqual)
}

0 comments on commit 80daddd

Please sign in to comment.