Huge update.
This commit is contained in:
33
resource/static/js/home.js
Normal file
33
resource/static/js/home.js
Normal file
@@ -0,0 +1,33 @@
|
||||
let pendingUsername = null;
|
||||
|
||||
function getUsername() {
|
||||
const username = document.getElementById('username-input').value.trim();
|
||||
if (!username) {
|
||||
alert("Please enter your name.");
|
||||
throw new Error("Username required");
|
||||
}
|
||||
pendingUsername = username;
|
||||
return username;
|
||||
}
|
||||
|
||||
document.getElementById('create-button').addEventListener('click', async () => {
|
||||
try {
|
||||
const username = getUsername();
|
||||
const res = await fetch('/api/room', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({})
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.code) {
|
||||
const allUsernames = JSON.parse(localStorage.getItem('usernames') || '{}');
|
||||
allUsernames[data.code] = pendingUsername;
|
||||
localStorage.setItem('usernames', JSON.stringify(allUsernames));
|
||||
|
||||
window.location.href = `/room/${data.code}`;
|
||||
} else {
|
||||
alert("Failed to create room.");
|
||||
}
|
||||
} catch (_) {
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user