From 4637f4e9488ecc6e59d097ec263455f10b919c48 Mon Sep 17 00:00:00 2001 From: Stefano Ricci Date: Fri, 25 Oct 2024 23:27:09 +0200 Subject: [PATCH] fixed chain: order of selected dimensions --- .../DimensionsSelector/DimensionsSelector.js | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/webapp/views/App/views/Analysis/Chain/statisticalAnalysis/DimensionsSelector/DimensionsSelector.js b/webapp/views/App/views/Analysis/Chain/statisticalAnalysis/DimensionsSelector/DimensionsSelector.js index 7b0547b29d..8c55ea0ca1 100644 --- a/webapp/views/App/views/Analysis/Chain/statisticalAnalysis/DimensionsSelector/DimensionsSelector.js +++ b/webapp/views/App/views/Analysis/Chain/statisticalAnalysis/DimensionsSelector/DimensionsSelector.js @@ -1,6 +1,6 @@ import './DimensionsSelector.scss' -import React, { useCallback } from 'react' +import React, { useCallback, useMemo } from 'react' import PropTypes from 'prop-types' import * as Survey from '@core/survey/survey' @@ -25,42 +25,49 @@ export const DimensionsSelector = (props) => { const survey = useSurvey() const lang = useSurveyPreferredLang() - const options = [] const entityDef = entityDefUuid ? Survey.getNodeDefByUuid(entityDefUuid)(survey) : null - const isDimensionIncluded = (nodeDef) => - NodeDef.isSingleAttribute(nodeDef) && - [NodeDef.nodeDefType.boolean, NodeDef.nodeDefType.code, NodeDef.nodeDefType.taxon].includes( - NodeDef.getType(nodeDef) - ) && - NodeDef.isInCycle(cycle)(nodeDef) && - (!chainUuid || !NodeDef.getChainUuid(nodeDef) || chainUuid === NodeDef.getChainUuid(nodeDef)) + const isDimensionIncluded = useCallback( + (nodeDef) => + NodeDef.isSingleAttribute(nodeDef) && + [NodeDef.nodeDefType.boolean, NodeDef.nodeDefType.code, NodeDef.nodeDefType.taxon].includes( + NodeDef.getType(nodeDef) + ) && + NodeDef.isInCycle(cycle)(nodeDef) && + (!chainUuid || !NodeDef.getChainUuid(nodeDef) || chainUuid === NodeDef.getChainUuid(nodeDef)), + [chainUuid, cycle] + ) - if (entityDef) { + const { options, optionsByDimensionUuid } = useMemo(() => { + if (!entityDef) return { options: [], optionsByDimensionUuid: {} } + const _options = [] + const _optionsByDimensionUuid = {} Survey.visitAncestorsAndSelf(entityDef, (ancestorDef) => { const dimensionsInAncestor = Survey.getNodeDefDescendantAttributesInSingleEntities({ nodeDef: ancestorDef, includeAnalysis: showAnalysisAttributes, })(survey).filter(isDimensionIncluded) - const nestedOptions = dimensionsInAncestor.map((dimension) => ({ - value: NodeDef.getUuid(dimension), - label: NodeDef.getLabel(dimension, lang, labelType), - icon: NodeDefUIProps.getIconByType(NodeDef.getType(dimension)), - })) - - const option = { + const nestedOptions = dimensionsInAncestor.map((dimension) => { + const dimensionUuid = NodeDef.getUuid(dimension) + const nestedOption = { + value: dimensionUuid, + label: NodeDef.getLabel(dimension, lang, labelType), + icon: NodeDefUIProps.getIconByType(NodeDef.getType(dimension)), + } + _optionsByDimensionUuid[dimensionUuid] = nestedOption + return nestedOption + }) + _options.push({ value: NodeDef.getUuid(ancestorDef), label: NodeDef.getLabel(ancestorDef, lang, labelType), options: nestedOptions, - } - options.push(option) + }) })(survey) - } + return { options: _options, optionsByDimensionUuid: _optionsByDimensionUuid } + }, [entityDef, isDimensionIncluded, labelType, lang, showAnalysisAttributes, survey]) - const selectedOptions = options - .flatMap((ancestorDefOption) => ancestorDefOption.options) - .filter((option) => selectedDimensionsUuids.includes(option.value)) + const selectedOptions = selectedDimensionsUuids.map((dimensionUuid) => optionsByDimensionUuid[dimensionUuid]) const onChange = useCallback( (_selectedOptions) => {