feat: Moved the JS out of the signup page into its own file

Related to #9
This commit is contained in:
silver 2024-01-01 09:24:48 +00:00
parent da7adea36e
commit 68953f0d5f
4 changed files with 105 additions and 85 deletions

View file

@ -15,7 +15,7 @@
<main class="page-body"> <main class="page-body">
<section> <section>
<h2> <h2>
<a href="./signup.html">Sign-up</a> <a href="signup/index.html">Sign-up</a>
</h2> </h2>
<p> <p>
Please use this if you have yet to activate an account on Skynet before. Please use this if you have yet to activate an account on Skynet before.

View file

@ -1,84 +0,0 @@
<!DOCTYPE html>
<html lang="en-ie">
<head>
<meta charset="UTF-8"/>
<title>Skynet Sign-up</title>
<link href="images/favicon/favicon-16x16.png" rel="icon" type="image/png"/>
<link href="stylesheets/index.css" rel="stylesheet" type="text/css"/>
<style>
/* Taken from the W3 schools loader tutorial */
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 60px;
height: 60px;
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 20s linear;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div class="wrapper">
<header class="page-header">
<img alt="Sharky, our mascot" height="81.56" src="./images/sharky.svg" width="145"/>
<h1>Skynet Self Service</h1>
</header>
<main class="page-body">
<h2>Welcome to Skynet</h2>
<p>
If you previously had an account please <a href="modify/index.html">set your email</a> to be the same as UL Wolves.
<br/>
If you are a new user, please fill in the form below with the email that you use on ul wolves.
</p>
<form id="register">
<label for="mail">Email address</label>
<input id="mail" name="email" type="email"/> <br/>
<input type="submit" value="Submit"/>
</form>
<p id="formStatus"></p>
<script>
const formEl = document.getElementById('register');
formEl.addEventListener('submit', (listener) => formHandler(listener));
function formHandler(listener) {
listener.preventDefault();
//HTML below taken from the W3 schools tutorial ()
document.getElementById('formStatus').innerHTML = "<div class='loader'></div>"
const formData = new FormData(formEl);
const email = formData.get("email");
const object = {email: email};
fetch('https://api.account.skynet.ie/ldap/new/email', {
method: 'POST',
body: JSON.stringify(object),
mode: "cors"
}).then(res => {
if (res.status === 200) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success</span>";
} else if (res.status === 500) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
} else {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure: Failed to communicate to server</span>";
}
}).catch(() => document.getElementById('formStatus').innerHTML = "<span style='background-color: yellow; color: black'>Please try again</span>");
}
</script>
</main>
<footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span>
<script>
document.getElementById('year').textContent = new Date().getFullYear().toString()
</script>
</footer>
</div>
</body>
</html>

59
src/signup/index.html Normal file
View file

@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en-ie">
<head>
<meta charset="UTF-8"/>
<title>Skynet Sign-up</title>
<link href="../images/favicon/favicon-16x16.png" rel="icon" type="image/png"/>
<link href="../stylesheets/index.css" rel="stylesheet" type="text/css"/>
<style>
/* Taken from the W3 schools loader tutorial */
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 60px;
height: 60px;
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 20s linear;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div class="wrapper">
<header class="page-header">
<img alt="Sharky, our mascot" height="81.56" src="../images/sharky.svg" width="145"/>
<h1>Skynet Self Service</h1>
</header>
<main class="page-body">
<h2>Welcome to Skynet</h2>
<p>
If you previously had an account please <a href="../modify/index.html">set your email</a> to be the same as UL Wolves.
<br/>
If you are a new user, please fill in the form below with the email that you use on ul wolves.
</p>
<form id="form">
<label for="mail">Email address</label>
<input id="mail" name="email" type="email"/> <br/>
<input id="button" type="submit" value="Submit"/>
</form>
<p id="formStatus"></p>
<script src="signup.js"></script>
</main>
<footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span>
<script>
document.getElementById('year').textContent = new Date().getFullYear().toString()
</script>
</footer>
</div>
</body>
</html>

45
src/signup/signup.js Normal file
View file

@ -0,0 +1,45 @@
const form = document.getElementById("form");
form.addEventListener('submit', formHandler);
const form_status = document.getElementById("formStatus");
const button = document.getElementById("button");
button.addEventListener('submit', formHandler);
async function formHandler(listener) {
listener.preventDefault();
//HTML below taken from the W3 schools tutorial ()
form_status.innerHTML = "<div class='loader'></div>"
const formData = new FormData(form);
const email = formData.get("email");
const body = {email: email};
let url = 'https://api.account.skynet.ie/ldap/new/email';
let req;
try {
req = await fetch(url, {
method: 'POST',
body: JSON.stringify(body),
mode: "cors"
});
} catch (e) {
form_status.innerHTML = `<span style='background-color: red; color: white'>${e}</span>`;
return;
}
if (req.status !== 200) {
form_status.innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
return;
}
let temp = await req.json();
if (temp.result === 'error') {
form_status.innerHTML = `<span style='background-color: red; color: white'>${temp.error}</span>`;
} else {
form_status.innerHTML = "<span style='background-color: green; color: white'>Success</span>";
}
}