From 6abcd16339afc9118a35162331d8129323f64101 Mon Sep 17 00:00:00 2001 From: BPierrick Date: Thu, 7 Oct 2021 10:44:35 +0200 Subject: [PATCH] fix: Pending badge for choice nodes parend of pending attributes --- app/src/common/hooks/useIsNodePending.tsx | 28 ++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/app/src/common/hooks/useIsNodePending.tsx b/app/src/common/hooks/useIsNodePending.tsx index 570c796b3..380c70483 100644 --- a/app/src/common/hooks/useIsNodePending.tsx +++ b/app/src/common/hooks/useIsNodePending.tsx @@ -10,10 +10,34 @@ import { useApiStaticInputsListQuery, } from "services/api/endpoints"; import type { + Attribute, SQLInput, StaticInput, } from "services/api/generated/api.generated"; +/** + * Tests if node is a direct parent of attribute and if node kind is choice + * + * ie: returns true if + * Node path => `Observation.effective[x]` & attribute path => `Observation.effectiveBoolean` + * @param attribute The attribute that has an input + * @param node The current node to be tested as the attribute ancestor + * @returns True if node kind is "choice" and node is a parent of attribute + */ +const isAttributeChoiceOfNode = ( + attribute: Attribute, + node: ElementNode +): boolean => { + // Node kind has to be "choice" + if (node.kind !== "choice") return false; + + const isNodeParentOfAttribute = node.children.some( + ({ path }) => path === attribute.path + ); + + return isNodeParentOfAttribute; +}; + const useIsNodePending = (node: ElementNode): boolean => { const { mappingId } = useParams<{ mappingId?: string }>(); @@ -66,7 +90,9 @@ const useIsNodePending = (node: ElementNode): boolean => { attributesWithInputs !== undefined && attributesWithInputs.some( (attribute) => - attribute.path === node.path || attribute.path.startsWith(node.path) + attribute.path === node.path || + attribute.path.startsWith(node.path) || + isAttributeChoiceOfNode(attribute, node) ) ); };