From e129b3c9f4cdcfe79aa562b9e1c73e035ef78021 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Fri, 8 Sep 2023 12:13:40 -0300 Subject: [PATCH] refactor(reactflowUtils.ts): extract validation logic into a separate function for better code organization and reusability feat(reactflowUtils.ts): add validateSelection function to validate the selection of nodes and edges in the React Flow instance --- src/frontend/src/utils/reactflowUtils.ts | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/frontend/src/utils/reactflowUtils.ts b/src/frontend/src/utils/reactflowUtils.ts index 424791fd3..5c1386b47 100644 --- a/src/frontend/src/utils/reactflowUtils.ts +++ b/src/frontend/src/utils/reactflowUtils.ts @@ -487,6 +487,37 @@ export function concatFlows( ReactFlowInstance.addEdges(edges); } +export function validateSelection( + selection: OnSelectionChangeParams +): Array { + let errorsArray: Array = []; + // check if there is more than one node + if (selection.nodes.length < 2) { + errorsArray.push("Please select more than one node"); + } + + //check if there are two or more nodes with free outputs + if ( + selection.nodes.filter( + (n) => !selection.edges.some((e) => e.source === n.id) + ).length > 1 + ) { + errorsArray.push("Please select only one node with free outputs"); + } + + // check if there is any node that does not have any connection + if ( + selection.nodes.some( + (node) => + !selection.edges.some((edge) => edge.target === node.id) && + !selection.edges.some((edge) => edge.source === node.id) + ) + ) { + errorsArray.push("Please select only nodes that are connected"); + } + return errorsArray; +} + export function generateNodeFromFlow(flow: FlowType): NodeType { const { nodes } = flow.data!; const outputNode = _.cloneDeep(findLastNode(flow.data!));