-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
57 lines (45 loc) · 1.1 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { useState } from "react";
import "./App.css";
interface ProxyResponse {
authenticated: boolean;
token: string;
}
function App() {
const [proxyResponse, setProxyResponse] = useState<ProxyResponse | null>(
null,
);
const handleClick = async () => {
if (proxyResponse) {
setProxyResponse(null);
return;
}
try {
const BASE_URL = import.meta.env.VITE_PROXY_URL;
const response = await fetch(`${BASE_URL}/example`);
if (!response.ok) {
console.error("Failed to fetch data");
return;
}
const data = await response.json();
setProxyResponse(data);
} catch (error) {
console.error(error);
}
};
const buttonText = proxyResponse ? "Clear" : "Fetch data";
return (
<>
<h1>React App</h1>
<div className="card">
<button onClick={handleClick}>{buttonText}</button>
{proxyResponse && (
<>
<h4>Response from Proxy:</h4>
<pre>{JSON.stringify(proxyResponse, null, 2)}</pre>
</>
)}
</div>
</>
);
}
export default App;