Spaces:
Sleeping
Sleeping
Upload components/DataFetcher.js with huggingface_hub
Browse files- components/DataFetcher.js +58 -0
components/DataFetcher.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState } from 'react';
|
| 2 |
+
|
| 3 |
+
export default function DataFetcher() {
|
| 4 |
+
const [data, setData] = useState(null);
|
| 5 |
+
const [loading, setLoading] = useState(false);
|
| 6 |
+
const [error, setError] = useState(null);
|
| 7 |
+
|
| 8 |
+
const fetchData = async () => {
|
| 9 |
+
setLoading(true);
|
| 10 |
+
setError(null);
|
| 11 |
+
try {
|
| 12 |
+
const response = await fetch('/api/status');
|
| 13 |
+
if (!response.ok) {
|
| 14 |
+
throw new Error('Network response was not ok');
|
| 15 |
+
}
|
| 16 |
+
const result = await response.json();
|
| 17 |
+
setData(result);
|
| 18 |
+
} catch (err) {
|
| 19 |
+
setError(err.message);
|
| 20 |
+
} finally {
|
| 21 |
+
setLoading(false);
|
| 22 |
+
}
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
return (
|
| 26 |
+
<div className="bg-gray-50 dark:bg-gray-900 p-6 rounded-xl border border-gray-200 dark:border-gray-700">
|
| 27 |
+
<h3 className="text-xl font-semibold mb-4 text-gray-800 dark:text-white flex items-center gap-2">
|
| 28 |
+
API Interaction Demo
|
| 29 |
+
</h3>
|
| 30 |
+
|
| 31 |
+
<button
|
| 32 |
+
onClick={fetchData}
|
| 33 |
+
disabled={loading}
|
| 34 |
+
className="w-full sm:w-auto bg-gray-900 dark:bg-white text-white dark:text-gray-900 font-medium py-2 px-6 rounded-lg hover:bg-gray-700 dark:hover:bg-gray-200 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
| 35 |
+
>
|
| 36 |
+
{loading ? 'Loading...' : 'Check System Status'}
|
| 37 |
+
</button>
|
| 38 |
+
|
| 39 |
+
{error && (
|
| 40 |
+
<div className="mt-4 p-3 bg-red-100 border border-red-400 text-red-700 rounded-lg">
|
| 41 |
+
Error: {error}
|
| 42 |
+
</div>
|
| 43 |
+
)}
|
| 44 |
+
|
| 45 |
+
{data && !loading && (
|
| 46 |
+
<div className="mt-4 p-4 bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700">
|
| 47 |
+
<h4 className="font-bold text-green-600 dark:text-green-400 mb-2">Response Received:</h4>
|
| 48 |
+
<div className="space-y-2 text-sm font-mono text-gray-700 dark:text-gray-300">
|
| 49 |
+
<p><span className="font-bold">Status:</span> {data.status}</p>
|
| 50 |
+
<p><span className="font-bold">Message:</span> {data.message}</p>
|
| 51 |
+
<p><span className="font-bold">Timestamp:</span> {data.timestamp}</p>
|
| 52 |
+
<p><span className="font-bold">Uptime:</span> {Math.floor(data.uptime)}s</p>
|
| 53 |
+
</div>
|
| 54 |
+
</div>
|
| 55 |
+
)}
|
| 56 |
+
</div>
|
| 57 |
+
);
|
| 58 |
+
}
|