covas-bo/web/js/addUser.js

97 lines
3.5 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(){
2023-10-27 23:51:16 +02:00
var updateProfil = ["username", "email", "firstName", "birth", "password"];
2022-08-12 22:17:39 +02:00
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": "",
2023-10-27 23:51:16 +02:00
"username": document.getElementById("usernameUpdateInput").value,
2022-08-12 22:17:39 +02:00
"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-14 19:36:23 +02:00
idAlert="createUser"
2023-10-27 23:51:16 +02:00
var user = document.getElementById("usernameUpdateInput").value;
2022-08-15 17:43:30 +02:00
if(user.length > 0){
document.getElementById("displayUser").textContent=user;
}
2022-08-12 22:17:39 +02:00
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:
2023-10-27 23:51:16 +02:00
listExist = [ "username", "email" ];
2022-08-14 19:32:40 +02:00
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;
2023-10-27 23:58:23 +02:00
case 403:
idAlert="syntaxe";
break;
2022-08-12 22:17:39 +02:00
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
});
}
});