watch-party/frontend/lib/join-session.mjs

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-12-03 20:28:36 +00:00
import { joinSession } from "./watch-session.mjs?v=5";
2021-11-09 13:21:14 +00:00
/**
* @param {HTMLInputElement} field
*/
const loadNickname = (field) => {
try {
const savedNickname = localStorage.getItem("watch-party-nickname");
field.value = savedNickname;
} catch (_err) {
// Sometimes localStorage is blocked from use
}
};
/**
* @param {HTMLInputElement} field
*/
const saveNickname = (field) => {
try {
localStorage.setItem("watch-party-nickname", field.value);
} catch (_err) {
// see loadNickname
}
};
2021-11-11 17:26:30 +00:00
const displayPostCreateMessage = () => {
const params = new URLSearchParams(window.location.search);
if (params.get("created") == "true") {
document.querySelector("#post-create-message").style["display"] = "block";
window.history.replaceState({}, document.title, `/${window.location.hash}`);
}
};
2021-11-09 13:21:14 +00:00
export const setupJoinSessionForm = () => {
2021-11-11 17:26:30 +00:00
displayPostCreateMessage();
2021-11-09 13:21:14 +00:00
const form = document.querySelector("#join-session-form");
const nickname = form.querySelector("#join-session-nickname");
const sessionId = form.querySelector("#join-session-id");
2021-12-03 19:37:16 +00:00
const button = form.querySelector("#join-session-button");
2021-11-09 13:21:14 +00:00
loadNickname(nickname);
if (window.location.hash.match(/#[0-9a-f\-]+/)) {
sessionId.value = window.location.hash.substring(1);
}
2021-12-03 19:37:16 +00:00
form.addEventListener("submit", (event) => {
event.preventDefault();
2021-11-09 13:21:14 +00:00
2021-12-03 19:37:16 +00:00
button.disabled = true;
saveNickname(nickname);
joinSession(nickname.value, sessionId.value);
});
2021-11-09 13:21:14 +00:00
};