Replace admin report CableReady broadcast with ActionCable

This commit is contained in:
wandji20
2024-10-15 04:44:39 +01:00
parent 7403fa8b6d
commit 88e5aea11a
3 changed files with 21 additions and 17 deletions

View File

@@ -2,7 +2,6 @@
# Renders a report and stores it in a given blob.
class ReportJob < ApplicationJob
include CableReady::Broadcaster
delegate :render, to: ActionController::Base
before_perform :enable_active_storage_urls
@@ -39,21 +38,25 @@ class ReportJob < ApplicationJob
end
def broadcast_result(channel, format, blob)
cable_ready[channel]
.inner_html(
ActionCable.server.broadcast(
channel,
{
selector: "#report-go",
html: Spree::Admin::BaseController.helpers.button(I18n.t(:go), "report__submit-btn")
).inner_html(
selector: "#report-table",
html: actioncable_content(format, blob)
).broadcast
}
)
ActionCable.server.broadcast(
channel,
{ selector: "#report-table", html: actioncable_content(format, blob) }
)
end
def broadcast_error(channel)
cable_ready[channel].inner_html(
selector: "#report-table",
html: I18n.t("report_job.report_failed")
).broadcast
ActionCable.server.broadcast(
channel,
{ selector: "#report-table", html: I18n.t("report_job.report_failed") }
)
end
def actioncable_content(format, blob)

View File

@@ -1,6 +1,5 @@
import { Controller } from "stimulus";
import consumer from "../channels/consumer";
import CableReady from "cable_ready";
export default class extends Controller {
static values = { id: String };
@@ -10,7 +9,9 @@ export default class extends Controller {
{ channel: "ScopedChannel", id: this.idValue },
{
received(data) {
if (data.cableReady) CableReady.perform(data.operations);
if (!data.selector) return;
document.querySelector(data.selector).innerHTML = data.html;
},
}
);

View File

@@ -3,8 +3,6 @@
require 'spec_helper'
RSpec.describe ReportJob do
include CableReady::Broadcaster
let(:report_args) {
{ report_class:, user:, params:, format:, blob: }
}
@@ -41,8 +39,10 @@ RSpec.describe ReportJob do
with_channel = report_args.merge(channel:)
ReportJob.perform_later(**with_channel)
expect(cable_ready[channel]).to receive(:broadcast).and_call_original
expect(ActionCable.server).to receive(:broadcast).with(
channel,
instance_of(Hash)
).twice.and_call_original
expect {
perform_enqueued_jobs(only: ReportJob)