From 3a344125854eb0d0c7808e25e9f624df2cd7aab4 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Mon, 29 May 2023 18:24:54 -0300 Subject: [PATCH] update utils with updateIds function --- src/frontend/src/utils.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/frontend/src/utils.ts b/src/frontend/src/utils.ts index 5084d1076..2c2732962 100644 --- a/src/frontend/src/utils.ts +++ b/src/frontend/src/utils.ts @@ -20,6 +20,7 @@ import { Connection, Edge, Node, ReactFlowInstance } from "reactflow"; import { FlowType } from "./types/flow"; import { APITemplateType, TemplateVariableType } from "./types/api"; import _ from "lodash"; +import { v4 as uuidv4 } from "uuid"; export function classNames(...classes: Array) { return classes.filter(Boolean).join(" "); @@ -534,3 +535,27 @@ export function checkUpperWords(str: string) { return words.join(" "); } + +export function updateIds(newFLow: FlowType, baseFlow: FlowType) { + newFLow.data.nodes.forEach((node) => { + while (baseFlow.data.nodes.some((n) => n.id === node.id)) { + const newId = uuidv4(); + newFLow.data.edges.forEach((edge) => { + if (edge.source === node.id) { + edge.source = newId; + } + if (edge.target === node.id) { + edge.target = newId; + } + const index = edge.id.split("|").findIndex((e) => e === node.id); + if (index != -1) { + let tempList = edge.id.split("|"); + tempList[index] = newId; + edge.id = tempList.concat(newId).join("|"); + } + node.id = newId; + }); + } + }); + return newFLow; +}