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

Related to #9
This commit is contained in:
silver 2024-01-01 08:59:05 +00:00
parent 520464b73d
commit a7bde90cb8
6 changed files with 123 additions and 111 deletions

View file

@ -31,7 +31,7 @@
</section> </section>
<section> <section>
<h2> <h2>
<a href="./modify.html">Account modification</a> <a href="modify/index.html">Account modification</a>
</h2> </h2>
<p> <p>
Please use this service to modify your user data (email, login ssh key, etc.) Please use this service to modify your user data (email, login ssh key, etc.)

View file

@ -1,108 +0,0 @@
<!DOCTYPE html>
<html lang="en-ie">
<head>
<meta charset="UTF-8"/>
<title>Modify Account</title>
<link href="./images/favicon/favicon-16x16.png" rel="icon" type="image/png"/>
<link href="./stylesheets/index.css" rel="stylesheet" type="text/css"/>
</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>User modification page</h2>
<p>
Modify details of your Skynet account.
</p>
<form id="reset">
<table id="table">
<tr>
<td><label for="user">Username</label></td>
<td><input id="user" name="user" type="text"/> <br/></td>
</tr>
<tr>
<td><label for="pass">Password</label></td>
<td><input id="pass" name="pass" type="password"/> <br/></td>
</tr>
<tr>
<td><label for="field">Field</label></td>
<td>
<select id="field" onchange="selectField()">
<option selected="selected" value="">Please select an option</option>
<option value="mail">Email</option>
<option value="sshPublicKey">SSH key</option>
<option value="cn">First name & Surname</option>
<option value="sn">Surname</option>
<option value="skDiscord">Discord username</option>
</select>
</td>
</tr>
<tr>
<td><label for="value">Value</label></td>
<td><input id="value" name="value" type="text"/> <br/></td>
</tr>
<tr>
<td colspan="2"><input type="Submit" value="Submit"/></td>
</tr>
</table>
</form>
<p id="formStatus"></p>
<script>
const formEl = document.getElementById("reset");
formEl.addEventListener('submit', (listener) => formHandler(listener));
function formHandler(listener) {
listener.preventDefault();
const formData = new FormData(formEl);
const field = document.getElementById('field').value;
if (field !== "") {
const user = formData.get("user");
const pass = formData.get("pass");
const value = formData.get("value");
const object = {auth: {user: user, pass: pass}, field: field, value: value}
fetch('https://api.account.skynet.ie/ldap/update', {
method: 'POST',
body: JSON.stringify(object),
mode: "cors"
}).then(res => {
if (res.status === 200) {
let temp = res.json();
if (temp.result === 'error') {
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>${temp.error}</span>`;
} else {
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>");
} else {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Please select a field to modify</span>";
}
}
function selectField() {
const field = document.getElementById('field').value;
const value = document.getElementById('value');
if (field === 'mail') {
value.type = 'email';
} else {
value.type = 'text';
}
}
</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>

62
src/modify/index.html Normal file
View file

@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en-ie">
<head>
<meta charset="UTF-8"/>
<title>Modify Account</title>
<link href="../images/favicon/favicon-16x16.png" rel="icon" type="image/png"/>
<link href="../stylesheets/index.css" rel="stylesheet" type="text/css"/>
</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>User modification page</h2>
<p>
Modify details of your Skynet account.
</p>
<form id="form">
<table id="table">
<tr>
<td><label for="user">Username</label></td>
<td><input id="user" name="user" type="text"/> <br/></td>
</tr>
<tr>
<td><label for="pass">Password</label></td>
<td><input id="pass" name="pass" type="password"/> <br/></td>
</tr>
<tr>
<td><label for="dropdown">Field</label></td>
<td>
<select id="dropdown">
<option selected="selected" value="">Please select an option</option>
<option value="mail">Email</option>
<option value="sshPublicKey">SSH key</option>
<option value="cn">First name & Surname</option>
<option value="sn">Surname</option>
</select>
</td>
</tr>
<tr>
<td><label for="value">Value</label></td>
<td><input id="value" name="value" type="text"/> <br/></td>
</tr>
<tr>
<td colspan="2"><input type="Submit" value="Submit" id="button"/></td>
</tr>
</table>
</form>
<p id="formStatus"></p>
<script src="modify.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>

58
src/modify/modify.js Normal file
View file

@ -0,0 +1,58 @@
const form = document.getElementById("form");
form.addEventListener('submit', formHandler);
const button = document.getElementById("button");
button.addEventListener('submit', formHandler);
const dropdown = document.getElementById("dropdown");
dropdown.addEventListener('onchange', selectField);
async function formHandler(listener) {
listener.preventDefault();
const formData = new FormData(form);
const dropdown_value = dropdown.value;
if (dropdown_value === "") {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Please select a field to modify</span>";
return;
}
const user = formData.get("user");
const pass = formData.get("pass");
const value = formData.get("value");
const body = {auth: {user: user, pass: pass}, field: dropdown_value, value: value};
let url = 'https://api.account.skynet.ie/ldap/update';
let req;
try {
req = await fetch(url, {
method: 'POST',
body: JSON.stringify(body),
mode: "cors"
});
} catch (e) {
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>${e}</span>`;
return;
}
if (req.status !== 200) {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
return;
}
let temp = await req.json();
if (temp.result === 'error') {
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>${temp.error}</span>`;
} else {
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success</span>";
}
}
function selectField() {
if (dropdown.value === 'mail') {
document.getElementById('value').type = 'email';
} else {
document.getElementById('value').type = 'text';
}
}

View file

@ -73,7 +73,7 @@
return; return;
} }
let data = req.json(); let data = await req.json();
if (data.result === 'error') { if (data.result === 'error') {
document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>${data.error}</span>`; document.getElementById('formStatus').innerHTML = `<span style='background-color: red; color: white'>${data.error}</span>`;
} else { } else {

View file

@ -36,7 +36,7 @@
<main class="page-body"> <main class="page-body">
<h2>Welcome to Skynet</h2> <h2>Welcome to Skynet</h2>
<p> <p>
If you previously had an account please <a href="./modify.html">set your email</a> to be the same as UL Wolves. If you previously had an account please <a href="modify/index.html">set your email</a> to be the same as UL Wolves.
<br/> <br/>
If you are a new user, please fill in the form below with the email that you use on ul wolves. If you are a new user, please fill in the form below with the email that you use on ul wolves.
</p> </p>