review pull request #126 and changed some error messages

This commit is contained in:
anovazzi1 2023-04-06 16:24:53 -03:00
commit c7d5139ecc

View file

@ -14,7 +14,7 @@ import {
} from "react"; } from "react";
import { sendAll } from "../../controllers/API"; import { sendAll } from "../../controllers/API";
import { alertContext } from "../../contexts/alertContext"; import { alertContext } from "../../contexts/alertContext";
import { classNames, nodeColors } from "../../utils"; import { classNames, nodeColors, snakeToNormalCase } 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";
@ -29,7 +29,7 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
const [open, setOpen] = useState(true); const [open, setOpen] = useState(true);
const [chatValue, setChatValue] = useState(""); const [chatValue, setChatValue] = useState("");
const [chatHistory, setChatHistory] = useState(flow.chat); const [chatHistory, setChatHistory] = useState(flow.chat);
const { setErrorData } = useContext(alertContext); const { setErrorData, setNoticeData } = useContext(alertContext);
const addChatHistory = ( const addChatHistory = (
message: string, message: string,
isSend: boolean, isSend: boolean,
@ -76,34 +76,48 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
}, [chatHistory]); }, [chatHistory]);
function validateNode(n: NodeType): Array<string> { function validateNode(n: NodeType): Array<string> {
if (!n.data?.node?.template || if (!n.data?.node?.template || !Object.keys(n.data.node.template)) {
!Object.keys(n.data.node.template)) { setNoticeData({
console.warn("There is a broken node in the flow. Please submit a bug report and include your exported flow file."); 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 []; return [];
} }
const { type, node: { template } } = n.data; const {
type,
node: { template },
} = n.data;
return Object.keys(template).reduce((errors: Array<string>, t) => return Object.keys(template).reduce(
errors.concat( (errors: Array<string>, t) =>
template[t].required && errors.concat(
(!template[t].value || template[t].required &&
template[t].value === "") && (!template[t].value || template[t].value === "") &&
!reactFlowInstance !reactFlowInstance
.getEdges() .getEdges()
.some( .some(
(e) => (e) =>
e.targetHandle.split("|")[1] === t && e.targetHandle.split("|")[1] === t &&
e.targetHandle.split("|")[2] === n.id e.targetHandle.split("|")[2] === n.id
) )
? [`${type} is missing ${template[t].name}.`] ? [
: [] `${type} is missing ${
), [] as string[] template.display_name
? template.display_name
: snakeToNormalCase(template[t].name)
}.`,
]
: []
),
[] as string[]
); );
}; }
function validateNodes() { function validateNodes() {
return reactFlowInstance.getNodes().flatMap((n: NodeType) => validateNode(n)) return reactFlowInstance
.getNodes()
.flatMap((n: NodeType) => validateNode(n));
} }
const ref = useRef(null); const ref = useRef(null);
@ -145,11 +159,8 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
}); });
} else { } else {
setErrorData({ setErrorData({
title: "Error sending message", title: "Oops! Looks like you missed some required information:",
list: [ list: nodeValidationErrors,
"Oops! Looks like you missed some required information. Please fill in all the required fields before continuing:",
...nodeValidationErrors
],
}); });
} }
} else { } else {