JanetFrontEnd/src/ChatPage.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-04-18 15:31:38 +02:00
import React, { useState, useEffect, useRef } from "react";
2023-04-18 03:30:50 +02:00
import { useSearchParams, useParams } from "react-router-dom";
2023-03-30 15:15:54 +02:00
import "./style.css";
import ChatBox from "./ChatBox.js";
import FeedbackForm from "./FeedbackForm.js";
2023-04-18 03:30:50 +02:00
const ChatPage = () => {
2023-03-30 15:15:54 +02:00
const [needForm, setNeedForm] = useState(false);
2023-04-18 22:31:43 +02:00
2023-04-18 03:30:50 +02:00
//const { token } = useParams();
2023-04-18 14:01:39 +02:00
const backendUrl = '/api';
2023-04-18 03:30:50 +02:00
const [searchParams, setSearchParams] = useSearchParams();
const token = searchParams.get("gcube-token") || '';
2023-03-30 15:15:54 +02:00
const [messages, setMessages] = useState([]);
2023-04-18 15:31:38 +02:00
const tick = useRef()
const [timer, setTimer] = useState(0);
2023-04-18 22:31:43 +02:00
const [stat, setStat] = useState("start");
2023-08-03 11:18:06 +02:00
const [err, setErr] = useState("");
2023-04-18 22:31:43 +02:00
const version = process.env.REACT_APP_VERSION_INFO;
if (!version) {
console.info("No version information present at build time.");
}else{console.info('version: ${version}');}
2023-08-29 13:27:11 +02:00
2023-03-30 15:15:54 +02:00
2023-04-18 14:01:39 +02:00
useEffect(() => {
2023-04-18 22:31:43 +02:00
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);
2023-08-03 11:18:06 +02:00
setErr(r.err);
2023-04-18 22:31:43 +02:00
}).catch((error) => {
console.error('Error:', error);
});
}
if (stat==="start"){
2023-04-18 15:31:38 +02:00
tick.current = setInterval(() => {
setTimer((timer) => timer + 1);
}, 1000);
2023-04-19 15:52:25 +02:00
if(timer%30 == 0){
2023-04-18 15:31:38 +02:00
fetch(backendUrl + '/dm', {
2023-04-18 14:01:39 +02:00
method: 'POST',
2023-04-18 22:31:43 +02:00
body: JSON.stringify({ stat: stat, token: token }),
2023-04-18 14:01:39 +02:00
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
2023-04-18 15:31:38 +02:00
}).then(r => r.json()).then(r => {
2023-04-18 22:31:43 +02:00
setStat(r.stat);
2023-08-03 11:18:06 +02:00
setErr(r.err);
2023-04-18 15:31:38 +02:00
}).catch((error) => {
console.error('Error:', error);
});
setTimer((timer) => 0);
clearInterval(tick.current);
}
}
2023-04-18 22:31:43 +02:00
2023-04-18 15:31:38 +02:00
return () => clearInterval(tick.current);
2023-04-18 22:31:43 +02:00
}, [stat, timer]);
2023-04-18 14:01:39 +02:00
2023-03-30 15:15:54 +02:00
function updateNeedForm(value) {
setNeedForm(value);
}
2023-04-19 15:44:41 +02:00
2023-04-19 17:12:27 +02:00
function updateMessages(msg) {
setMessages((prevMessages) => [msg, ...prevMessages]);
2023-03-30 15:15:54 +02:00
}
return (
<div>
2023-08-03 11:18:06 +02:00
{stat==="waiting" && <p>Loading resources + {token} ...</p>}
{stat==="start" && <p>Connecting to Server + {token} ...</p>}
{stat==="rejected" && <p>Unauthorized Use + {token} ...</p>}
{stat==="init_dm_error" && <p> {err} + {token} </p>}
2023-04-18 22:31:43 +02:00
2023-04-19 17:03:48 +02:00
{stat==="done" && <ChatBox props={{ updateNeedForm, messages, updateMessages, token}} /> }
2023-04-18 22:31:43 +02:00
{stat==="done" && needForm && <FeedbackForm props={{ updateNeedForm, messages }} /> }
2023-03-30 15:15:54 +02:00
</div>
);
};
export default ChatPage;