backoffice/web/js/addItem.js
2023-11-20 23:39:23 +01:00

145 lines
5.7 KiB
JavaScript

checkToken();
signOut();
closeButton();
pathURL = document.location.pathname.split("/")[document.location.pathname.split("/").length-1].split(".")[0].replace("add","")
if (pathURL == "event"){
lastdate = new Date();
document.getElementById("start_dateUpdateInput").value=lastdate.getFullYear()+"-"+lastdate.getMonth()+"-"+lastdate.getDate()+"T00:00";
document.getElementById("end_dateUpdateInput").value=lastdate.getFullYear()+"-"+lastdate.getMonth()+"-"+lastdate.getDate()+"T06:00";
}
document.getElementById("addUser").addEventListener("click", function(){
switch (pathURL){
case "user":
var updateProfil = ["username", "email", "firstName", "birth", "password"];
break;
case "event":
var updateProfil = ["name", "place", "start_date", "end_date"];
break;
default:
var updateProfil = []
break;
}
var updateOk = true;
for (var i=0; i<updateProfil.length; i++){
if(updateProfil[i] == "place"){
size = document.getElementsByClassName("mapboxgl-ctrl-geocoder--input")[0].value.length
} else {
size= document.getElementById(updateProfil[i]+"UpdateInput").value.length
}
if(size == 0){
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;
}
}
}
switch(pathURL){
case "user":
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;
case "event":
organizers = []
listValue = JSON.parse(document.getElementById("organizersUpdateInput").value)
for (var i=0; i<listValue.length; i++){
organizers.push(listValue[i]["value"])
}
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,
"organizers":organizers,
"latitude": document.getElementById("placeUpdateInput").getAttribute("data-latitude"),
"longitude": document.getElementById("placeUpdateInput").getAttribute("data-longitude")
};
break;
default:
dataPut = {};
break;
}
instance({
method : "put",
url:pathURL+"s",
withCredentials: true,
data: dataPut
}).then(function(response){
var idAlert = "";
switch(response.status){
case 201:
idAlert="createUser"
switch (pathURL){
case "user":
input = "username";
break;
case "event":
input = "name";
break;
default:
input = "";
break;
}
var user = document.getElementById(input+"UpdateInput").value;
if(user.length > 0){
document.getElementById("displayUser").textContent=user;
}
break;
case 204:
idAlert="notModified"
break;
default:
idAlert="unknown";
break
}
document.getElementById(idAlert+"Alert").classList.remove("hidden");
}).catch(function(error){
console.log(error);
switch(error.status){
case 401:
listExist = [ "username", "email" ];
for(var i=0; i<listExist.length; i++){
if(error.response.data[listExist[i]]){
document.getElementById(listExist[i]+"ExistAlert").classList.remove("hidden");
}1
}
idAlert="notAuthorized";
break;
case 403:
idAlert="forbidden";
break;
case 403:
idAlert="syntaxe";
break;
case 500:
idAlert="internalServer";
break;
default:
idAlert="unknown";
break;
}
document.getElementById(idAlert+"Alert").classList.remove("hidden");
});
}
});