2024-11-04 07:06:04 -05:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
|
|
|
|
import consumer from "../channels/consumer";
|
|
|
|
|
|
|
|
|
|
export default class extends Controller {
|
|
|
|
|
static targets = ["index"];
|
|
|
|
|
|
|
|
|
|
connect() {
|
2024-11-07 07:07:54 -05:00
|
|
|
if (!this.hasIndexTarget) {
|
|
|
|
|
console.log("No index target found, skipping subscription")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 07:06:04 -05:00
|
|
|
this.setupSubscription();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setupSubscription() {
|
|
|
|
|
const userId = this.element.dataset.userId;
|
|
|
|
|
|
|
|
|
|
this.channel = consumer.subscriptions.create(
|
|
|
|
|
{ channel: "ImportsChannel" },
|
|
|
|
|
{
|
|
|
|
|
connected: () => {
|
|
|
|
|
},
|
|
|
|
|
disconnected: () => {
|
|
|
|
|
},
|
|
|
|
|
received: (data) => {
|
|
|
|
|
const row = this.element.querySelector(`tr[data-import-id="${data.import.id}"]`);
|
|
|
|
|
|
|
|
|
|
if (row) {
|
|
|
|
|
const pointsCell = row.querySelector('[data-points-count]');
|
|
|
|
|
if (pointsCell) {
|
|
|
|
|
pointsCell.textContent = new Intl.NumberFormat().format(data.import.points_count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
|
if (this.channel) {
|
|
|
|
|
this.channel.unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|