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-25 23:25:48 +00:00
|
|
|
unameCheck()
|
2021-07-24 14:10:41 +00:00
|
|
|
|
|
|
|
//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-25 23:25:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
//IF THE API SUCCESSFULLY UPDATES INFO FOR A USER THEN DO THIS
|
|
|
|
try {
|
|
|
|
const updateResponse = await loginStatus();
|
|
|
|
|
|
|
|
if (updateResponse == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateResponse.status === 'ok') {
|
|
|
|
document.querySelector("#errormessage").innerHTML = 'Login Changed!'
|
2021-07-26 22:48:24 +00:00
|
|
|
window.location.replace("../html/login.html")
|
2021-07-25 23:25:48 +00:00
|
|
|
} else {
|
|
|
|
document.querySelector("#errormessage").innerHTML = 'Failed to update info. Try again later.'
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
document.querySelector("#errormessage").innerHTML = 'An Error has Occurred. Try again later. ' + e.toString();
|
|
|
|
}
|
2021-07-23 06:51:08 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2021-07-25 23:25:48 +00:00
|
|
|
//CHECKS IF A USERNAME IS TAKEN
|
|
|
|
async function unameCheck(){
|
|
|
|
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.`
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-25 23:25:48 +00:00
|
|
|
return await updateInfo()
|
2021-07-23 06:51:08 +00:00
|
|
|
} 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-25 23:25:48 +00:00
|
|
|
const response = await fetch('/api/change', {
|
2021-07-23 06:51:08 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(sendUpdateInfo),
|
|
|
|
});
|
2021-07-25 23:25:48 +00:00
|
|
|
return await response.json()
|
2021-07-23 06:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.'
|
|
|
|
}
|