From 9042ce0df952308e2da0c0f51e666e988bba51cb Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Mon, 1 Jan 2024 10:06:21 +0000 Subject: [PATCH] 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 --- src/js/lib.js | 28 ++++++++++++++++++++++++++++ src/modify.html | 2 +- src/modify.js | 31 ++++++------------------------- src/password.html | 2 +- src/password.js | 27 +++------------------------ src/recovery/password.html | 6 ++++-- src/recovery/password_reset.html | 2 +- src/recovery/password_reset.js | 26 ++++---------------------- src/recovery/ssh-request.html | 2 +- src/recovery/ssh-request.js | 27 +++------------------------ src/recovery/username.html | 2 +- src/recovery/username.js | 27 +++------------------------ src/register.html | 2 +- src/register.js | 27 +++------------------------ src/signup.html | 2 +- src/signup.js | 27 +++------------------------ 16 files changed, 64 insertions(+), 176 deletions(-) create mode 100644 src/js/lib.js diff --git a/src/js/lib.js b/src/js/lib.js new file mode 100644 index 0000000..2b585dd --- /dev/null +++ b/src/js/lib.js @@ -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 = `${e}`; + return; + } + + if (req.status !== 200) { + form_status.innerHTML = "Failure"; + return; + } + + let temp = await req.json(); + if (temp.result === 'error') { + form_status.innerHTML = `${temp.error}`; + } else { + form_status.innerHTML = "Success"; + } +} \ No newline at end of file diff --git a/src/modify.html b/src/modify.html index f201542..2498418 100644 --- a/src/modify.html +++ b/src/modify.html @@ -49,7 +49,7 @@

- +