handle update element in listener

This commit is contained in:
Arthur Lu 2022-12-17 16:34:40 -08:00
parent 3d902a745d
commit 46da693292

View File

@ -1,5 +1,7 @@
import { request } from "./utils.js"; import { request } from "./utils.js";
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
class Instance extends HTMLElement { class Instance extends HTMLElement {
constructor () { constructor () {
super(); super();
@ -58,9 +60,21 @@ class Instance extends HTMLElement {
let powerButton = this.shadowElement.querySelector("#power-btn"); let powerButton = this.shadowElement.querySelector("#power-btn");
powerButton.src = data.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg"; powerButton.src = data.status === "running" ? "images/actions/stop.svg" : "images/actions/start.svg";
powerButton.addEventListener("click", () => { powerButton.addEventListener("click", async () => {
let targetAction = this.status === "running" ? "shutdown" : "start"; 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 targetStatus = this.status === "running" ? "stopped" : "running";
await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/${targetAction}`, "POST", {node: this.node, vmid: this.vmid});
while (true) {
let data = await request(`/nodes/${this.node}/${this.type}/${this.vmid}/status/current`);
console.log(data);
if(data.data.status === targetStatus) {
break;
}
waitFor(1000);
}
this.status = targetStatus;
let typeImg = this.shadowElement.querySelector("#instance-type");
typeImg.src = `images/instances/${this.type}/${this.status}.svg`;
}); });
let configButton = this.shadowElement.querySelector("#configure-btn"); let configButton = this.shadowElement.querySelector("#configure-btn");