[Modify user] Refactored existing form handling code for what this form will be used for

#1
This commit is contained in:
eoghan.conlon 2023-07-29 23:07:15 +01:00
parent 5235c785b0
commit 13270f2f4e

View file

@ -13,8 +13,6 @@
<input type="password" id="pass" name="pass" /> <br /> <input type="password" id="pass" name="pass" /> <br />
<label for="newEmail">New email</label> <label for="newEmail">New email</label>
<input type="email" id="newEmail" name="newPW" /> <br /> <input type="email" id="newEmail" name="newPW" /> <br />
<label for="newEmail2">Confirm</label>
<input type="email" id="newEmail2" name="newPWConfirm" /> <br />
<input type="Submit" value="Submit" /> <input type="Submit" value="Submit" />
</form> </form>
<p id="formStatus"></p> <p id="formStatus"></p>
@ -31,23 +29,19 @@
const user = formData.get("user"); const user = formData.get("user");
const pass = formData.get("pass"); const pass = formData.get("pass");
const newEmail = formData.get("newEmail"); const newEmail = formData.get("newEmail");
if(newEmail === formData.get("newEmail2")){ const object = {user: user, pass: pass, field: "mail", value: newEmail}
const object = {user: user, pass: pass, field: "mail", value: newEmail} fetch('https://api.sso.skynet.ie/ldap/update', {
fetch('https://api.sso.skynet.ie/ldap/update', { method: 'POST',
method: 'POST', body: JSON.stringify(object)
body: JSON.stringify(object) }).then(res => {
}).then(res => { if(res.status === 200) {
if(res.status === 200) { document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success</span>";
document.getElementById('formStatus').innerHTML = "<span style='background-color: green; color: white'>Success</span>"; } else if (res.status === 500){
} else if (res.status === 500){ document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure</span>";
document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure</span>"; } else {
} else { document.getElementById('formStatus').innerHTML = "<span style='background-color: red; color: white'>Failure: Failed to communicate to server</span>";
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>");
}).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'>Failure: new passwords don't match</span>";
}
} }
</script> </script>
</body> </body>