From 69c5e0397b34c54423d050403f733f72d89ad6c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Thu, 2 Nov 2023 15:43:11 +0100 Subject: [PATCH] fetch data as soon as the client connects to the websocket server --- frontend/src/App.tsx | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index cb1d28b..b52e4b9 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -126,11 +126,6 @@ const App = () => { }, [state]); useEffect(() => { - // Fetch data from the API when the component mounts - fetch(`http://${hostname}:${port}/service-properties`) - .then((response) => response.json()) - .then((data: DataServiceJSON) => dispatch({ type: 'SET_DATA', data })); - // Allow the user to add a custom css file fetch(`http://${hostname}:${port}/custom.css`) .then((response) => { @@ -145,7 +140,13 @@ const App = () => { }) .catch(console.error); // Handle the error appropriately - socket.on('connect', () => setConnectionStatus('connected')); + socket.on('connect', () => { + // Fetch data from the API when the client connects + fetch(`http://${hostname}:${port}/service-properties`) + .then((response) => response.json()) + .then((data: DataServiceJSON) => dispatch({ type: 'SET_DATA', data })); + setConnectionStatus('connected'); + }); socket.on('disconnect', () => { setConnectionStatus('disconnected'); setTimeout(() => { @@ -222,7 +223,7 @@ const App = () => { // While the data is loading if (!state) { - return

Loading...

; + return ; } return ( <>