mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
fix: add auto-refresh for processing exports on index page (#715)
Wrap export list in turbo_frame_tag with conditional polling attributes. When exports are pending/processing, page polls every 3 seconds for updates. Add turbo_frame: _top to download/delete buttons for proper frame handling.
This commit is contained in:
64
app/javascript/controllers/polling_controller.js
Normal file
64
app/javascript/controllers/polling_controller.js
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Controller } from "@hotwired/stimulus";
|
||||
|
||||
// Connects to data-controller="polling"
|
||||
// Automatically refreshes a turbo frame at a specified interval
|
||||
export default class extends Controller {
|
||||
static values = {
|
||||
url: String,
|
||||
interval: { type: Number, default: 3000 },
|
||||
};
|
||||
|
||||
connect() {
|
||||
this.startPolling();
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.stopPolling();
|
||||
}
|
||||
|
||||
startPolling() {
|
||||
if (!this.hasUrlValue) return;
|
||||
|
||||
this.poll = setInterval(() => {
|
||||
this.refresh();
|
||||
}, this.intervalValue);
|
||||
}
|
||||
|
||||
stopPolling() {
|
||||
if (this.poll) {
|
||||
clearInterval(this.poll);
|
||||
this.poll = null;
|
||||
}
|
||||
}
|
||||
|
||||
async refresh() {
|
||||
try {
|
||||
const response = await fetch(this.urlValue, {
|
||||
headers: {
|
||||
Accept: "text/vnd.turbo-stream.html",
|
||||
"Turbo-Frame": this.element.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const html = await response.text();
|
||||
const template = document.createElement("template");
|
||||
template.innerHTML = html;
|
||||
|
||||
const newFrame = template.content.querySelector(
|
||||
`turbo-frame#${this.element.id}`,
|
||||
);
|
||||
if (newFrame) {
|
||||
this.element.innerHTML = newFrame.innerHTML;
|
||||
|
||||
// Check if we should stop polling (no more pending/processing exports)
|
||||
if (!newFrame.hasAttribute("data-polling-url-value")) {
|
||||
this.stopPolling();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Polling error:", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user