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
This commit is contained in:
anovazzi1 2023-09-08 12:13:40 -03:00
commit e129b3c9f4

View file

@ -487,6 +487,37 @@ export function concatFlows(
ReactFlowInstance.addEdges(edges);
}
export function validateSelection(
selection: OnSelectionChangeParams
): Array<string> {
let errorsArray: Array<string> = [];
// 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!));