From 80dadddc05fdad6898500fde1066c5e8bc8d2386 Mon Sep 17 00:00:00 2001 From: Stefano Ricci <1219739+SteRiccio@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:18:12 +0100 Subject: [PATCH] fixed using userProp function in items filter expression (#3693) Co-authored-by: Stefano Ricci --- .../components/types/useItemsFilter.js | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/webapp/components/survey/SurveyForm/nodeDefs/components/types/useItemsFilter.js b/webapp/components/survey/SurveyForm/nodeDefs/components/types/useItemsFilter.js index d362d316ba..c693b6682e 100644 --- a/webapp/components/survey/SurveyForm/nodeDefs/components/types/useItemsFilter.js +++ b/webapp/components/survey/SurveyForm/nodeDefs/components/types/useItemsFilter.js @@ -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() @@ -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) +}