show error state when room api is not reachable

This commit is contained in:
neri 2021-05-15 19:56:02 +02:00
parent 9a335a8c6b
commit 35947778ce
1 changed files with 17 additions and 11 deletions

View File

@ -1,21 +1,27 @@
import React, { useEffect, useState } from "react"
import React, { useEffect, useState } from 'react'
const roomStateData = {
loading: { text: "lade…", color: "white" },
open: { text: "OFFEN", color: "#00aa00" },
closed: { text: "GESCHLOSSEN", color: "#ee3333" },
loading: { text: '...', color: 'white' },
open: { text: 'offen', color: '#00aa00' },
closed: { text: 'geschlossen', color: '#ee3333' },
error: { text: 'laut API kaputt', color: '#ee3333' },
}
export default () => {
const [openState, setOpenState] = useState("loading")
const [openState, setOpenState] = useState('loading')
useEffect(() => {
async function fetchState() {
const response = await fetch("https://status.ctdo.de/api/simple/v2")
const json = await response.json()
setOpenState(json.state ? "open" : "closed")
}
fetchState()
fetch('https://status.ctdo.de/api/simple/v2')
.then(response => {
if (response.status >= 200 && response.status <= 299) {
return response
} else {
throw new Error()
}
})
.then(response => response.json())
.then(json => setOpenState(json.state ? 'open' : 'closed'))
.catch(() => setOpenState('error'))
}, [])
return (