covas-bo/web/js/user.js
2023-11-03 21:36:53 +01:00

183 lines
7.0 KiB
JavaScript

checkToken();
signOut();
closeButton();
var id = getQuery("id");
if(id.length > 0){
instance.get("users/"+id, {withCredentials: true})
.then(function(response){
var listValue = [ "id", "username", "name", "firstName", "email", "birth"];
var listTime = ["connected_at", "created_at", "updated_at", "deleted_at"];
for (var i=0; i<listValue.length; i++){
if(response.data[listValue[i]] != null){
document.getElementById(listValue[i]+"UpdateInput").value=response.data[listValue[i]];
}
}
for (var i=0; i<listTime.length; i++){
if(response.data[listTime[i]] != null){
document.getElementById(listTime[i]+"UpdateInput").value=new Date(response.data[listTime[i]]).toLocaleString();
}
}
var listSelector = ["roles", "status"];
for (var i=0; i<listSelector.length; i++){
var select = document.getElementById(listSelector[i]+"Selector").querySelectorAll("option");
for (var j=0; j<select.length; j++){
if(select[j].value == response.data[listSelector[i]]){
select[j].setAttribute("selected", true);
}
}
}
var buttonUser = "";
switch(response.data.status){
case 1:
buttonUser = "disableUser";
break;
case 2:
buttonUser = "enableUser";
break;
case -1:
buttonUser = "cancelUser";
document.getElementById("removeUser").classList.add("hidden");
break;
case 0:
buttonUser = "enableUser";
break;
default:
break;
}
if(buttonUser.length > 0){
document.getElementById(buttonUser).classList.remove("hidden");
}
var listModalButton = ["disable", "enable", "cancel", "remove"];
for (var i=0; i<listModalButton.length; i++){
document.getElementById(listModalButton[i]+"UserButton").addEventListener("click", function(e){
var httpMethod = ""
var httpUrl = "";
var idInput = document.getElementById("idUpdateInput").value;
switch(e.currentTarget.id){
case "disableUserButton":
httpMethod = "delete";
httpUrl = "users/"+idInput;
break;
case "removeUserButton":
httpMethod = "delete";
httpUrl = "users/"+idInput+"?remove=true";
break;
default:
httpMethod = "patch";
httpUrl = "users/"+idInput;
break;
}
instance({
method: httpMethod,
url: httpUrl,
withCredentials: true
})
.then(function(response){
if(response.status == 200){
location.href="/html/user.html?id="+idInput;
}
})
.catch(function(error){
console.log(error);
});
});
}
})
.catch(function(error){
console.log(error);
});
document.getElementById("updateUserButton").addEventListener("click", function(){
var updateProfil = ["id", "username", "email", "firstName", "birth"];
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": "", "status": 0};
var listSelector = [ "roles", "status"];
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": document.getElementById("idUpdateInput").value,
"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,
"status": selector["status"],
"password": document.getElementById("passwordUpdateInput").value,
"roles": selector["roles"],
"created_at": "",
"updated_at": "",
"deleted_at": "",
"connected_at": ""
}
instance({
method : "put",
url:"users/"+document.getElementById("idUpdateInput").value,
withCredentials: true,
data: dataPut
}).then(function(response){
var idAlert = "";
switch(response.status){
case 200:
location.reload();
break;
case 304:
idAlert="noContent";
break;
default:
idAlert="unknown";
break;
}
document.getElementById(idAlert+"Alert").classList.remove("hidden");
}).catch(function(error){
var idAlert = "";
switch(error.status){
case 401:
idAlert="notAuthorized";
break;
case 403:
idAlert="forbidden";
break;
case 500:
idAlert="internalServer";
break;
default:
idAlert="unknown";
break;
}
document.getElementById(idAlert+"Alert").classList.remove("hidden");
});
}
});
}