implement better HTTP request types handling,

add live updating of instances using statushash endpoint to reduce data transfer
This commit is contained in:
Arthur Lu 2023-07-11 21:14:54 +00:00
parent 2b161d44d8
commit f8d59fd9c9
2 changed files with 22 additions and 3 deletions

View File

@ -4,6 +4,9 @@ import { PVE } from "../vars.js";
window.addEventListener("DOMContentLoaded", init);
let currentHash;
const refreshRate = 5000;
async function init () {
setTitleAndHeader();
const cookie = document.cookie;
@ -11,13 +14,24 @@ async function init () {
goToPage("login.html");
}
currentHash = (await requestAPI("/cluster/statushash")).data;
await populateInstances();
const addInstanceBtn = document.querySelector("#instance-add");
addInstanceBtn.addEventListener("click", handleInstanceAdd);
window.setInterval(async () => {
const newHash = (await requestAPI("/cluster/statushash")).data;
if (currentHash !== newHash) {
currentHash = newHash;
populateInstances();
}
}, refreshRate);
}
async function populateInstances () {
const newHash = (await requestAPI("/cluster/statushash")).data;
currentHash = newHash;
const resources = await requestPVE("/cluster/resources", "GET");
const instanceContainer = document.getElementById("instance-container");
const instances = [];

View File

@ -192,13 +192,18 @@ export async function requestAPI (path, method, body = null) {
async function request (url, content) {
const response = await fetch(url, content);
const contentType = response.headers.get("Content-Type");
let data = null;
try {
if (contentType.includes("application/json")) {
data = await response.json();
data.status = response.status;
}
catch {
data = null;
else if (contentType.includes("text/html")) {
data = { data: await response.text() };
data.status = response.status;
}
else {
data = response;
}
if (!response.ok) {