Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: Add Helper for Field Processing & Update Code Validation Logic #4429

Merged
merged 7 commits into from
Nov 7, 2024
15 changes: 12 additions & 3 deletions src/frontend/src/CustomNodes/GenericNode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { getNodeInputColors } from "../helpers/get-node-input-colors";
import { getNodeInputColorsName } from "../helpers/get-node-input-colors-name";
import { getNodeOutputColors } from "../helpers/get-node-output-colors";
import { getNodeOutputColorsName } from "../helpers/get-node-output-colors-name";
import { processNodeAdvancedFields } from "../helpers/process-node-advanced-fields";
import useCheckCodeValidity from "../hooks/use-check-code-validity";
import useUpdateNodeCode from "../hooks/use-update-node-code";
import getFieldTitle from "../utils/get-field-title";
Expand Down Expand Up @@ -85,6 +86,8 @@ export default function GenericNode({

const { mutate: validateComponentCode } = usePostValidateComponentCode();

const edges = useFlowStore((state) => state.edges);

const handleUpdateCode = () => {
setLoadingUpdate(true);
takeSnapshot();
Expand All @@ -99,9 +102,15 @@ export default function GenericNode({
validateComponentCode(
{ code: currentCode, frontend_node: data.node },
{
onSuccess: ({ data, type }) => {
if (data && type && updateNodeCode) {
updateNodeCode(data, currentCode, "code", type);
onSuccess: ({ data: resData, type }) => {
if (resData && type && updateNodeCode) {
const newNode = processNodeAdvancedFields(
resData,
edges,
data.id,
);

updateNodeCode(newNode, currentCode, "code", type);
setLoadingUpdate(false);
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { APIClassType } from "@/types/api";
import { cloneDeep } from "lodash";
import { Edge } from "reactflow";

export function processNodeAdvancedFields(
resData: APIClassType,
edges: Edge[],
nodeId: string,
) {
let newNode = cloneDeep(resData);

const relevantEdges = edges.filter(
(edge) => edge.source !== nodeId || edge.target !== nodeId,
);

if (relevantEdges.length === 0) {
return newNode;
}

for (const edge of relevantEdges) {
const field = edge?.data?.targetHandle?.fieldName;

if (field) {
const fieldTemplate = newNode.template[field];
if (fieldTemplate?.advanced === true) {
newNode.template[field].advanced = false;
}
}
}

return newNode;
}
Loading