newly added club status, currently disabled until spaceapi is set public
This commit is contained in:
parent
6281666093
commit
1e245cf6cd
4 changed files with 161 additions and 0 deletions
67
assets/js/clubstatus.js
Normal file
67
assets/js/clubstatus.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
const spaceapiUrl = "https://spaceapi.hamburg.ccc.de"
|
||||
const interval_ms = 60000
|
||||
|
||||
console.log("clubstatus.js geladen!"); /* TODO: ENTFERNEN */
|
||||
|
||||
function timeDistanceDE(pastDate) {
|
||||
const seconds = Math.floor((Date.now() - pastDate.getTime()) / 1000)
|
||||
const minutes = Math.floor(seconds / 60)
|
||||
const hours = Math.floor(minutes / 60)
|
||||
const days = Math.floor(hours / 24)
|
||||
|
||||
if (days = 1) return `since ${days}d`
|
||||
if (days > 1) return `since ${days}d`
|
||||
if (hours = 1) return `since ${hours}h`
|
||||
if (hours > 1) return `since ${hours}h`
|
||||
if (minutes = 1) return `since ${minutes}min`
|
||||
if (minutes > 1) return `since ${minutes}min`
|
||||
return `gerade eben`
|
||||
}
|
||||
|
||||
function updateRoomState(openState, lastChangeTimestamp) {
|
||||
const roomstate = document.querySelector('#clubstatus-badge')
|
||||
const statusItem = roomstate.querySelector(".state")
|
||||
const durationItem = roomstate.querySelector(".duration")
|
||||
|
||||
if (!statusItem || !durationItem) return
|
||||
|
||||
roomstate.classList.remove("open", "closed", "unknown")
|
||||
statusItem.classList.remove("open", "closed", "unknown")
|
||||
|
||||
if (openState === null || lastChangeTimestamp === null) {
|
||||
statusItem.textContent = "unbekannt"
|
||||
statusItem.classList.add("unknown")
|
||||
durationItem.textContent = "(since unbekannt)"
|
||||
} else {
|
||||
const changeDate = new Date(lastChangeTimestamp * 1000)
|
||||
const sinceText = timeDistanceDE(changeDate)
|
||||
|
||||
if (openState) {
|
||||
statusItem.textContent = "open"
|
||||
statusItem.classList.add("open")
|
||||
} else {
|
||||
statusItem.textContent = "closed"
|
||||
statusItem.classList.add("closed")
|
||||
}
|
||||
|
||||
durationItem.textContent = `(${sinceText})`
|
||||
}
|
||||
}
|
||||
|
||||
function fetchRoomState() {
|
||||
fetch(spaceapiUrl)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const openState = data?.state?.open ?? null
|
||||
const lastChange = data?.state?.lastchange ?? null
|
||||
updateRoomState(openState, lastChange)
|
||||
})
|
||||
.catch(err => {
|
||||
console.error("Fehler beim Laden der SpaceAPI:", err)
|
||||
updateRoomState(null, null)
|
||||
})
|
||||
}
|
||||
|
||||
// Initialer Abruf + dann regelmäßig
|
||||
fetchRoomState()
|
||||
setInterval(fetchRoomState, interval_ms)
|
Loading…
Add table
Add a link
Reference in a new issue