backoffice/web/js/addItem.js

139 lines
5.4 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();
2023-11-04 23:37:04 +01:00
pathURL = document.location.pathname.split("/")[document.location.pathname.split("/").length-1].split(".")[0].replace("add","")
2022-08-12 22:17:39 +02:00
document.getElementById("addUser").addEventListener("click", function(){
2023-11-12 17:09:18 +01:00
switch (pathURL){
2023-11-12 18:41:15 +01:00
case "user":
2023-11-12 17:09:18 +01:00
var updateProfil = ["username", "email", "firstName", "birth", "password"];
break;
2023-11-12 18:41:15 +01:00
case "event":
2023-11-12 17:09:18 +01:00
var updateProfil = ["name", "place", "start_date", "end_date"];
break;
default:
var updateProfil = []
break;
}
2022-08-12 22:17:39 +02:00
var updateOk = true;
for (var i=0; i<updateProfil.length; i++){
2023-11-12 18:41:15 +01:00
if(updateProfil[i] == "place"){
2023-11-12 17:09:18 +01:00
size = document.getElementsByClassName("mapboxgl-ctrl-geocoder--input")[0].value.length
} else {
size= document.getElementById(updateProfil[i]+"UpdateInput").value.length
}
if(size == 0){
2022-08-12 22:17:39 +02:00
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;
}
}
}
2023-11-12 17:09:18 +01:00
switch(pathURL){
2023-11-12 18:41:15 +01:00
case "user":
2023-11-12 17:09:18 +01:00
dataPut = {
"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,
"password": document.getElementById("passwordUpdateInput").value,
"roles": selector["roles"]
};
break;
2023-11-12 18:41:15 +01:00
case "event":
2023-11-15 23:46:55 +01:00
organizers = []
2023-11-16 21:40:06 +01:00
listValue = JSON.parse(document.getElementById("organizersUpdateInput").value)
2023-11-15 23:46:55 +01:00
for (var i=0; i<listValue.length; i++){
2023-11-16 21:40:06 +01:00
organizers.push(listValue[i]["value"])
2023-11-15 23:46:55 +01:00
}
2023-11-12 17:09:18 +01:00
dataPut = {
"name": document.getElementById("nameUpdateInput").value,
"place": document.getElementsByClassName("mapboxgl-ctrl-geocoder--input")[0].value,
"start_date": document.getElementById("start_dateUpdateInput").value,
"end_date": document.getElementById("end_dateUpdateInput").value,
2023-11-20 23:16:56 +01:00
"organizers":organizers,
"latitude": document.getElementById("placeUpdateInput").getAttribute("data-latitude"),
"longitude": document.getElementById("placeUpdateInput").getAttribute("data-longitude")
2023-11-12 17:09:18 +01:00
};
break;
default:
dataPut = {};
break;
2022-08-12 22:17:39 +02:00
}
instance({
method : "put",
2023-11-04 23:37:04 +01:00
url:pathURL+"s",
2022-08-12 22:17:39 +02:00
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-11-20 23:16:56 +01:00
switch (pathURL){
case "user":
input = "username";
break;
case "event":
input = "name";
break;
default:
input = "";
break;
}
var user = document.getElementById(input+"UpdateInput").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);
2023-11-20 23:16:56 +01:00
switch(error.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");
2023-11-12 17:09:18 +01:00
}1
2022-08-14 19:32:40 +02:00
}
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
});
}
});