From 4153d7f76c05884e62fd870a8496914e056a21f3 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Sat, 17 Dec 2022 16:07:18 -0800 Subject: [PATCH] run instance population every second --- scripts/elements.js | 6 ++---- scripts/index.js | 8 +++++++- scripts/utils.js | 6 +++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/scripts/elements.js b/scripts/elements.js index c766e0c..23f8573 100644 --- a/scripts/elements.js +++ b/scripts/elements.js @@ -59,10 +59,8 @@ class Instance extends HTMLElement { let powerButton = this.shadowElement.querySelector("#power-btn"); powerButton.src = data.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg"; powerButton.addEventListener("click", () => { - let targetState = this.status == "running" ? "shutdown" : "start"; - let data = request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/${targetState}`, "POST", {node: this.node, vmid: this.vmid}); - console.log(data); - this.status = this.status === "running" ? "stopped" : "running"; + let targetAction = this.status === "running" ? "shutdown" : "start"; + request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node, vmid: this.vmid}); }); let configButton = this.shadowElement.querySelector("#configure-btn"); diff --git a/scripts/index.js b/scripts/index.js index 4e086c1..9c2beb5 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -3,8 +3,13 @@ import {requestTicket, setTicket, request} from "./utils.js"; window.addEventListener("DOMContentLoaded", init); async function init () { + await populateInstances(); + setInterval(populateInstances, 1000); +} + +async function populateInstances () { let cookie = document.cookie; - if (cookie === '') { + if (cookie === "") { window.location.href = "login.html"; } @@ -33,6 +38,7 @@ async function init () { instances.sort((a, b) => (a.vmid > b.vmid) ? 1 : -1); + instanceContainer.innerText = ""; for(let i = 0; i < instances.length; i++) { let newInstance = document.createElement("instance-article"); newInstance.data = instances[i]; diff --git a/scripts/utils.js b/scripts/utils.js index 8d05dc4..b302b78 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -15,13 +15,13 @@ export class NetworkError extends Error { function getCookie(cname) { let name = cname + "="; let decodedCookie = decodeURIComponent(document.cookie); - let ca = decodedCookie.split(';'); + let ca = decodedCookie.split(";"); for(let i = 0; i