forked from v4l3n71n/covas-bo
266 lines
11 KiB
JavaScript
266 lines
11 KiB
JavaScript
checkToken();
|
|
signOut();
|
|
closeButton();
|
|
pathURL = document.location.pathname.split("/")[document.location.pathname.split("/").length-1].split(".")[0]
|
|
|
|
var id = getQuery("id");
|
|
|
|
if(id.length > 0){
|
|
instance.get(pathURL + "s/"+id, {withCredentials: true})
|
|
.then(function(response){
|
|
switch(pathURL){
|
|
case "user":
|
|
var listValue = [ "id", "username", "name", "firstName", "email", "birth"];
|
|
break;
|
|
case "event":
|
|
var listValue = [ "id", "name", "organizers", "place", "start_date", "end_date"];
|
|
break;
|
|
default:
|
|
var listValue = []
|
|
break;
|
|
}
|
|
var listTime = ["connected_at", "created_at", "updated_at", "deleted_at", "disabled_at"];
|
|
for (var i=0; i<listValue.length; i++){
|
|
if(response.data[listValue[i]] != null){
|
|
switch(listValue[i]){
|
|
case "place":
|
|
document.getElementsByClassName("mapboxgl-ctrl-geocoder--input")[0].value = response.data[listValue[i]];
|
|
document.getElementById("placeUpdateInput").setAttribute("data-latitude", response.data["latitude"]);
|
|
document.getElementById("placeUpdateInput").setAttribute("data-longitude", response.data["longitude"])
|
|
break;
|
|
case "organizers":
|
|
if (response.data[listValue[i]].length > 0 ){
|
|
listOrga = []
|
|
for (var j=0; j<response.data[listValue[i]].length; j++){
|
|
listOrga.push({"value":response.data[listValue[i]][j]});
|
|
}
|
|
valueJson = JSON.stringify(listOrga);
|
|
document.getElementById(listValue[i]+"UpdateInput").value=valueJson;
|
|
}
|
|
break;
|
|
default:
|
|
document.getElementById(listValue[i]+"UpdateInput").value=response.data[listValue[i]];
|
|
break;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
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++){
|
|
if (document.getElementById(listSelector[i]+"Selector") != null){
|
|
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 = pathURL + "s/"+idInput;
|
|
break;
|
|
case "removeUserButton":
|
|
httpMethod = "delete";
|
|
httpUrl = pathURL + "s/"+idInput+"?remove=true";
|
|
break;
|
|
default:
|
|
httpMethod = "patch";
|
|
httpUrl = pathURL + "s/"+idInput;
|
|
break;
|
|
}
|
|
instance({
|
|
method: httpMethod,
|
|
url: httpUrl,
|
|
withCredentials: true
|
|
})
|
|
.then(function(response){
|
|
if(response.status == 200){
|
|
location.href="/html/"+pathURL+".html?id="+idInput;
|
|
}
|
|
})
|
|
.catch(function(error){
|
|
console.log(error);
|
|
});
|
|
|
|
});
|
|
}
|
|
|
|
|
|
})
|
|
.catch(function(error){
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
|
|
document.getElementById("updateUserButton").addEventListener("click", function(){
|
|
switch(pathURL){
|
|
case "user":
|
|
var updateProfil = [ "id", "username", "name", "firstName", "email", "birth"];
|
|
break;
|
|
case "event":
|
|
var updateProfil = [ "id", "name", "place", "organizers", "start_date", "end_date"];
|
|
break;
|
|
default:
|
|
var updateProfil = []
|
|
break;
|
|
}
|
|
var updateOk = true;
|
|
for (var i=0; i<updateProfil.length; i++){
|
|
switch(updateProfil[i]){
|
|
case "place":
|
|
size = document.getElementsByClassName("mapboxgl-ctrl-geocoder--input")[0].value.length;
|
|
break;
|
|
case "organizers":
|
|
organizers = [];
|
|
listValue = JSON.parse(document.getElementById("organizersUpdateInput").value)
|
|
for (var J=0; J<listValue.length; J++){
|
|
organizers.push(listValue[J]["value"])
|
|
}
|
|
break;
|
|
default:
|
|
size = document.getElementById(updateProfil[i]+"UpdateInput").value.length;
|
|
break;
|
|
|
|
}
|
|
if(size == 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;
|
|
}
|
|
}
|
|
}
|
|
switch (pathURL){
|
|
case "user":
|
|
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": "",
|
|
"disabled_at":"",
|
|
"connected_at": ""
|
|
};
|
|
break;
|
|
case "event":
|
|
dataPut = {
|
|
"id": document.getElementById("idUpdateInput").value,
|
|
"name": document.getElementById("nameUpdateInput").value,
|
|
"place": document.getElementsByClassName("mapboxgl-ctrl-geocoder--input")[0].value,
|
|
"latitude": document.getElementById("placeUpdateInput").getAttribute("data-latitude"),
|
|
"longitude": document.getElementById("placeUpdateInput").getAttribute("data-longitude"),
|
|
"start_date": document.getElementById("start_dateUpdateInput").value,
|
|
"end_date": document.getElementById("end_dateUpdateInput").value,
|
|
"status": selector["status"],
|
|
"organizers":organizers,
|
|
"created_at": "",
|
|
"updated_at": "",
|
|
"deleted_at": "",
|
|
"disabled_at":"",
|
|
};
|
|
break;
|
|
default:
|
|
datePut = {};
|
|
break;
|
|
}
|
|
instance({
|
|
method : "put",
|
|
url:pathURL+"s/"+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");
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} |