mirror of
https://github.com/AlecM33/Werewolf.git
synced 2026-01-01 16:59:29 +01:00
webpack
This commit is contained in:
47
client/src/scripts/home.js
Normal file
47
client/src/scripts/home.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import { XHRUtility } from "../modules/XHRUtility.js";
|
||||
import { toast } from "../modules/Toast.js";
|
||||
|
||||
const home = () => {
|
||||
document.getElementById("join-form").onsubmit = (e) => {
|
||||
e.preventDefault();
|
||||
let userCode = document.getElementById("room-code").value;
|
||||
if (roomCodeIsValid(userCode)) {
|
||||
attemptToJoinGame(userCode);
|
||||
} else {
|
||||
toast('Invalid code. Codes are 6 numbers or letters.', 'error', true, true);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function roomCodeIsValid(code) {
|
||||
return typeof code === "string" && /^[a-z0-9]{6}$/.test(code.toLowerCase());
|
||||
}
|
||||
|
||||
function attemptToJoinGame(code) {
|
||||
XHRUtility.xhr(
|
||||
'/api/games/availability/' + code.toLowerCase().trim(),
|
||||
'GET',
|
||||
null,
|
||||
null
|
||||
)
|
||||
.then((res) => {
|
||||
if (res.status === 200) {
|
||||
window.location = '/game/' + res.content;
|
||||
}
|
||||
}).catch((res) => {
|
||||
if (res.status === 404) {
|
||||
toast("Game not found", "error", true);
|
||||
} else if (res.status === 400) {
|
||||
toast(res.content, "error", true);
|
||||
} else {
|
||||
toast("An unknown error occurred. Please try again later.", "error", true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
||||
module.exports = home;
|
||||
} else {
|
||||
home();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user