Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 6f8c398

Browse files
committed
Minor changes on first api call example
1 parent c7a35a4 commit 6f8c398

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
lines changed

webapp_frontend/src/api.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
import Axios from "axios";
2-
import {
3-
AxiosRequestConfig,
4-
AxiosResponse,
5-
AxiosError,
6-
} from "axios";
72

83
const uri = "http://localhost:8080";
94

105
interface BackendHealthData {
116
uptimeInSeconds: number
127
}
138

14-
function callBackendHealth():any{//BackendHealthData{
9+
function callBackendHealth():Promise<BackendHealthData>{
1510
return new Promise((resolve, reject) => {
1611
Axios.get(`${uri}/health`)
1712
.then((data) => {
1813
resolve(data.data);
1914
})
2015
.catch(((error) => {
21-
console.log(error)
22-
//reject(error);
16+
reject(error);
2317
}));
2418
});
2519
}
Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
1-
import React, {useEffect, useState} from 'react';
1+
import React, {ReactElement, useEffect, useState} from 'react';
22
import logo from '../logo.svg';
33
import './App.css';
44
import {callBackendHealth} from "../api";
55

6-
function App() {
7-
const[test,setTest] = useState("test");
6+
function App():ReactElement {
7+
const [backendLiveTime, setBackendLiveTime] = useState<number | string>("Init");
88

9-
useEffect(() => {
10-
Promise.all([callBackendHealth()])
11-
.then(([backendHealthData]) => {
12-
setTest(backendHealthData.uptimeInSeconds)
13-
})
14-
}, [test]);
9+
useEffect(() => {
10+
updateVariables()
11+
});
1512

16-
return (
17-
<div className="App">
18-
<header className="App-header">
19-
<p>
20-
Hello World
21-
</p>
22-
<img src={logo} className="App-logo" alt="logo" />
23-
<p>
24-
Edit <code>src/App.tsx</code> and save to reload.
25-
</p>
26-
<a
27-
className="App-link"
28-
href="https://reactjs.org"
29-
target="_blank"
30-
rel="noopener noreferrer"
31-
>
32-
Learn React
33-
</a>
34-
<button onClick={() => setTest(callBackendHealth().data)}>Test</button>
35-
<p>{test}</p>
36-
</header>
37-
</div>
38-
);
13+
function updateVariables(): void {
14+
Promise.all([callBackendHealth()])
15+
.then(([backendHealthData]) => {
16+
setBackendLiveTime(backendHealthData.uptimeInSeconds)
17+
})
18+
}
19+
20+
return (
21+
<div className="App">
22+
<header className="App-header">
23+
<p>
24+
Hello World
25+
</p>
26+
<img src={logo} className="App-logo" alt="logo"/>
27+
<p>
28+
Edit <code>src/App.tsx</code> and save to reload.
29+
</p>
30+
<a
31+
className="App-link"
32+
href="https://reactjs.org"
33+
target="_blank"
34+
rel="noopener noreferrer"
35+
>
36+
Learn React
37+
</a>
38+
<button style={{marginTop: "20px"}} onClick={() => updateVariables()}>Test</button>
39+
<p>{backendLiveTime}</p>
40+
</header>
41+
</div>
42+
);
3943
}
4044

4145
export default App;

0 commit comments

Comments
 (0)