feat: Moved the JS out of the modify page into its own file
Related to #9
This commit is contained in:
parent
520464b73d
commit
a7bde90cb8
6 changed files with 123 additions and 111 deletions
62
src/modify/index.html
Normal file
62
src/modify/index.html
Normal 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
58
src/modify/modify.js
Normal 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';
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue