From 36a3051019645f2ed90522dca7dc8012190f9d2e Mon Sep 17 00:00:00 2001 From: ahmed531998 Date: Tue, 18 Apr 2023 14:01:39 +0200 Subject: [PATCH] fix --- src/ChatBoxProps.js | 25 +++---------------------- src/ChatPage.js | 30 +++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/ChatBoxProps.js b/src/ChatBoxProps.js index 92e9f49..429175e 100644 --- a/src/ChatBoxProps.js +++ b/src/ChatBoxProps.js @@ -8,31 +8,12 @@ const ChatBoxProps = (props) => { const backendUrl = '/api'; const [clicked, setClicked] = useState(false); const [timer, setTimer] = useState(0); - const [first, setFirst] = useState(true); + const [first, setFirst] = useState(false); const [busy, setBusy] = useState(false); - const tick = useRef() - const [username, setUsername] = useState(""); - - + const tick = useRef() + useEffect(() => { if (first){ - fetch(backendUrl + '/dm', { - method: 'POST', - body: JSON.stringify({ token: token }), - mode: 'cors', - headers: { - 'Content-Type': 'application/json' - }, - }).then(r => r.json()).then(r => { - let msg2 = { name: "Janet", message: r.answer}; - //updateMessages(msg2); - setUsername(r.assignedname); - }).catch((error) => { - console.error('Error:', error); - let msg3 = { name: "Janet", message: "Server is down, try later."}; - updateMessages(msg3); - }); - fetch(backendUrl + '/predict', { method: 'POST', body: JSON.stringify({ message: "", token: token }), diff --git a/src/ChatPage.js b/src/ChatPage.js index 937c4ff..7d90f4c 100644 --- a/src/ChatPage.js +++ b/src/ChatPage.js @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import { useSearchParams, useParams } from "react-router-dom"; import "./style.css"; import ChatBox from "./ChatBox.js"; @@ -6,11 +6,35 @@ import FeedbackForm from "./FeedbackForm.js"; const ChatPage = () => { const [needForm, setNeedForm] = useState(false); + const [ready, setReady] = useState(false); //const { token } = useParams(); + const backendUrl = '/api'; const [searchParams, setSearchParams] = useSearchParams(); const token = searchParams.get("gcube-token") || ''; const [messages, setMessages] = useState([]); + + const [username, setUsername] = useState(""); + + useEffect(() => { + if (!(token === "") && !ready){ + fetch(backendUrl + '/dm', { + method: 'POST', + body: JSON.stringify({ token: token }), + mode: 'cors', + headers: { + 'Content-Type': 'application/json' + }, + }).then(r => r.json()).then(r => { + let msg2 = { name: "Janet", message: r.answer}; + setUsername(r.assignedname); + setReady(true); + }).catch((error) => { + console.error('Error:', error); + }); + } + }, [token, ready, username]); + function updateNeedForm(value) { setNeedForm(value); } @@ -19,8 +43,8 @@ const ChatPage = () => { } return (
- - { needForm && } + {ready && } + {ready && needForm && }
); };