2021-07-23 06:51:08 +00:00
|
|
|
|
|
|
|
//SETTING VARIABLES. GRABBING ELEMENT VALUES FROM FORM.
|
|
|
|
|
|
|
|
let uname = document.querySelector('#uname').value;
|
|
|
|
let pin = document.querySelector('#pin').value;
|
|
|
|
let newUname = document.querySelector('#newuname').value;
|
|
|
|
let newPin = document.querySelector('#newpin').value;
|
|
|
|
const form = document.querySelector('form');
|
|
|
|
let selected = document.querySelector('#selected').value;
|
|
|
|
let custom = document.querySelector('#custom').value;
|
|
|
|
let updateEvent = ''
|
|
|
|
let newEvent = ''
|
|
|
|
let newPronouns = ''
|
|
|
|
|
|
|
|
//FORM SUMBIT FUNCTION & GET THE USERS USERNAME AND SEE IF IT IS CORRECT.
|
|
|
|
|
|
|
|
|
|
|
|
form.addEventListener("submit", async function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const formData = new FormData(form);
|
|
|
|
|
|
|
|
uname = formData.get('uname');
|
|
|
|
pin = formData.get('pin');
|
|
|
|
newUname = formData.get('newuname');
|
|
|
|
newPin = formData.get('newpin');
|
|
|
|
selected = formData.get('selected');
|
|
|
|
custom = formData.get('custom')
|
|
|
|
|
2021-07-24 14:10:41 +00:00
|
|
|
|
|
|
|
//SETS NEWPRONOUNS DEPENDING ON WHAT THE USER SELECTED/TYPED
|
2021-07-23 06:51:08 +00:00
|
|
|
if (custom === '' && selected === 'none') {
|
|
|
|
newPronouns = ''
|
|
|
|
} else if (custom !== '') {
|
|
|
|
newPronouns = custom
|
|
|
|
} else {
|
|
|
|
newPronouns = selected
|
|
|
|
}
|
|
|
|
|
2021-07-24 14:10:41 +00:00
|
|
|
//CHECKS IF A USERNAME IS TAKEN
|
|
|
|
if (newUname !== '') {
|
|
|
|
const response = await fetch(`api/users/${newUname}/`);
|
|
|
|
const isTaken = await response.json();
|
|
|
|
|
|
|
|
if (isTaken.status === "ok") {
|
|
|
|
document.querySelector('#errormessage').innerHTML = `${newUname} is already taken.`
|
2021-07-24 15:17:21 +00:00
|
|
|
return;
|
2021-07-24 14:10:41 +00:00
|
|
|
} else {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//CHECKS IF THE USER IS CHANGING MORE THAN ONE TEXT FIELD AT A TIME
|
2021-07-23 06:51:08 +00:00
|
|
|
if (newUname !== '' && newPin !== '') {
|
2021-07-24 14:10:41 +00:00
|
|
|
document.querySelector("#errormessage").innerHTML = 'You can only change one at a time!'
|
2021-07-24 15:17:21 +00:00
|
|
|
return;
|
2021-07-23 06:51:08 +00:00
|
|
|
} else if (newUname !== '' && newPronouns !== '') {
|
2021-07-24 14:10:41 +00:00
|
|
|
document.querySelector("#errormessage").innerHTML = 'You can only change one at a time!'
|
2021-07-24 15:17:21 +00:00
|
|
|
return;
|
2021-07-23 06:51:08 +00:00
|
|
|
} else if (newPin !== '' && newPronouns !== '') {
|
2021-07-24 14:10:41 +00:00
|
|
|
document.querySelector("#errormessage").innerHTML = 'You can only change one at a time!'
|
2021-07-24 15:17:21 +00:00
|
|
|
return;
|
2021-07-24 14:10:41 +00:00
|
|
|
} else if (newUname !== '' && newPin !== '' && newPronouns !== '') {
|
|
|
|
document.querySelector("#errormessage").innerHTML = 'You can only change one at a time!'
|
2021-07-24 15:17:21 +00:00
|
|
|
return;
|
2021-07-23 06:51:08 +00:00
|
|
|
} else {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-24 14:10:41 +00:00
|
|
|
// ASSIGNS VARIABLES TO BE SENT TO API
|
2021-07-23 06:51:08 +00:00
|
|
|
if (newUname === '' && newPin === '' && newPronouns !== '') {
|
|
|
|
newEvent = newPronouns
|
2021-07-24 15:17:21 +00:00
|
|
|
updateEvent = 'Pronouns'
|
2021-07-23 06:51:08 +00:00
|
|
|
} else if (newUname === '' && newPronouns === '' && newPin !== '') {
|
|
|
|
newEvent = newPin
|
2021-07-24 15:17:21 +00:00
|
|
|
updateEvent = 'Pin'
|
2021-07-23 06:51:08 +00:00
|
|
|
} else if (newPin === '' && newPronouns === '' && newUname !== '') {
|
|
|
|
newEvent = newUname
|
2021-07-24 15:17:21 +00:00
|
|
|
updateEvent = 'Name'
|
2021-07-23 06:51:08 +00:00
|
|
|
} else if (newPin === '' && newUname === '' && newPronouns === '') {
|
|
|
|
document.querySelector("#errormessage").innerHTML = 'Please enter a new name, pin, or pronouns!'
|
2021-07-24 15:17:21 +00:00
|
|
|
return;
|
2021-07-23 06:51:08 +00:00
|
|
|
} else {
|
|
|
|
}
|
2021-07-24 15:17:21 +00:00
|
|
|
|
2021-07-24 14:10:41 +00:00
|
|
|
loginStatus()
|
2021-07-23 06:51:08 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2021-07-24 14:10:41 +00:00
|
|
|
//CHECKS IF THE LOGIN IS A SUCCESS
|
|
|
|
async function loginStatus() {
|
|
|
|
const loginInfo = await checkLoginInfo();
|
2021-07-23 06:51:08 +00:00
|
|
|
|
2021-07-24 14:10:41 +00:00
|
|
|
if (loginInfo.status === 'ok') {
|
2021-07-23 06:51:08 +00:00
|
|
|
updateInfo()
|
|
|
|
} else {
|
|
|
|
incorrectLogin()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 14:10:41 +00:00
|
|
|
//TODO ADD CHECKING THE TOKEN WITH LOGIN IN IF STATEMENT
|
|
|
|
//CHECKING IF THE USER CAN LOGIN WITH GIVEN CURRENT USERNAME AND PIN
|
|
|
|
// LOGIN FETCH
|
|
|
|
|
|
|
|
async function checkLoginInfo() {
|
|
|
|
let sendLoginInfo = { "name": uname, "pin": pin }
|
|
|
|
const res = await fetch('/api/login/', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(sendLoginInfo),
|
|
|
|
});
|
|
|
|
return await res.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
//FETCH FUNCTION TO UPDATE USER INFO
|
|
|
|
|
2021-07-23 06:51:08 +00:00
|
|
|
async function updateInfo() {
|
|
|
|
let sendUpdateInfo = { "name": uname, "pin": pin, "changed_event": updateEvent, "new_event": newEvent }
|
2021-07-24 15:17:21 +00:00
|
|
|
fetch('/api/change', {
|
2021-07-23 06:51:08 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(sendUpdateInfo),
|
|
|
|
});
|
2021-07-24 14:10:41 +00:00
|
|
|
document.querySelector("#errormessage").innerHTML = 'Login Changed!'
|
2021-07-23 06:51:08 +00:00
|
|
|
//window.location.replace("/login.html")
|
|
|
|
}
|
|
|
|
|
|
|
|
function incorrectLogin() {
|
|
|
|
document.querySelector("#errormessage").innerHTML = 'Username and pin combination do not match! Or user not found.'
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorMessage() {
|
|
|
|
document.querySelector("#errormessage").innerHTML = 'An Error has Occurred. Try again later.'
|
|
|
|
}
|