covas-bo/web/js/users.js
2022-08-25 01:41:06 +02:00

181 lines
5.0 KiB
JavaScript

checkToken();
signOut();
var param = {}
param["nbPages"] = 20;
instance.get("users/count", { withCredentials: true }).then(function(response){
var count = response.data / param["nbPages"];
var reste = response.data % param["nbPages"];
var total = 0;
if(reste != 0){
total = Number.parseInt(count) + 1;
}
if(total > 1){
document.getElementById("page").classList.remove("hidden");
var pageCurrent = getQuery("page");
var page = 1;
if(pageCurrent.length > 0){
page = Number.parseInt(pageCurrent);
}
for (var i=0; i<total; i++){
var liPage = document.querySelectorAll(".page-item")[i+1];
var cloneLi = liPage.cloneNode(true);
liPage.after(cloneLi);
var newPage = i + 2;
var aPage = document.querySelectorAll(".page-item")[newPage].querySelector("a");
aPage.text=newPage;
aPage.href="/html/users.html?page="+newPage;
}
document.querySelectorAll(".page-item")[page].classList.add("active");
if(page != 1){
document.querySelectorAll(".page-item")[0].classList.remove("disabled");
}
if(page == total){
document.querySelectorAll(".page-item")[page].classList.add("disabled");
}
}
});
param["page"] = 0;
var pageCurrent = getQuery("page");
if (pageCurrent.length > 0){
param["page"] = Number.parseInt(pageCurrent);
param["page"] = param["page"] - 1;
}
var listQuery = ["search", "uuid", "roles", "status", "email"];
for (var i=0; i<listQuery.length; i++){
var query = getQuery(listQuery[i]);
if(query.length > 0){
param[listQuery[i]]=query;
}
}
instance.get("users", {withCredentials: true, params :param}).then(function(response){
listData = response.data;
for (var i=0; i<listData.length; i++){
var trTag = document.querySelector("#users tbody tr").cloneNode(true);
if(i != 0){
document.querySelector("#users tbody").append(trTag);
}
var trAll = document.querySelectorAll("#users tbody tr")
td = trAll[i].querySelectorAll("td");
td[0].textContent = listData[i].id;
td[1].textContent = listData[i].pseudo
td[2].textContent = listData[i].email
td[3].textContent = listData[i].roles
var classTd = "";
switch(listData[i].status){
case 1:
classTd = ".activeStatus";
break;
case 0:
classTd = ".disableStatus";
break;
case 2:
classTd = ".confirmStatus";
break;
case -1:
classTd = ".removeStatus";
break;
default:
classTd = ".unknownStatus"
break;
}
td[4].querySelector(".activeStatus").classList.add("hidden");
td[4].querySelector(classTd).classList.remove("hidden");
trAll[i].addEventListener("click", function(e){
var id = e.currentTarget.querySelector("td").textContent;
location.href="/html/user.html?id="+id;
});
}
});
document.getElementById("addUser").addEventListener("click", function(){
location.href="/html/adduser.html";
});
document.getElementById("searchUser").addEventListener("keydown", function(ev){
if(ev.key === "Enter"){
var search = document.getElementById("searchUser").value;
if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(search)){
location.href="/html/users.html?email="+search;
} else if(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(search)){
location.href="/html/users.html?uuid="+search;
} else {
location.href="/html/users.html?search="+search;
}
}
});
document.getElementById("inputStatus").addEventListener("change", function(e){
var search = document.getElementById("searchUser").value;
var select = document.getElementById("inputRoles");
var roles = select.options[select.selectedIndex].value;
var param = "";
var page = getQuery("page");
if(page.length > 0){
param = param + "page="+page+"&";
}
var nbPages = getQuery("nbPages");
if(nbPages.length > 0 ){
param = param+ "nbPages="+nbPages+"&";
}
if(search.length > 0){
param = param + "search="+search+"&";
}
var status = e.target.value;
if(status != "-2"){
param = param + "status="+status+"&";
}
if(roles != "All"){
param = param + "roles="+roles;
}
location.href="/html/users.html?"+param;
});
document.getElementById("inputRoles").addEventListener("change", function(e){
var search = document.getElementById("searchUser").value;
var select = document.getElementById("inputStatus");
var status = select.options[select.selectedIndex].value;
var param = "";
var page = getQuery("page");
if(page.length > 0){
param = param + "page="+page+"&";
}
var nbPages = getQuery("nbPages");
if(nbPages.length > 0 ){
param = param + "nbPages="+nbPages+"&";
}
if(search.length > 0){
param = param + "search="+search+"&";
}
if(status != "-2"){
param = param + "status="+status+"&";
}
var roles = e.target.value;
if(roles != "All"){
param = param + "roles="+roles;
}
console.log(param);
location.href="/html/users.html?"+param;
});