You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
import './style.scss'
|
|
|
|
export const BASE_URL = import.meta.env.BASE_URL.replace(/\/$/, '')
|
|
|
|
let USER = false
|
|
export async function getLoggedUser() {
|
|
if (USER === false) {
|
|
console.log('Caching user data...')
|
|
USER = await (await fetch(`${BASE_URL}/api/user`)).json()
|
|
}
|
|
|
|
return USER
|
|
}
|
|
|
|
export function createRoomEventStream(roomId) {
|
|
return new EventSource(`${BASE_URL}/api/room_events?id=${roomId}`)
|
|
}
|
|
|
|
export const Database = {
|
|
async getSeats(roomId) {
|
|
const seatList = await (await fetch(`${BASE_URL}/api/room/seats?id=${roomId}`)).json()
|
|
const seats = {}
|
|
|
|
seatList.forEach(seat => {
|
|
seats[seat.id] = seat
|
|
})
|
|
|
|
return seats
|
|
},
|
|
async occupySeat(seatId) {
|
|
const response = await fetch(`${BASE_URL}/api/seat/occupy?id=${seatId}`, {
|
|
method: 'POST',
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error(await response.text())
|
|
}
|
|
|
|
await response.json()
|
|
},
|
|
async leaveSeat(seatId) {
|
|
const response = await fetch(`${BASE_URL}/api/seat/leave?id=${seatId}`, {
|
|
method: 'POST',
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error(await response.text())
|
|
}
|
|
|
|
await response.json()
|
|
},
|
|
}
|