Tranga-Website/Website/App.tsx

100 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-10-18 02:20:22 +02:00
import React, {ReactElement, 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/Manga.css'
2024-10-18 02:10:58 +02:00
export default function App(){
2024-10-18 02:20:22 +02:00
const [content, setContent] = React.useState<ReactElement>();
2024-10-19 16:28:49 +02:00
function ShowSearch() {
setContent(<>
<Search />
<MonitorJobsList onStartSearch={ShowSearch} />
</>);
}
2024-10-18 02:20:22 +02:00
useEffect(() => {
setContent(<h1>Testing connection to backend...</h1>)
getData('http://127.0.0.1:6531/v2/Ping').then((result) => {
console.log(result);
if(result === null){
setContent(<h1>No connection to backend</h1>);
}else{
2024-10-18 19:46:17 +02:00
setContent(<>
2024-10-19 16:28:49 +02:00
<MonitorJobsList onStartSearch={ShowSearch} />
2024-10-18 19:46:17 +02:00
</>)
2024-10-18 02:20:22 +02:00
}
})
}, []);
return(<div>
2024-10-18 02:10:58 +02:00
<Header />
2024-10-18 02:20:22 +02:00
{content}
2024-10-18 02:10:58 +02:00
<Footer />
2024-10-18 02:20:22 +02:00
</div>)
2024-10-18 02:10:58 +02:00
}
export function getData (uri: string) : Promise<object> {
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");
return response.json();
})
.catch(function(err){
console.error(`Error POSTing Data ${uri}\n${err}`);
return Promise.reject();
});
}
export function deleteData(uri: string) {
fetch(uri,
{
method: 'DELETE',
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.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;
}
}