fixed AutoUpdate for each node

This commit is contained in:
anovazzi1 2023-05-02 18:46:29 -03:00 • committed by Gabriel Luiz Freitas Almeida
commit 83b3347fc5
2 changed files with 380 additions and 361 deletions

View file

@ -8,10 +8,10 @@ import {
} from "react"; } from "react";
import { FlowType } from "../types/flow"; import { FlowType } from "../types/flow";
import { LangFlowState, TabsContextType } from "../types/tabs"; import { LangFlowState, TabsContextType } from "../types/tabs";
import { normalCaseToSnakeCase, updateObject } from "../utils"; import { normalCaseToSnakeCase, updateObject, updateTemplate } from "../utils";
import { alertContext } from "./alertContext"; import { alertContext } from "./alertContext";
import { typesContext } from "./typesContext"; import { typesContext } from "./typesContext";
import { TemplateVariableType } from "../types/api"; import { APITemplateType, TemplateVariableType } from "../types/api";
const { v4: uuidv4 } = require("uuid"); const { v4: uuidv4 } = require("uuid");
const TabsContextInitialValue: TabsContextType = { const TabsContextInitialValue: TabsContextType = {
@ -64,12 +64,12 @@ export function TabsProvider({ children }: { children: ReactNode }) {
cookieObject.flows.forEach((flow) => { cookieObject.flows.forEach((flow) => {
flow.data.nodes.forEach((node) => { flow.data.nodes.forEach((node) => {
if (Object.keys(templates[node.data.type]["template"]).length > 0) { if (Object.keys(templates[node.data.type]["template"]).length > 0) {
node.data.node.template = updateObject( node.data.node.template = updateTemplate(
templates[node.data.type][ templates[node.data.type][
"template" "template"
] as unknown as TemplateVariableType, ] as unknown as APITemplateType,
node.data.node.template as TemplateVariableType node.data.node.template as APITemplateType
); );
} }
}); });
@ -127,16 +127,6 @@ export function TabsProvider({ children }: { children: ReactNode }) {
file.text().then((text) => { file.text().then((text) => {
// parse the text into a JSON object // parse the text into a JSON object
let flow: FlowType = JSON.parse(text); let flow: FlowType = JSON.parse(text);
flow.data.nodes.forEach((node) => {
if (Object.keys(templates[node.data.type]["template"]).length > 0) {
node.data.node.template = updateObject(
templates[node.data.type][
"template"
] as unknown as TemplateVariableType,
node.data.node.template as TemplateVariableType
);
}
});
addFlow(flow); addFlow(flow);
}); });
@ -176,6 +166,18 @@ export function TabsProvider({ children }: { children: ReactNode }) {
const data = flow?.data ? flow.data : null; const data = flow?.data ? flow.data : null;
const description = flow?.description ? flow.description : ""; const description = flow?.description ? flow.description : "";
if(data){
data.nodes.forEach((node) => {
if (Object.keys(templates[node.data.type]["template"]).length > 0) {
node.data.node.template = updateTemplate(
templates[node.data.type][
"template"
] as unknown as APITemplateType,
node.data.node.template as APITemplateType
);
}
});
}
// Create a new flow with a default name if no flow is provided. // Create a new flow with a default name if no flow is provided.
let newFlow: FlowType = { let newFlow: FlowType = {
description, description,

View file

@ -18,6 +18,7 @@ import {
} from "@heroicons/react/24/outline"; } from "@heroicons/react/24/outline";
import { Connection, Edge, Node, ReactFlowInstance } from "reactflow"; import { Connection, Edge, Node, ReactFlowInstance } from "reactflow";
import { FlowType } from "./types/flow"; import { FlowType } from "./types/flow";
import { APITemplateType, TemplateVariableType } from "./types/api";
var _ = require("lodash"); var _ = require("lodash");
export function classNames(...classes: Array<string>) { export function classNames(...classes: Array<string>) {
@ -429,3 +430,19 @@ export function debounce(func, wait) {
timeout = setTimeout(() => func.apply(context, args), wait); timeout = setTimeout(() => func.apply(context, args), wait);
}; };
} }
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;
}
}
return clonedObject;
}