This commit is contained in:
ahmed531998 2023-04-18 22:31:43 +02:00
parent 785d87093a
commit e37714b129
1 changed files with 30 additions and 13 deletions

View File

@ -6,35 +6,48 @@ 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 tick = useRef()
const [username, setUsername] = useState("");
const [timer, setTimer] = useState(0);
const [stat, setStat] = useState("start");
useEffect(() => {
if (!ready && username===""){
if (stat === "waiting"){
let x = "set"
fetch(backendUrl + '/dm', {
method: 'POST',
body: JSON.stringify({stat: x, token: token }),
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
}).then(r => r.json()).then(r => {
setStat(r.stat);
}).catch((error) => {
console.error('Error:', error);
});
}
if (stat==="start"){
tick.current = setInterval(() => {
setTimer((timer) => timer + 1);
}, 1000);
if(timer >= 10){
if(timer >= 30){
fetch(backendUrl + '/dm', {
method: 'POST',
body: JSON.stringify({ token: token }),
body: JSON.stringify({ stat: stat, 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);
setStat(r.stat);
}).catch((error) => {
console.error('Error:', error);
});
@ -42,8 +55,9 @@ const ChatPage = () => {
clearInterval(tick.current);
}
}
return () => clearInterval(tick.current);
}, [ready, username, timer]);
}, [stat, timer]);
function updateNeedForm(value) {
setNeedForm(value);
@ -53,9 +67,12 @@ const ChatPage = () => {
}
return (
<div>
{!ready && <p>Loading...</p>}
{ready && <ChatBox props={{ updateNeedForm, messages, updateMessages, token}} /> }
{ready && needForm && <FeedbackForm props={{ updateNeedForm, messages }} /> }
{stat==="waiting" && <p>Loading resources...</p>}
{stat==="start" && <p>Connecting to Server...</p>}
{stat==="rejected" && <p>Unauthorized Use...</p>}
{stat==="done" && <ChatBox props={{ updateNeedForm, messages, updateMessages, token}} /> }
{stat==="done" && needForm && <FeedbackForm props={{ updateNeedForm, messages }} /> }
</div>
);
};