121 lines
3.2 KiB
JavaScript
121 lines
3.2 KiB
JavaScript
import { authedAPIRequest } from "../utils/auth";
|
|
import { subscribe } from "../utils/streams";
|
|
import { resetValues } from "./canvas/utils";
|
|
// FIXME: This file should handle the rooms API
|
|
// Functions may include:
|
|
// - fetchRoomConfig (get the configuration of a room)
|
|
// - setCurrentRoomConfig (set the current room configuration and update the DOM accordingly)
|
|
// - getCurrentRoomConfig (get the current room configuration)
|
|
// - joinRoom (join a room by its slug)
|
|
// - listRooms (list all the rooms available)
|
|
// - createRoom (create a room)
|
|
// - updateRoom (update a room's configuration)
|
|
// - deleteRoom (delete a room)
|
|
|
|
let roomConfig = null;
|
|
|
|
function setCurrentRoomConfig(cfg) {
|
|
roomConfig = cfg;
|
|
}
|
|
async function getCurrentRoomConfig(room) {
|
|
if (!roomConfig) {
|
|
await fetchRoomConfig(room);
|
|
}
|
|
|
|
return roomConfig;
|
|
}
|
|
async function joinRoom(room) {
|
|
// socket.on('connection', (sockett) => {
|
|
// sockett.join(room);
|
|
// broadcast to all connected clients in the room
|
|
// socket.to('some room').emit('hello', 'world');
|
|
// broadcast to all connected clients except those in the room
|
|
// socket.except('some room').emit('hello', 'world');
|
|
// });
|
|
|
|
resetValues();
|
|
subscribe(room);
|
|
}
|
|
|
|
// async function leaveRoom(room) {
|
|
// socket.on('connection', (socket) => {
|
|
// socket.leave(room);
|
|
// })
|
|
// }
|
|
|
|
async function listRooms() {
|
|
const response = await authedAPIRequest("/rooms/", { method: "GET" });
|
|
|
|
if (!response || !response.ok) {
|
|
console.error(
|
|
"Could not retrieve rooms list: " + response && response.statusText
|
|
? response.statusText
|
|
: "null",
|
|
);
|
|
console.debug(await response.text());
|
|
return;
|
|
}
|
|
|
|
const res = await response.json();
|
|
|
|
console.debug("Retrieved rooms list");
|
|
console.debug(res);
|
|
|
|
// Update HTML
|
|
// const roomNameElt = document.getElementById("room-name");
|
|
// roomNameElt.innerText = res.metadata.name;
|
|
}
|
|
// async function createRoom() {
|
|
|
|
// }
|
|
// async function updateRoom() {
|
|
|
|
// }
|
|
// async function deleteRoom() {
|
|
|
|
// }
|
|
|
|
async function fetchRoomConfig(room) {
|
|
if (!room) {
|
|
console.error("Cannot fetch an undefined room");
|
|
return;
|
|
}
|
|
|
|
const response = await authedAPIRequest("/rooms/" + room + "/config", {
|
|
method: "GET",
|
|
});
|
|
|
|
if (!response || !response.ok) {
|
|
console.error(
|
|
"Could not retrieve room config" + response && response.statusText
|
|
? response.statusText
|
|
: "null",
|
|
);
|
|
console.debug(await response.text());
|
|
return;
|
|
}
|
|
|
|
const res = await response.json();
|
|
|
|
console.debug(`Retrieved config for room ${room}:`);
|
|
console.debug(res);
|
|
|
|
setCurrentRoomConfig(res);
|
|
|
|
// Update HTML
|
|
document.getElementById("room-name").innerText = res.metadata.name;
|
|
document.getElementById("room-description").innerText =
|
|
res.metadata.description;
|
|
document.getElementById("room-description").style.display = "block";
|
|
}
|
|
|
|
export {
|
|
fetchRoomConfig,
|
|
setCurrentRoomConfig,
|
|
getCurrentRoomConfig,
|
|
joinRoom,
|
|
listRooms,
|
|
// createRoom,
|
|
// updateRoom,
|
|
// deleteRoom
|
|
};
|