From 9a41ca9ce2eeaf6ed636bc79593d17e1227a1a32 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Thu, 13 Jul 2023 18:43:57 -0300 Subject: [PATCH] updateTemplate migration done --- src/frontend/src/contexts/tabsContext.tsx | 8 ++------ src/frontend/src/utils/reactflowUtils.ts | 24 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/frontend/src/contexts/tabsContext.tsx b/src/frontend/src/contexts/tabsContext.tsx index 479131db2..c5c4264de 100644 --- a/src/frontend/src/contexts/tabsContext.tsx +++ b/src/frontend/src/contexts/tabsContext.tsx @@ -20,12 +20,8 @@ import { import { APIClassType, APITemplateType } from "../types/api"; import { FlowType, NodeType } from "../types/flow"; import { TabsContextType, TabsState } from "../types/tabs"; -import { - getRandomDescription, - getRandomName, - updateIds, - updateTemplate, -} from "../utils"; +import { getRandomDescription, getRandomName, updateIds } from "../utils"; +import { updateTemplate } from "../utils/reactflowUtils"; import { alertContext } from "./alertContext"; import { typesContext } from "./typesContext"; diff --git a/src/frontend/src/utils/reactflowUtils.ts b/src/frontend/src/utils/reactflowUtils.ts index 7bd9cc4a2..c8623cf69 100644 --- a/src/frontend/src/utils/reactflowUtils.ts +++ b/src/frontend/src/utils/reactflowUtils.ts @@ -1,5 +1,6 @@ import _ from "lodash"; import { Connection, ReactFlowInstance } from "reactflow"; +import { APITemplateType } from "../types/api"; import { FlowType } from "../types/flow"; import { cleanEdgesType } from "../types/utils/reactflowUtils"; @@ -100,3 +101,26 @@ export function removeApiKeys(flow: FlowType): FlowType { }); return cleanFLow; } + +export function updateTemplate( + reference: APITemplateType, + objectToUpdate: APITemplateType +): APITemplateType { + let clonedObject: APITemplateType = _.cloneDeep(reference); + + // Loop through each key in the reference object + for (const key in clonedObject) { + // If the key is not in the object to update, add it + if (objectToUpdate[key] && objectToUpdate[key].value) { + clonedObject[key].value = objectToUpdate[key].value; + } + if ( + objectToUpdate[key] && + objectToUpdate[key].advanced !== null && + objectToUpdate[key].advanced !== undefined + ) { + clonedObject[key].advanced = objectToUpdate[key].advanced; + } + } + return clonedObject; +}