2024-10-20 17:27:02 +02:00
|
|
|
import React, {useEffect, useState} 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";
|
2024-10-19 19:52:28 +02:00
|
|
|
import './styles/index.css'
|
2024-10-20 17:27:02 +02:00
|
|
|
import {Job} from "./modules/Job";
|
2024-10-20 20:12:27 +02:00
|
|
|
import IFrontendSettings, {FrontendSettingsWith} from "./modules/interfaces/IFrontendSettings";
|
|
|
|
import {useCookies} from "react-cookie";
|
2024-10-18 02:10:58 +02:00
|
|
|
|
|
|
|
export default function App(){
|
2024-10-20 20:12:27 +02:00
|
|
|
const [, setCookie] = useCookies(['apiUri', 'jobInterval']);
|
2024-10-19 19:52:28 +02:00
|
|
|
const [connected, setConnected] = React.useState(false);
|
|
|
|
const [showSearch, setShowSearch] = React.useState(false);
|
2024-10-20 20:12:27 +02:00
|
|
|
const [frontendSettings, setFrontendSettings] = useState<IFrontendSettings>(FrontendSettingsWith(undefined, undefined, undefined));
|
|
|
|
const [updateInterval, setUpdateInterval] = React.useState<number>();
|
|
|
|
|
|
|
|
const apiUri = frontendSettings.apiUri;
|
2024-10-19 16:28:49 +02:00
|
|
|
|
2024-10-18 02:20:22 +02:00
|
|
|
useEffect(() => {
|
2024-10-20 20:12:27 +02:00
|
|
|
checkConnection(apiUri).then(res => setConnected(res)).catch(() => setConnected(false));
|
|
|
|
if(updateInterval === undefined){
|
|
|
|
setUpdateInterval(setInterval(() => {
|
|
|
|
checkConnection(apiUri).then(res => setConnected(res)).catch(() => setConnected(false));
|
|
|
|
}, 500));
|
|
|
|
}else{
|
|
|
|
clearInterval(updateInterval);
|
|
|
|
setUpdateInterval(undefined);
|
|
|
|
}
|
|
|
|
}, [frontendSettings]);
|
|
|
|
|
|
|
|
function ChangeSettings(settings: IFrontendSettings) {
|
|
|
|
setFrontendSettings(settings);
|
|
|
|
setCookie('apiUri', settings.apiUri);
|
|
|
|
setCookie('jobInterval', settings.jobInterval);
|
|
|
|
}
|
2024-10-20 16:06:21 +02:00
|
|
|
|
2024-10-20 17:27:02 +02:00
|
|
|
function CreateJob(internalId: string, jobType: string){
|
2024-10-20 20:12:27 +02:00
|
|
|
Job.CreateJobDateInterval(apiUri, internalId, jobType, frontendSettings.jobInterval);
|
2024-10-19 19:52:28 +02:00
|
|
|
}
|
|
|
|
|
2024-10-18 02:20:22 +02:00
|
|
|
return(<div>
|
2024-10-20 20:12:27 +02:00
|
|
|
<Header apiUri={apiUri} backendConnected={connected} settings={frontendSettings} changeSettings={ChangeSettings}/>
|
2024-10-19 19:52:28 +02:00
|
|
|
{connected
|
|
|
|
? <>
|
|
|
|
{showSearch
|
|
|
|
? <>
|
2024-10-20 17:27:02 +02:00
|
|
|
<Search createJob={CreateJob} closeSearch={() => setShowSearch(false)} />
|
2024-10-19 19:52:28 +02:00
|
|
|
<hr/>
|
|
|
|
</>
|
|
|
|
: <></>}
|
2024-10-20 20:12:27 +02:00
|
|
|
<MonitorJobsList apiUri={apiUri} onStartSearch={() => setShowSearch(true)} onJobsChanged={() => console.info("jobsChanged")} connectedToBackend={connected} />
|
2024-10-19 19:52:28 +02:00
|
|
|
</>
|
|
|
|
: <h1>No connection to backend</h1>}
|
2024-10-20 20:12:27 +02:00
|
|
|
<Footer apiUri={apiUri} connectedToBackend={connected} />
|
2024-10-18 02:20:22 +02:00
|
|
|
</div>)
|
2024-10-18 02:10:58 +02:00
|
|
|
}
|
|
|
|
|
2024-10-19 19:52:28 +02:00
|
|
|
export function getData(uri: string) : Promise<object> {
|
2024-10-18 02:10:58 +02:00
|
|
|
return fetch(uri,
|
|
|
|
{
|
2024-10-18 19:42:17 +02:00
|
|
|
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){
|
2024-10-18 19:42:17 +02:00
|
|
|
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){
|
2024-10-19 19:52:28 +02:00
|
|
|
if(!response.ok)
|
|
|
|
throw new Error("Could not fetch data");
|
|
|
|
let json = response.json();
|
|
|
|
return json.then((json) => json).catch(() => null);
|
2024-10-18 19:42:17 +02:00
|
|
|
})
|
|
|
|
.catch(function(err){
|
|
|
|
console.error(`Error POSTing Data ${uri}\n${err}`);
|
|
|
|
return Promise.reject();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-19 19:52:28 +02:00
|
|
|
export function deleteData(uri: string) : Promise<void> {
|
|
|
|
return fetch(uri,
|
2024-10-18 19:42:17 +02:00
|
|
|
{
|
|
|
|
method: 'DELETE',
|
|
|
|
headers : {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Accept': 'application/json'
|
|
|
|
}
|
|
|
|
})
|
2024-10-19 19:52:28 +02:00
|
|
|
.then(() =>{
|
|
|
|
return Promise.resolve();
|
|
|
|
})
|
2024-10-18 19:42:17 +02:00
|
|
|
.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;
|
|
|
|
}
|
2024-10-20 17:27:02 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 20:12:27 +02:00
|
|
|
export const checkConnection = async (apiUri: string): Promise<boolean> =>{
|
|
|
|
return getData(`${apiUri}/v2/Ping`).then((result) => {
|
2024-10-20 17:27:02 +02:00
|
|
|
return result != null;
|
|
|
|
}).catch(() => Promise.reject());
|
2024-10-18 02:10:58 +02:00
|
|
|
}
|