Skip to content

Commit

Permalink
fix loading
Browse files Browse the repository at this point in the history
Signed-off-by: Max Wolfs <[email protected]>
  • Loading branch information
maxwolfs committed Jan 12, 2024
1 parent 63c7354 commit d04b81f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
35 changes: 29 additions & 6 deletions src/components/ArchitecturalModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,40 @@ const ArchitecturalModel: React.FunctionComponent<ArchitecturalModelProps> = ({
jsonFilePath
}) => {
const [data, setData] = useState<ArchitecturalLayerData | null>(null)
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState<string | null>(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 <div>Loading...</div>
}

if (error) {
return <div>Error: {error}</div>
}

if (!data) {
return <div>No data available.</div>
}

return (
<div className={`${styles.gradient} row`}>
<div className="col col--3">
Expand Down
22 changes: 16 additions & 6 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit d04b81f

Please sign in to comment.