show error state when room api is not reachable
This commit is contained in:
parent
9a335a8c6b
commit
35947778ce
|
@ -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")
|
||||
fetch('https://status.ctdo.de/api/simple/v2')
|
||||
.then(response => {
|
||||
if (response.status >= 200 && response.status <= 299) {
|
||||
return response
|
||||
} else {
|
||||
throw new Error()
|
||||
}
|
||||
fetchState()
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(json => setOpenState(json.state ? 'open' : 'closed'))
|
||||
.catch(() => setOpenState('error'))
|
||||
}, [])
|
||||
|
||||
return (
|
||||
|
|
Loading…
Reference in New Issue