From 72dea88f14516bbae35ef0e7cd130e82c9573732 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Mon, 12 Jun 2023 18:20:52 -0300 Subject: [PATCH] moved validateNodes function to utils --- src/frontend/src/modals/chatModal/index.tsx | 51 +-------------------- src/frontend/src/utils.ts | 45 ++++++++++++++++++ 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/src/frontend/src/modals/chatModal/index.tsx b/src/frontend/src/modals/chatModal/index.tsx index 59f117319..17ae2bb41 100644 --- a/src/frontend/src/modals/chatModal/index.tsx +++ b/src/frontend/src/modals/chatModal/index.tsx @@ -6,7 +6,7 @@ import { import { Fragment, useContext, useEffect, useRef, useState } from "react"; import { FlowType, NodeType } from "../../types/flow"; import { alertContext } from "../../contexts/alertContext"; -import { toNormalCase } from "../../utils"; +import { toNormalCase, validateNodes } from "../../utils"; import { typesContext } from "../../contexts/typesContext"; import ChatMessage from "./chatMessage"; import { FaEraser } from "react-icons/fa"; @@ -273,53 +273,6 @@ export default function ChatModal({ if (ref.current) ref.current.scrollIntoView({ behavior: "smooth" }); }, [chatHistory]); - function validateNode(n: NodeType): Array { - if (!n.data?.node?.template || !Object.keys(n.data.node.template)) { - setNoticeData({ - title: - "We've noticed a potential issue with a node in the flow. Please review it and, if necessary, submit a bug report with your exported flow file. Thank you for your help!", - }); - return []; - } - - const { - type, - node: { template }, - } = n.data; - - return Object.keys(template).reduce( - (errors: Array, t) => - errors.concat( - template[t].required && - template[t].show && - (template[t].value === undefined || - template[t].value === null || - template[t].value === "") && - !reactFlowInstance - .getEdges() - .some( - (e) => - e.targetHandle.split("|")[1] === t && - e.targetHandle.split("|")[2] === n.id - ) - ? [ - `${type} is missing ${ - template.display_name - ? template.display_name - : toNormalCase(template[t].name) - }.`, - ] - : [] - ), - [] as string[] - ); - } - - function validateNodes() { - return reactFlowInstance - .getNodes() - .flatMap((n: NodeType) => validateNode(n)); - } const ref = useRef(null); @@ -331,7 +284,7 @@ export default function ChatModal({ function sendMessage() { if (chatValue !== "") { - let nodeValidationErrors = validateNodes(); + let nodeValidationErrors = validateNodes(reactFlowInstance); if (nodeValidationErrors.length === 0) { setLockChat(true); let message = chatValue; diff --git a/src/frontend/src/utils.ts b/src/frontend/src/utils.ts index 920c1b296..e13f26745 100644 --- a/src/frontend/src/utils.ts +++ b/src/frontend/src/utils.ts @@ -703,3 +703,48 @@ export function groupByFamily(data, baseClasses) { return groupedObj; } + +export function validateNode(n: NodeType, reactFlowInstance:ReactFlowInstance):Array{ + if (!n.data?.node?.template || !Object.keys(n.data.node.template)) { + return ["We've noticed a potential issue with a node in the flow. Please review it and, if necessary, submit a bug report with your exported flow file. Thank you for your help!"]; + } + + const { + type, + node: { template }, + } = n.data; + + return Object.keys(template).reduce( + (errors: Array, t) => + errors.concat( + template[t].required && + template[t].show && + (template[t].value === undefined || + template[t].value === null || + template[t].value === "") && + !reactFlowInstance + .getEdges() + .some( + (e) => + e.targetHandle.split("|")[1] === t && + e.targetHandle.split("|")[2] === n.id + ) + ? [ + `${type} is missing ${ + template.display_name + ? template.display_name + : toNormalCase(template[t].name) + }.`, + ] + : [] + ), + [] as string[] + ); + +} + +export function validateNodes(reactFlowInstance:ReactFlowInstance){ + return reactFlowInstance + .getNodes() + .flatMap((n: NodeType) => validateNode(n, reactFlowInstance)); +} \ No newline at end of file