2022-06-10 19:57:36 -07:00
|
|
|
import { createSession } from "./watch-session.mjs?v=bfdcf21";
|
2021-11-11 09:26:30 -08:00
|
|
|
|
2022-06-10 19:57:36 -07:00
|
|
|
const getFreeSessionId = async () => {
|
|
|
|
// fetch /random_unused_session
|
|
|
|
const response = await fetch("/random_unused_session");
|
|
|
|
const json = await response.json();
|
|
|
|
return json.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkIfSessionExists = async (sessionId) => {
|
|
|
|
// fetch /check_if_session_exists/:sessionId
|
|
|
|
const response = await fetch(`/check_if_session_exists/${sessionId}`);
|
|
|
|
const json = await response.json();
|
|
|
|
return json.exists;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const setupCreateSessionForm = async () => {
|
2021-11-11 09:26:30 -08:00
|
|
|
const form = document.querySelector("#create-session-form");
|
|
|
|
const videoUrl = form.querySelector("#create-session-video");
|
|
|
|
const subsUrl = form.querySelector("#create-session-subs");
|
|
|
|
const subsName = form.querySelector("#create-session-subs-name");
|
2022-06-10 19:57:36 -07:00
|
|
|
const sessionId = form.querySelector("#create-session-id");
|
|
|
|
|
|
|
|
// get free session id
|
|
|
|
const freeSessionId = await getFreeSessionId();
|
2021-11-11 09:26:30 -08:00
|
|
|
|
2022-06-10 19:57:36 -07:00
|
|
|
// set session id to free session id
|
|
|
|
sessionId.placeholder = freeSessionId;
|
|
|
|
// sessionId.value = freeSessionId;
|
|
|
|
|
|
|
|
form.addEventListener("submit", async (event) => {
|
2021-11-11 09:26:30 -08:00
|
|
|
event.preventDefault();
|
|
|
|
|
2022-06-10 19:57:36 -07:00
|
|
|
const realSessionId = sessionId.value || sessionId.placeholder;
|
|
|
|
|
|
|
|
// check if session exists
|
|
|
|
const exists = await checkIfSessionExists(realSessionId);
|
|
|
|
if (exists) {
|
|
|
|
alert("Session already exists");
|
|
|
|
return;
|
|
|
|
}
|
2021-11-11 09:26:30 -08:00
|
|
|
let subs = [];
|
|
|
|
if (subsUrl.value) {
|
|
|
|
subs.push({ url: subsUrl.value, name: subsName.value || "default" });
|
|
|
|
}
|
2022-06-10 19:57:36 -07:00
|
|
|
createSession(realSessionId, videoUrl.value, subs);
|
2021-11-11 09:26:30 -08:00
|
|
|
});
|
2022-06-10 19:57:36 -07:00
|
|
|
}
|