Files
openfoodnetwork/app/views/admin/reports/_fallback_display.html.haml
Maikel Linke 23aa762be2 Add fallback report loading in case websockets fail
This also resolves a race condition scenario. Even if the report gets
rendered via websockets before the controller response is rendered then
the fallback script loads the report again. It's not the most beautiful
but probably okay until we replace websockts altogether.

I'm leaving websockets in at the moment because it can render the report
much quicker than polling can.
2024-08-16 15:24:34 +10:00

45 lines
1.5 KiB
Plaintext

.download.hidden
= link_to t("admin.reports.download.button"), file_url, target: "_blank", class: "button icon icon-file"
:javascript
(function () {
const tryDownload = function() {
const link = document.querySelector(".download a");
// If the report was already rendered via web sockets:
if (link == null) return;
fetch(link.href).then((response) => {
if (response.ok) {
response.blob().then((blob) => blob.text()).then((text) => {
const loading = document.querySelector(".loading");
if (loading == null) return;
loading.remove();
document.querySelector("#report-go button").disabled = false;
if (link.href.endsWith(".html")) {
// This replaces the hidden download button with the report:
link.parentElement.outerHTML = text;
} else {
// Or just show the download button when it's ready:
document.querySelector(".download").classList.remove("hidden")
}
});
} else {
setTimeout(tryDownload, 1000);
}
});
}
/*
A lot of reports are rendered within 250ms. Others take at least
2.5 seconds. There's a big gap in between. Observed on:
https://openfoodnetwork.org.au/admin/sidekiq/metrics/ReportJob?period=8h
https://openfoodnetwork.org.uk/admin/sidekiq/metrics/ReportJob?period=8h
https://coopcircuits.fr/admin/sidekiq/metrics/ReportJob?period=8h
*/
setTimeout(tryDownload, 250);
})();