diff --git a/src/frontend/src/modals/formModal/index.tsx b/src/frontend/src/modals/formModal/index.tsx index ca8133708..0992c4c50 100644 --- a/src/frontend/src/modals/formModal/index.tsx +++ b/src/frontend/src/modals/formModal/index.tsx @@ -40,17 +40,17 @@ export default function FormModal({ setOpen: Function; flow: FlowType; }) { - const [chatValue, setChatValue] = useState(""); + const { tabsState, setTabsState } = useContext(TabsContext); + const [chatValue, setChatValue] = useState(tabsState[flow.id].formKeysData.input_keys[Object.keys(tabsState[flow.id].formKeysData.input_keys).find((k) => !tabsState[flow.id].formKeysData.handle_keys.some((j) => j === k))]); const [chatHistory, setChatHistory] = useState([]); const { reactFlowInstance } = useContext(typesContext); const { setErrorData, setNoticeData } = useContext(alertContext); - const { tabsState, setTabsState } = useContext(TabsContext); const ws = useRef(null); const [lockChat, setLockChat] = useState(false); const isOpen = useRef(open); const messagesRef = useRef(null); const id = useRef(flow.id); - const [chatKey, setChatKey] = useState(0); + const [chatKey, setChatKey] = useState(Object.keys(tabsState[flow.id].formKeysData.input_keys).find((k) => !tabsState[flow.id].formKeysData.handle_keys.some((j) => j === k))); useEffect(() => { if (messagesRef.current) { @@ -319,15 +319,7 @@ export default function FormModal({ let nodeValidationErrors = validateNodes(reactFlowInstance); if (nodeValidationErrors.length === 0) { setLockChat(true); - // Message variable makes a object with the keys being the names from tabsState[flow.id].formKeysData.input_keys and the values being the keysValue of the correspondent index - let key = Object.keys(tabsState[id.current].formKeysData.input_keys)[chatKey]; - setTabsState((old) => { - let newTabsState = _.cloneDeep(old); - newTabsState[id.current].formKeysData.input_keys[key] = chatValue; - return newTabsState; - }) let inputs = tabsState[id.current].formKeysData.input_keys; - inputs[key] = chatValue; setChatValue(""); const message = formatMessage(inputs); addChatHistory(message, true); @@ -338,6 +330,11 @@ export default function FormModal({ name: flow.name, description: flow.description, }); + setTabsState((old) => { + let newTabsState = _.cloneDeep(old); + newTabsState[id.current].formKeysData.input_keys[chatKey] = ""; + return newTabsState; + }); } else { setErrorData({ title: "Oops! Looks like you missed some required information:", @@ -361,9 +358,10 @@ export default function FormModal({ setOpen(x); } - function handleOnCheckedChange(checked: boolean, index: number) { + function handleOnCheckedChange(checked: boolean, i: string) { if (checked === true) { - setChatKey(index); + setChatKey(i); + setChatValue(tabsState[flow.id].formKeysData.input_keys[i]); } } return ( @@ -383,7 +381,7 @@ export default function FormModal({
-
+
@@ -400,9 +398,9 @@ export default function FormModal({
t === i) } + enabled={chatKey === i} setEnabled={(value) => - handleOnCheckedChange(value, k) + handleOnCheckedChange(value, i) } size="small" disabled={false} @@ -411,14 +409,16 @@ export default function FormModal({ }
@@ -486,7 +486,11 @@ export default function FormModal({ chatValue={chatValue} lockChat={lockChat} sendMessage={sendMessage} - setChatValue={setChatValue} + setChatValue={(value) => {setChatValue(value); setTabsState((old) => { + let newTabsState = _.cloneDeep(old); + newTabsState[id.current].formKeysData.input_keys[chatKey] = chatValue; + return newTabsState; + });}} inputRef={ref} />