function loadAdminPage() {
var html = `
Wrapperr users
`;
document.getElementById("setup").innerHTML = html;
getUsers();
}
function getUsers() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
alert('Failed to parse API response.');
console.log('Failed to parse API response. Error: ' + this.responseText);
return;
}
if(result.error) {
alert(result.error);
} else {
placeUsers(result.data);
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "get/users");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.setRequestHeader("Authorization", cookie);
xhttp.send();
return;
}
function placeUsers(usersArray) {
userModule = document.getElementById("users-module")
userModule.innerHTML = `
`;
usersArray.forEach(user => {
var historyDiv = "";
if(user.wrappings.length > 0) {
historyDiv = `
`;
}
var active_state_class = ""
if(user.user_active) {
active_state_class = "user-active-true"
} else {
active_state_class = "user-active-false"
}
var tautulliServersString = ""
user.user_tautulli_servers.forEach(server => {
tautulliServersString += `${server}
`
});
var html = `
${user.user_id}
${user.user_name}
${user.user_friendly_name}
${user.user_email}
${tautulliServersString}
${historyDiv}
`;
userModule.innerHTML += html;
});
}
function getHistory(userId) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
alert('Failed to parse API response.');
console.log('Failed to parse API response. Error: ' + this.responseText);
return;
}
if(result.error) {
alert(result.error);
} else {
placeHistory(result.data);
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "get/users/" + userId);
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.setRequestHeader("Authorization", cookie);
xhttp.send();
return;
}
function placeHistory(userObject) {
var modal = document.getElementById("myModal");
var modalContent = document.getElementById("modal-content-real");
modal.style.display = "block";
userObject.wrappings.forEach(entry => {
var date = new Date(entry.entry_date);
var html = `
${date}
${entry.entry_ip}
`;
modalContent.innerHTML += html;
});
}
function closeModal() {
var modal = document.getElementById("myModal");
var modalContent = document.getElementById("modal-content-real");
modal.style.display = "none";
modalContent.innerHTML = "";
}
function syncTautulli() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
try {
var result= JSON.parse(this.responseText);
} catch(error) {
alert('Failed to parse API response.');
console.log('Failed to parse API response. Error: ' + this.responseText);
return;
}
if(result.error) {
alert(result.error);
} else {
placeUsers(result.data);
alert(result.message)
}
}
};
xhttp.withCredentials = true;
xhttp.open("post", api_url + "sync/users");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.setRequestHeader("Authorization", cookie);
xhttp.send();
return;
}