setup: Perform a check for app availability after the page loads

- Using AJAX request instead of loading the initial page slowly.

Tests:

- Unit tests passes.

- Deluge app is not available in bookworm and is available in Trixie.

- When app is available, no message is shown. Install button is enabled.

- When app is not available a proper warning alert message is shown. Install
button is disabled.

- During check for the availability, the progress message is shown. Install
button is disabled.

- When Javascript is disabled on the page, no availability check is performed.
Install button is enabled.

- When an exception is raised in the is-available view, error message is shown.
Install button is enabled.

- When is-available view return HTML response, error message is shown. Install
button is enabled.

- When is-available view invalid JSON response, error message is shown. Install
button is enabled.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Joseph Nuthalapati <njoseph@riseup.net>
This commit is contained in:
Sunil Mohan Adapa authored and Joseph Nuthalapati committed 2025-08-02 21:06:17 +05:30
1 parent 36c36dad8d
commit 1f98dfcad1
9 files changed
+158 -48

No files matched your search

+69
View File
@@ -206,3 +206,72 @@ function setSelectAllValue(parent) {
parent.querySelector('.select-all').checked = enableSelectAll;
}
/*
* Check whether an app is available on its setup page.
*/
document.addEventListener('DOMContentLoaded', function(event) {
const checkingElement = document.querySelector('.app-checking-availability');
if (!checkingElement)
return;
// App does not need setup, it likely needs upgrade
const setupState = checkingElement.getAttribute('data-setup-state');
if (setupState !== 'needs-setup')
return;
const appId = checkingElement.getAttribute('data-app-id');
checkingElement.classList.remove('d-none');
function setInstallButtonState(enable) {
const installButton = document.querySelector('.install-button');
if (enable) {
installButton.removeAttribute('disabled');
} else {
installButton.setAttribute('disabled', 'disabled');
}
}
function unavailable() {
document.querySelector('.app-unavailable').classList.remove('d-none');
setInstallButtonState(false);
}
function error() {
const element = document.querySelector('.app-checking-availability-error');
element.classList.remove('d-none');
checkingElement.classList.add('d-none');
setInstallButtonState(true); // Allow trying installation
}
let request = new XMLHttpRequest();
request.timeout = 2 * 60 * 1000; // 2 minutes
request.onload = function() {
// Remove the progress spinner
checkingElement.classList.add('d-none');
let available = false;
if (this.status === 200) {
try {
const response = JSON.parse(this.responseText);
if (response.is_available === true) {
setInstallButtonState(true);
} else if (response.is_available === false) {
unavailable();
} else {
error();
}
} catch (e) {
error();
}
} else {
error();
}
};
request.onerror = error;
request.ontimeout = error;
request.open('GET', `/plinth/is-available/${appId}/`, true);
setInstallButtonState(false);
request.send();
});