Tranga-Website/Website/App.tsx

120 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-10-19 21:10:59 +02:00
import React, {EventHandler, useEffect} from 'react';
2024-10-18 02:10:58 +02:00
import Footer from "./modules/Footer";
import Search from "./modules/Search";
import Header from "./modules/Header";
2024-10-18 19:46:17 +02:00
import MonitorJobsList from "./modules/MonitorJobsList";
import './styles/index.css'
2024-10-18 02:10:58 +02:00
export default function App(){
const [connected, setConnected] = React.useState(false);
const [showSearch, setShowSearch] = React.useState(false);
const [lastMangaListUpdate, setLastMangaListUpdate] = React.useState<Date>(new Date());
2024-10-19 20:15:53 +02:00
const [lastJobListUpdate, setLastJobListUpdate] = React.useState<Date>(new Date());
2024-10-19 16:28:49 +02:00
2024-10-18 02:20:22 +02:00
useEffect(() => {
getData('http://127.0.0.1:6531/v2/Ping').then((result) => {
console.log(result);
if(result === null){
setConnected(false);
2024-10-18 02:20:22 +02:00
}else{
setConnected(true);
2024-10-18 02:20:22 +02:00
}
2024-10-19 20:15:53 +02:00
const interval = setInterval(() => {
setLastJobListUpdate(new Date());
2024-10-19 21:08:59 +02:00
}, 5000);
2024-10-19 20:15:53 +02:00
return () => clearInterval(interval);
});
2024-10-18 02:20:22 +02:00
}, []);
const JobsChanged : EventHandler<any> = () => {
console.log("Updating Mangalist");
setLastMangaListUpdate(new Date());
2024-10-19 20:15:53 +02:00
setLastJobListUpdate(new Date());
}
2024-10-18 02:20:22 +02:00
return(<div>
<Header/>
{connected
? <>
{showSearch
? <>
2024-10-19 20:57:03 +02:00
<Search onJobsChanged={JobsChanged} closeSearch={() => setShowSearch(false)} />
<hr/>
</>
: <></>}
2024-10-19 20:15:53 +02:00
<MonitorJobsList onStartSearch={() => setShowSearch(true)} onJobsChanged={JobsChanged} key={lastMangaListUpdate.getTime()}/>
</>
: <h1>No connection to backend</h1>}
2024-10-19 20:15:53 +02:00
<Footer key={lastJobListUpdate.getTime()} />
2024-10-18 02:20:22 +02:00
</div>)
2024-10-18 02:10:58 +02:00
}
export function getData(uri: string) : Promise<object> {
2024-10-18 02:10:58 +02:00
return fetch(uri,
{
method: 'GET',
2024-10-18 02:10:58 +02:00
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(function(response){
if(!response.ok) throw new Error("Could not fetch data");
return response.json();
})
.catch(function(err){
console.error(`Error GETting Data ${uri}\n${err}`);
return Promise.reject();
});
}
export function postData(uri: string, content: object) : Promise<object> {
return fetch(uri,
{
method: 'POST',
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(content)
})
.then(function(response){
if(!response.ok)
throw new Error("Could not fetch data");
let json = response.json();
return json.then((json) => json).catch(() => null);
})
.catch(function(err){
console.error(`Error POSTing Data ${uri}\n${err}`);
return Promise.reject();
});
}
export function deleteData(uri: string) : Promise<void> {
return fetch(uri,
{
method: 'DELETE',
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(() =>{
return Promise.resolve();
})
.catch(function(err){
console.error(`Error DELETEing Data ${uri}\n${err}`);
return Promise.reject();
2024-10-18 02:10:58 +02:00
});
}
export function isValidUri(uri: string) : boolean{
try {
new URL(uri);
return true;
} catch (err) {
return false;
}
}