Files
openfoodnetwork/app/webpacker/controllers/file_loading_controller.js
David Cook e62b640372 Poll to check when invoice file finished
The BulkInvoiceJob already sends a notification via WebSockets once complete, but sometimes that fails. So this is added on top, just in case.
2024-07-05 08:58:10 +10:00

39 lines
904 B
JavaScript

import { Controller } from "stimulus";
const NOTIFICATION_TIME = 5000; // 5 seconds
const HIDE_CLASS = "hidden";
export default class extends Controller {
static targets = ["loading", "loaded", "link"];
connect() {
this.setTimeout();
}
disconnect() {
this.clearTimeout();
}
checkFile() {
if (!this.loadedTarget.classList.contains(HIDE_CLASS)) {
// If link already loaded successfully, we don't need to check anymore.
return;
}
const response = fetch(this.linkTarget.href).then((response) => {
if (response.status == 200) {
this.loadingTarget.classList.add(HIDE_CLASS);
this.loadedTarget.classList.remove(HIDE_CLASS);
} else {
this.setTimeout();
}
});
}
setTimeout(){
this.timeout = setTimeout(this.checkFile.bind(this), NOTIFICATION_TIME);
}
clearTimeout(){
clearTimeout(this.timeout);
}
}