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

* 📝 (GenericNode/index.tsx): Add import statement for cloneDeep from lodash library
📝 (GenericNode/index.tsx): Add import statement for processNodeAdvancedFields helper function
📝 (GenericNode/index.tsx): Add edges variable to store edges state using useFlowStore hook
📝 (GenericNode/index.tsx): Modify onSuccess callback to process and update node data with advanced fields using processNodeAdvancedFields helper function

*  (process-node-advanced-fields.ts): add a new function to process advanced fields of a node based on edges in a graph for better customization and control.

* ♻️ (GenericNode/index.tsx): Remove unused import of cloneDeep from lodash to clean up the code and improve maintainability.

* 🔧 (process-node-advanced-fields.ts): rename variable edgesWithNode to relevantEdges for clarity and consistency
🐛 (process-node-advanced-fields.ts): fix condition to check relevantEdges length instead of edgesWithNode length for correct logic

---------

Co-authored-by: anovazzi1 <otavio2204@gmail.com>
This commit is contained in:
Cristhian Zanforlin Lousa 2024-11-07 10:41:23 -03:00 committed by GitHub
commit ada5f17d13
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 3 deletions

View file

@ -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";
@ -85,6 +86,8 @@ export default function GenericNode({
const { mutate: validateComponentCode } = usePostValidateComponentCode();
const edges = useFlowStore((state) => state.edges);
const handleUpdateCode = () => {
setLoadingUpdate(true);
takeSnapshot();
@ -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);
}
},

View file

@ -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;
}