backoffice/web/js/addUser.js

90 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-08-12 21:40:29 +02:00
checkToken();
2022-08-12 22:17:39 +02:00
signOut();
2022-08-14 19:32:40 +02:00
closeButton();
2022-08-12 22:17:39 +02:00
document.getElementById("addUser").addEventListener("click", function(){
var updateProfil = ["pseudo", "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 = {
"id": "",
"pseudo": document.getElementById("pseudoUpdateInput").value,
"email": document.getElementById("emailUpdateInput").value,
"name": document.getElementById("nameUpdateInput").value,
"firstName": document.getElementById("firstNameUpdateInput").value,
"birth": document.getElementById("birthUpdateInput").value,
"status": 1,
"password": document.getElementById("passwordUpdateInput").value,
"roles": selector["roles"],
"created_at": "",
"updated_at": "",
"deleted_at": "",
"connected_at": ""
}
instance({
method : "put",
url:"users",
withCredentials: true,
data: dataPut
}).then(function(response){
2022-08-12 22:31:04 +02:00
var idAlert = "";
2022-08-12 22:17:39 +02:00
switch(response.status){
2022-08-12 22:31:04 +02:00
case 201:
2022-08-12 22:17:39 +02:00
location.reload();
break;
case 204:
2022-08-12 22:31:04 +02:00
idAlert="notModified"
2022-08-12 22:17:39 +02:00
break;
2022-08-12 22:31:04 +02:00
default:
idAlert="unknown";
break
}
document.getElementById(idAlert+"Alert").classList.remove("hidden");
}).catch(function(error){
2022-08-14 19:32:40 +02:00
console.log(error);
switch(error.response.status){
2022-08-12 22:17:39 +02:00
case 401:
2022-08-14 19:32:40 +02:00
listExist = [ "pseudo", "email" ];
for(var i=0; i<listExist.length; i++){
if(error.response.data[listExist[i]]){
document.getElementById(listExist[i]+"ExistAlert").classList.remove("hidden");
}
}
2022-08-12 22:31:04 +02:00
idAlert="notAuthorized";
2022-08-12 22:17:39 +02:00
break;
case 403:
2022-08-12 22:31:04 +02:00
idAlert="forbidden";
2022-08-12 22:17:39 +02:00
break;
case 500:
2022-08-12 22:31:04 +02:00
idAlert="internalServer";
2022-08-12 22:17:39 +02:00
break;
default:
2022-08-12 22:31:04 +02:00
idAlert="unknown";
break;
2022-08-12 22:17:39 +02:00
}
2022-08-12 22:31:04 +02:00
document.getElementById(idAlert+"Alert").classList.remove("hidden");
2022-08-12 22:17:39 +02:00
});
}
});