From d04b81fbaf9e8b019fc7c5012e3efa7a831d35eb Mon Sep 17 00:00:00 2001 From: Max Wolfs Date: Fri, 12 Jan 2024 16:40:52 +0100 Subject: [PATCH] fix loading Signed-off-by: Max Wolfs --- src/components/ArchitecturalModel.tsx | 35 ++++++++++++++++++++++----- src/pages/index.tsx | 22 ++++++++++++----- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/components/ArchitecturalModel.tsx b/src/components/ArchitecturalModel.tsx index 3e061202ca..017996b1e2 100644 --- a/src/components/ArchitecturalModel.tsx +++ b/src/components/ArchitecturalModel.tsx @@ -23,17 +23,40 @@ const ArchitecturalModel: React.FunctionComponent = ({ jsonFilePath }) => { const [data, setData] = useState(null) + const [isLoading, setIsLoading] = useState(true) + const [error, setError] = useState(null) useEffect(() => { - fetch(jsonFilePath) // Use the jsonFilePath prop - .then((response) => response.json()) - .then((data: ArchitecturalLayerData) => setData(data)) - .catch((error) => console.error('Error fetching data:', error)) - }, [jsonFilePath]) // Dependency array includes jsonFilePath + const fetchData = async () => { + try { + const response = await fetch(jsonFilePath) + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`) + } + const data = (await response.json()) as ArchitecturalLayerData + setData(data) + } catch (err) { + setError(err.message) + } finally { + setIsLoading(false) + } + } - if (!data) { + fetchData() + }, [jsonFilePath]) + + if (isLoading) { return
Loading...
} + + if (error) { + return
Error: {error}
+ } + + if (!data) { + return
No data available.
+ } + return (
diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 2818c00304..768727c7ce 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -29,13 +29,23 @@ export default function Home(): JSX.Element { const [additionalResources, setAdditionalResources] = useState([]) useEffect(() => { - fetch('/data/featureContentData.json') - .then((response) => response.json()) - .then((data) => setFeatureContent(data)) + const fetchData = async () => { + try { + const featureResponse = await fetch('/data/featureContentData.json') + const featureData = await featureResponse.json() + setFeatureContent(featureData) - fetch('/data/additionalResourcesData.json') - .then((response) => response.json()) - .then((data) => setAdditionalResources(data)) + const resourcesResponse = await fetch( + '/data/additionalResourcesData.json' + ) + const resourcesData = await resourcesResponse.json() + setAdditionalResources(resourcesData) + } catch (error) { + console.error('Error fetching data:', error) + } + } + + fetchData() }, []) return (