change name addUser script
This commit is contained in:
91
web/js/addItem.js
Normal file
91
web/js/addItem.js
Normal file
@@ -0,0 +1,91 @@
|
||||
checkToken();
|
||||
signOut();
|
||||
closeButton();
|
||||
pathURL = document.location.pathname.split("/")[document.location.pathname.split("/").length-1].split(".")[0].replace("add","")
|
||||
|
||||
|
||||
document.getElementById("addUser").addEventListener("click", function(){
|
||||
var updateProfil = ["username", "email", "firstName", "birth", "password"];
|
||||
var updateOk = true;
|
||||
for (var i=0; i<updateProfil.length; i++){
|
||||
if(document.getElementById(updateProfil[i]+"UpdateInput").value.length == 0){
|
||||
updateOk = false;
|
||||
document.getElementById(updateProfil[i]+"Alert").classList.remove("hidden");
|
||||
}
|
||||
}
|
||||
if(updateOk){
|
||||
var selector = { "roles": ""};
|
||||
var listSelector = [ "roles"];
|
||||
for (var i=0; i<listSelector.length; i++){
|
||||
var options = document.querySelectorAll("#"+listSelector[i]+"Selector option");
|
||||
for (var j=0; j<options.length; j++){
|
||||
if(options[j].selected){
|
||||
selector[listSelector[i]] = options[j].value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dataPut = {
|
||||
"username": document.getElementById("usernameUpdateInput").value,
|
||||
"email": document.getElementById("emailUpdateInput").value,
|
||||
"name": document.getElementById("nameUpdateInput").value,
|
||||
"firstName": document.getElementById("firstNameUpdateInput").value,
|
||||
"birth": document.getElementById("birthUpdateInput").value,
|
||||
"password": document.getElementById("passwordUpdateInput").value,
|
||||
"roles": selector["roles"]
|
||||
|
||||
}
|
||||
instance({
|
||||
method : "put",
|
||||
url:pathURL+"s",
|
||||
withCredentials: true,
|
||||
data: dataPut
|
||||
}).then(function(response){
|
||||
var idAlert = "";
|
||||
switch(response.status){
|
||||
case 201:
|
||||
idAlert="createUser"
|
||||
var user = document.getElementById("usernameUpdateInput").value;
|
||||
if(user.length > 0){
|
||||
document.getElementById("displayUser").textContent=user;
|
||||
}
|
||||
break;
|
||||
case 204:
|
||||
idAlert="notModified"
|
||||
break;
|
||||
default:
|
||||
idAlert="unknown";
|
||||
break
|
||||
}
|
||||
document.getElementById(idAlert+"Alert").classList.remove("hidden");
|
||||
}).catch(function(error){
|
||||
console.log(error);
|
||||
switch(error.response.status){
|
||||
case 401:
|
||||
listExist = [ "username", "email" ];
|
||||
for(var i=0; i<listExist.length; i++){
|
||||
if(error.response.data[listExist[i]]){
|
||||
document.getElementById(listExist[i]+"ExistAlert").classList.remove("hidden");
|
||||
}
|
||||
}
|
||||
idAlert="notAuthorized";
|
||||
break;
|
||||
case 403:
|
||||
idAlert="forbidden";
|
||||
break;
|
||||
case 403:
|
||||
idAlert="syntaxe";
|
||||
break;
|
||||
case 500:
|
||||
idAlert="internalServer";
|
||||
break;
|
||||
default:
|
||||
idAlert="unknown";
|
||||
break;
|
||||
}
|
||||
document.getElementById(idAlert+"Alert").classList.remove("hidden");
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
});
|
Reference in New Issue
Block a user