2022-08-06 21:48:41 +02:00
|
|
|
checkToken();
|
|
|
|
signOut();
|
2022-08-11 00:12:52 +02:00
|
|
|
closeButton();
|
2022-08-06 21:48:41 +02:00
|
|
|
var id = getQuery("id");
|
|
|
|
|
|
|
|
if(id.length > 0){
|
|
|
|
instance.get("users/"+id, {withCredentials: true})
|
|
|
|
.then(function(response){
|
2023-10-25 23:43:46 +02:00
|
|
|
var listValue = [ "id", "username", "name", "firstName", "email", "birth"];
|
2022-08-07 18:11:48 +02:00
|
|
|
var listTime = ["connected_at", "created_at", "updated_at", "deleted_at"];
|
2022-08-06 21:48:41 +02:00
|
|
|
for (var i=0; i<listValue.length; i++){
|
2022-08-07 18:11:48 +02:00
|
|
|
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();
|
|
|
|
}
|
2022-08-06 21:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-06 22:48:00 +02:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2022-08-07 00:14:14 +02:00
|
|
|
|
|
|
|
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";
|
2023-10-25 23:43:46 +02:00
|
|
|
httpUrl = "users/"+idInput;
|
2022-08-07 00:14:14 +02:00
|
|
|
break;
|
|
|
|
case "removeUserButton":
|
|
|
|
httpMethod = "delete";
|
|
|
|
httpUrl = "users/"+idInput;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
httpMethod = "patch";
|
|
|
|
httpUrl = "users/enable/"+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);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-06 21:48:41 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
.catch(function(error){
|
|
|
|
console.log(error);
|
|
|
|
|
|
|
|
});
|
2022-08-07 00:14:14 +02:00
|
|
|
|
2022-08-11 00:12:52 +02:00
|
|
|
|
|
|
|
document.getElementById("updateUserButton").addEventListener("click", function(){
|
2023-10-25 23:43:46 +02:00
|
|
|
var updateProfil = ["id", "username", "email", "firstName", "birth"];
|
2022-08-11 00:12:52 +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": "", "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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-07 00:14:14 +02:00
|
|
|
|
2022-08-11 00:12:52 +02:00
|
|
|
dataPut = {
|
|
|
|
|
|
|
|
"id": id,
|
2023-10-25 23:43:46 +02:00
|
|
|
"username": document.getElementById("usernameUpdateInput").value,
|
2022-08-11 00:12:52 +02:00
|
|
|
"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({
|
2023-10-25 23:43:46 +02:00
|
|
|
method : "put",
|
|
|
|
url:"users",
|
2022-08-11 00:12:52 +02:00
|
|
|
withCredentials: true,
|
|
|
|
data: dataPut
|
|
|
|
}).then(function(response){
|
2022-08-14 16:47:24 +02:00
|
|
|
var idAlert = "";
|
2022-08-11 21:33:24 +02:00
|
|
|
switch(response.status){
|
|
|
|
case 200:
|
|
|
|
location.reload();
|
|
|
|
break;
|
|
|
|
case 304:
|
2022-08-14 16:47:24 +02:00
|
|
|
idAlert="noContent";
|
2022-08-11 21:33:24 +02:00
|
|
|
break;
|
|
|
|
default:
|
2022-08-14 16:47:24 +02:00
|
|
|
idAlert="unknown";
|
|
|
|
break;
|
2022-08-11 21:33:24 +02:00
|
|
|
}
|
2022-08-14 16:47:24 +02:00
|
|
|
document.getElementById(idAlert+"Alert").classList.remove("hidden");
|
|
|
|
|
2022-08-11 00:12:52 +02:00
|
|
|
}).catch(function(error){
|
2022-08-14 16:47:24 +02:00
|
|
|
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");
|
2022-08-11 00:12:52 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2022-08-07 00:14:14 +02:00
|
|
|
|
2022-08-06 21:48:41 +02:00
|
|
|
}
|