Modifies frontend chatComponent to itemize validation errors

This commit is contained in:
Cayal Walker 2023-04-06 13:09:03 -04:00
commit c13cfd09cf

View file

@ -18,6 +18,7 @@ import { classNames, nodeColors } from "../../utils";
import { TabsContext } from "../../contexts/tabsContext"; import { TabsContext } from "../../contexts/tabsContext";
import { ChatType } from "../../types/chat"; import { ChatType } from "../../types/chat";
import ChatMessage from "./chatMessage"; import ChatMessage from "./chatMessage";
import { NodeType } from "../../types/flow";
const _ = require("lodash"); const _ = require("lodash");
@ -73,36 +74,44 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
useEffect(() => { useEffect(() => {
if (ref.current) ref.current.scrollIntoView({ behavior: "smooth" }); if (ref.current) ref.current.scrollIntoView({ behavior: "smooth" });
}, [chatHistory]); }, [chatHistory]);
function validateNodes() {
if ( function validateNode(n: NodeType): Array<string> {
reactFlowInstance.getNodes().some( if (!n.data?.node?.template ||
(n) => !Object.keys(n.data.node.template)) {
n.data.node && console.warn("There is a broken node in the flow. Please submit a bug report and include your exported flow file.");
Object.keys(n.data.node.template).some((t: any) => { return [];
return (
n.data.node.template[t].required &&
(!n.data.node.template[t].value ||
n.data.node.template[t].value === "") &&
!reactFlowInstance
.getEdges()
.some(
(e) =>
e.targetHandle.split("|")[1] === t &&
e.targetHandle.split("|")[2] === n.id
)
);
})
)
) {
return false;
} }
return true;
const { type, node: { template } } = n.data;
return Object.keys(template).reduce((errors: Array<string>, t) =>
errors.concat(
template[t].required &&
(!template[t].value ||
template[t].value === "") &&
!reactFlowInstance
.getEdges()
.some(
(e) =>
e.targetHandle.split("|")[1] === t &&
e.targetHandle.split("|")[2] === n.id
)
? [`${type} is missing ${template[t].name}.`]
: []
), [] as string[]
);
};
function validateNodes() {
return reactFlowInstance.getNodes().flatMap((n: NodeType) => validateNode(n))
} }
const ref = useRef(null); const ref = useRef(null);
function sendMessage() { function sendMessage() {
if (chatValue !== "") { if (chatValue !== "") {
if (validateNodes()) { let nodeValidationErrors = validateNodes();
if (nodeValidationErrors.length === 0) {
setLockChat(true); setLockChat(true);
let message = chatValue; let message = chatValue;
setChatValue(""); setChatValue("");
@ -138,7 +147,8 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
setErrorData({ setErrorData({
title: "Error sending message", title: "Error sending message",
list: [ list: [
"Oops! Looks like you missed some required information. Please fill in all the required fields before continuing.", "Oops! Looks like you missed some required information. Please fill in all the required fields before continuing:",
...nodeValidationErrors
], ],
}); });
} }