Skip to content

Commit

Permalink
tidy up js
Browse files Browse the repository at this point in the history
  • Loading branch information
gferraro committed May 22, 2024
1 parent 6c8bf42 commit f9bee9d
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions static/js/audiorecording.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

let countdown = 0;
async function getAudioStatus() {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "/api/audio/audio-status", true);
Expand All @@ -8,8 +9,9 @@ async function getAudioStatus() {
xmlHttp.onload = async function () {
if (xmlHttp.status == 200) {
const state = Number(xmlHttp.response);
let statusText;
let statusText = "";
if (state == 1) {
countdown = 2;
if (lastState) {
clearInterval(intervalId);
lastState = null;
Expand All @@ -22,12 +24,19 @@ async function getAudioStatus() {
"Take Test Recording";
}
} else if (state == 2) {
countdown = 2;
lastState = state;
statusText = "Test Recording Pending";
} else if (state == 3) {
lastState = state;
statusText = "Taking Test Recording";
if (countdown == 0) {
statusText = "Taking Test Recording";
} else {
statusText = `Taking Test Recording in ${countdown}s`;
countdown -= 1;
}
} else {
countdown = 0;
statusText = "unknow state";
clearInterval(intervalId);
intervalId = null;
Expand All @@ -48,14 +57,21 @@ async function getAudioStatus() {
xmlHttp.send();
}

function enableRecButton() {
if (intervalId) {
clearInterval(intervalId);
}
document.getElementById("audio-test-button").removeAttribute("disabled");
document.getElementById("audio-test-button").innerText =
"Take Test Recording";
}
let intervalId = null;
let lastState = null;
async function takeTestRecording() {
document.getElementById("audio-test-button").innerText =
"Making a test recording";
document.getElementById("audio-test-button").setAttribute("disabled", "true");

console.log("making a test recording");
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("PUT", "/api/audio/test-recording", true);
xmlHttp.setRequestHeader("Authorization", "Basic " + btoa("admin:feathers"));
Expand All @@ -64,25 +80,20 @@ async function takeTestRecording() {
if (xmlHttp.status == 200) {
success = xmlHttp.responseText == '"Asked for a test recording"\n';
if (!success) {
clearInterval(intervalId);

document
.getElementById("audio-test-button")
.removeAttribute("disabled");
document.getElementById("audio-test-button").innerText =
"Take Test Recording";
enableRecButton();
updateAudioError(xmlHttp);
} else {
clearInterval(intervalId);
intervalId = setInterval(getAudioStatus, 1000);
}
alert(xmlHttp.responseText);
} else {
console.log(xmlHttp);
enableRecButton();
updateAudioError(xmlHttp);
}
};

xmlHttp.onerror = async function () {
enableRecButton();
updateAudioError(xmlHttp);
};

Expand All @@ -95,14 +106,9 @@ function handleEnableChange(event) {
}

function updateAudio() {
console.log("update Audio Recording");
var data = {};
data["enabled"] = $("#enabledCheck").prop("checked");

// var formData = new FormData();
// formData.append("section", "audio-recording");
// formData.append("config", JSON.stringify(data));

var xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", "/api/audiorecording", true);
xmlHttp.setRequestHeader("Authorization", "Basic " + btoa("admin:feathers"));
Expand All @@ -112,7 +118,6 @@ function updateAudio() {
);
xmlHttp.onload = async function () {
if (xmlHttp.status == 200) {
alert("updated audiorecording");
if ($("#enabledCheck").prop("checked")) {
document
.getElementById("audio-test-button")
Expand All @@ -123,7 +128,6 @@ function updateAudio() {
.setAttribute("disabled", "true");
}
} else {
console.log(xmlHttp);
updateAudioError(xmlHttp);
}
};
Expand Down

0 comments on commit f9bee9d

Please sign in to comment.