feat: since most pages were posting to the api and displaying any errors its easier to turn it into a shared function for easy refactoring.

Related to #9
This commit is contained in:
silver 2024-01-01 10:06:21 +00:00
parent 4881be37e1
commit 9042ce0df9
16 changed files with 64 additions and 176 deletions

28
src/js/lib.js Normal file
View file

@ -0,0 +1,28 @@
export async function post_request(url, body, form_status){
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>";
}
}

View file

@ -49,7 +49,7 @@
</table> </table>
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
<script src="modify.js"></script> <script type="module" src="modify.js"></script>
</main> </main>
<footer class="page-footer"> <footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span> UL Computer Society 2023-<span id="year">2023</span>

View file

@ -1,6 +1,10 @@
import {post_request} from "./js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
const form_status = document.getElementById("formStatus");
const button = document.getElementById("button"); const button = document.getElementById("button");
button.addEventListener('submit', formHandler); button.addEventListener('submit', formHandler);
@ -13,7 +17,7 @@ async function formHandler(listener) {
const dropdown_value = dropdown.value; const dropdown_value = dropdown.value;
if (dropdown_value === "") { if (dropdown_value === "") {
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Please select a field to modify</span>"; form_status.innerHTML = "<span style='background-color: red; color: white'>Please select a field to modify</span>";
return; return;
} }
@ -23,30 +27,7 @@ async function formHandler(listener) {
const body = {auth: {user: user, pass: pass}, field: dropdown_value, value: value}; const body = {auth: {user: user, pass: pass}, field: dropdown_value, value: value};
let url = 'https://api.account.skynet.ie/ldap/update'; let url = 'https://api.account.skynet.ie/ldap/update';
let req; await post_request(url, body, form_status);
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() { function selectField() {

View file

@ -41,7 +41,7 @@
</table> </table>
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
<script src="password.js"></script> <script type="module" src="password.js"></script>
</main> </main>
<footer class="page-footer"> <footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span> UL Computer Society 2023-<span id="year">2023</span>

View file

@ -1,3 +1,5 @@
import {post_request} from "./js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -22,28 +24,5 @@ async function formHandler(listener) {
const body = {auth: {user: user, pass: pass}, field: "userPassword", value: newPW} const body = {auth: {user: user, pass: pass}, field: "userPassword", value: newPW}
let url = 'https://api.account.skynet.ie/ldap/update'; let url = 'https://api.account.skynet.ie/ldap/update';
let req; await post_request(url, body, form_status);
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>";
}
} }

View file

@ -15,7 +15,9 @@
<main class="page-body"> <main class="page-body">
<h2>Forgot Password</h2> <h2>Forgot Password</h2>
<p> <p>
Request a password reset sent to your linked email. <br> Enter either email address or skynet username. </br> Request a password reset sent to your linked email.
<br>
Enter either email address or skynet username.
</p> </p>
<form id="form"> <form id="form">
<table id="table"> <table id="table">
@ -36,7 +38,7 @@
</table> </table>
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
<script src="password.js"></script> <script type="module" src="password.js"></script>
</main> </main>
<footer class="page-footer"> <footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span> UL Computer Society 2023-<span id="year">2023</span>

View file

@ -33,7 +33,7 @@
</table> </table>
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
<script src="password_reset.js"></script> <script type="module" src="password_reset.js"></script>
</main> </main>
<footer class="page-footer"> <footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span> UL Computer Society 2023-<span id="year">2023</span>

View file

@ -1,3 +1,5 @@
import {post_request} from "../js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -29,27 +31,7 @@ async function formHandler(listener) {
} }
let url = "https://api.account.skynet.ie/ldap/recover/password/auth"; let url = "https://api.account.skynet.ie/ldap/recover/password/auth";
let req; let body = {auth: auth, pass: pass};
try {
req = await fetch(url, {
method: 'POST',
body: JSON.stringify({auth: auth, pass: pass}),
mode: "cors"
});
} catch (e) {
form_status.innerHTML = `<span style='background-color: red; color: white'>${e}</span>`;
return;
}
if (req.status !== 200) { await post_request(url, body, form_status);
form_status.innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
return;
}
let data = await req.json();
if (data.result === 'error') {
form_status.innerHTML = `<span style='background-color: red; color: white'>${data.error}</span>`;
} else {
form_status.innerHTML = "<span style='background-color: green; color: white'>Success</span>";
}
} }

View file

@ -36,7 +36,7 @@
</table> </table>
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
<script src="ssh-request.js"></script> <script type="module" src="ssh-request.js"></script>
</main> </main>
<footer class="page-footer"> <footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span> UL Computer Society 2023-<span id="year">2023</span>

View file

@ -1,3 +1,5 @@
import {post_request} from "../js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -13,28 +15,5 @@ async function formHandler(listener) {
const body = {user: formData.get('user'), email: formData.get('mail')}; const body = {user: formData.get('user'), email: formData.get('mail')};
let url = 'https://api.account.skynet.ie/ldap/recover/ssh/request'; let url = 'https://api.account.skynet.ie/ldap/recover/ssh/request';
let req; await post_request(url, body, form_status);
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>";
}
} }

View file

@ -31,7 +31,7 @@
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
</div> </div>
<script src="username.js"></script> <script type="module" src="username.js"></script>
</main> </main>
<footer class="page-footer"> <footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span> UL Computer Society 2023-<span id="year">2023</span>

View file

@ -1,3 +1,5 @@
import {post_request} from "../js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -19,28 +21,5 @@ async function formHandler(listener) {
let url = 'https://api.account.skynet.ie/ldap/recover/username'; let url = 'https://api.account.skynet.ie/ldap/recover/username';
let req; await post_request(url, body, form_status);
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>";
}
} }

View file

@ -43,7 +43,7 @@
</table> </table>
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
<script src="register.js"></script> <script type="module" src="register.js"></script>
</main> </main>
<footer class="page-footer"> <footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span> UL Computer Society 2023-<span id="year">2023</span>

View file

@ -1,3 +1,5 @@
import {post_request} from "./js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -29,28 +31,5 @@ async function formHandler(listener) {
const body = {auth: auth, user: user, pass: pass}; const body = {auth: auth, user: user, pass: pass};
let url = 'https://api.account.skynet.ie/ldap/new/account'; let url = 'https://api.account.skynet.ie/ldap/new/account';
let req; await post_request(url, body, form_status);
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>";
}
} }

View file

@ -46,7 +46,7 @@
<input id="button" type="submit" value="Submit"/> <input id="button" type="submit" value="Submit"/>
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
<script src="signup.js"></script> <script type="module" src="signup.js"></script>
</main> </main>
<footer class="page-footer"> <footer class="page-footer">
UL Computer Society 2023-<span id="year">2023</span> UL Computer Society 2023-<span id="year">2023</span>

View file

@ -1,3 +1,5 @@
import {post_request} from "./js/lib.js";
const form = document.getElementById("form"); const form = document.getElementById("form");
form.addEventListener('submit', formHandler); form.addEventListener('submit', formHandler);
@ -17,29 +19,6 @@ async function formHandler(listener) {
let url = 'https://api.account.skynet.ie/ldap/new/email'; let url = 'https://api.account.skynet.ie/ldap/new/email';
let req; await post_request(url, body, form_status);
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>";
}
} }