backoffice/web/js/item.js

262 lines
11 KiB
JavaScript
Raw Normal View History

2022-08-06 21:48:41 +02:00
checkToken();
signOut();
2022-08-11 00:12:52 +02:00
closeButton();
2023-11-04 23:13:41 +01:00
pathURL = document.location.pathname.split("/")[document.location.pathname.split("/").length-1].split(".")[0]
2022-08-06 21:48:41 +02:00
var id = getQuery("id");
if(id.length > 0){
2023-11-04 23:13:41 +01:00
instance.get(pathURL + "s/"+id, {withCredentials: true})
2022-08-06 21:48:41 +02:00
.then(function(response){
2023-11-12 21:19:50 +01:00
switch(pathURL){
case "user":
var listValue = [ "id", "username", "name", "firstName", "email", "birth"];
break;
case "event":
2023-11-16 21:40:06 +01:00
var listValue = [ "id", "name", "organizers", "place", "start_date", "end_date"];
2023-11-12 21:19:50 +01:00
break;
default:
var listValue = []
break;
}
2023-11-04 20:59:06 +01:00
var listTime = ["connected_at", "created_at", "updated_at", "deleted_at", "disabled_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){
2023-11-16 21:40:06 +01:00
switch(listValue[i]){
case "place":
document.getElementsByClassName("mapboxgl-ctrl-geocoder--input")[0].value = response.data[listValue[i]];
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;
2023-11-12 21:34:07 +01:00
}
2022-08-07 18:11:48 +02:00
}
}
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++){
2023-11-12 21:19:50 +01:00
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);
}
2022-08-06 21:48:41 +02:00
}
}
}
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");
}
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-11-04 23:13:41 +01:00
httpUrl = pathURL + "s/"+idInput;
break;
case "removeUserButton":
httpMethod = "delete";
2023-11-04 23:13:41 +01:00
httpUrl = pathURL + "s/"+idInput+"?remove=true";
break;
default:
httpMethod = "patch";
2023-11-04 23:13:41 +01:00
httpUrl = pathURL + "s/"+idInput;
break;
}
instance({
method: httpMethod,
url: httpUrl,
withCredentials: true
})
.then(function(response){
if(response.status == 200){
2023-11-04 23:13:41 +01:00
location.href="/html/"+pathURL+".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-11 00:12:52 +02:00
document.getElementById("updateUserButton").addEventListener("click", function(){
2023-11-12 21:19:50 +01:00
switch(pathURL){
case "user":
var updateProfil = [ "id", "username", "name", "firstName", "email", "birth"];
break;
case "event":
2023-11-16 21:40:06 +01:00
var updateProfil = [ "id", "name", "place", "organizers", "start_date", "end_date"];
2023-11-12 21:19:50 +01:00
break;
default:
var updateProfil = []
break;
}
2022-08-11 00:12:52 +02:00
var updateOk = true;
for (var i=0; i<updateProfil.length; i++){
2023-11-16 21:40:06 +01:00
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;
2023-11-12 21:34:07 +01:00
}
if(size == 0){
2022-08-11 00:12:52 +02:00
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;
}
}
}
2023-11-12 21:19:50 +01:00
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,
2023-11-12 21:34:07 +01:00
"place": document.getElementsByClassName("mapboxgl-ctrl-geocoder--input")[0].value,
2023-11-12 21:19:50 +01:00
"start_date": document.getElementById("start_dateUpdateInput").value,
"end_date": document.getElementById("end_dateUpdateInput").value,
"status": selector["status"],
2023-11-16 21:40:06 +01:00
"organizers":organizers,
2023-11-12 21:19:50 +01:00
"created_at": "",
"updated_at": "",
"deleted_at": "",
2023-11-12 21:34:07 +01:00
"disabled_at":"",
2023-11-12 21:19:50 +01:00
};
break;
default:
datePut = {};
break;
2022-08-11 00:12:52 +02:00
}
instance({
2023-10-25 23:43:46 +02:00
method : "put",
2023-11-04 23:13:41 +01:00
url:pathURL+"s/"+document.getElementById("idUpdateInput").value,
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-06 21:48:41 +02:00
}