SpotifyRoulette/src/main/resources/public/js/join-game.js

25 lines
742 B
JavaScript

// public/js/join.js
import { getParam, fetchJson } from "./utils.js";
const username = getParam("username");
document.getElementById("joinForm").addEventListener("submit", async e => {
e.preventDefault();
const gameId = document.getElementById("gameId").value;
if (!username) {
alert("Kein Username gefunden!");
return;
}
try {
await fetchJson("/api/join-game", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ gameId, username })
});
window.location.href = `/game.html?gameId=${gameId}&username=${encodeURIComponent(username)}`;
} catch (err) {
alert(`Fehler: ${err.message}`);
}
});