Merge branch 'cayal-chatValidationReasons' into dev
This commit is contained in:
commit
200d89cc8c
1 changed files with 51 additions and 30 deletions
|
|
@ -14,10 +14,11 @@ 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";
|
||||||
|
import { NodeType } from "../../types/flow";
|
||||||
|
|
||||||
const _ = require("lodash");
|
const _ = require("lodash");
|
||||||
|
|
||||||
|
|
@ -28,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,
|
||||||
|
|
@ -73,16 +74,26 @@ 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 || !Object.keys(n.data.node.template)) {
|
||||||
(n) =>
|
setNoticeData({
|
||||||
n.data.node &&
|
title:
|
||||||
Object.keys(n.data.node.template).some((t: any) => {
|
"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 (
|
});
|
||||||
n.data.node.template[t].required &&
|
return [];
|
||||||
(!n.data.node.template[t].value ||
|
}
|
||||||
n.data.node.template[t].value === "") &&
|
|
||||||
|
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
|
!reactFlowInstance
|
||||||
.getEdges()
|
.getEdges()
|
||||||
.some(
|
.some(
|
||||||
|
|
@ -90,19 +101,31 @@ export default function Chat({ flow, reactFlowInstance }: ChatType) {
|
||||||
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.display_name
|
||||||
|
? template.display_name
|
||||||
|
: snakeToNormalCase(template[t].name)
|
||||||
|
}.`,
|
||||||
|
]
|
||||||
|
: []
|
||||||
|
),
|
||||||
|
[] as string[]
|
||||||
);
|
);
|
||||||
})
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
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("");
|
||||||
|
|
@ -136,10 +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.",
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue