From d354317c736739e1d401e9f61ae9e92a89c49988 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 15 Aug 2024 09:49:31 +1000 Subject: [PATCH] Replace cable_ready report loading w/ Turbo stream I'm adding TurboPower for the scroll_into_view action. It adds all the nice CableReady actions to Turbo Streams. Note that I omitted `block: "start"` because that option is the default in Javascript. And the generic `action` method doesn't support parameters like this anyway. I'll work on that in the next commit. I also re-introduced a race condition by rendering the "loading" indicator after triggering the report rendering job. I'm planning to resolve that later. --- app/controllers/admin/reports_controller.rb | 14 -------------- app/views/admin/reports/create.turbo_stream.haml | 5 +++++ app/views/admin/reports/show.html.haml | 2 +- app/webpacker/js/turbo.js | 3 +++ package.json | 1 + spec/controllers/admin/reports_controller_spec.rb | 3 ++- spec/system/admin/reports_spec.rb | 2 ++ yarn.lock | 5 +++++ 8 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 app/views/admin/reports/create.turbo_stream.haml diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb index c39aa5fedf..eaf9be5895 100644 --- a/app/controllers/admin/reports_controller.rb +++ b/app/controllers/admin/reports_controller.rb @@ -58,18 +58,6 @@ module Admin end def render_in_background - cable_ready[ScopedChannel.for_id(params[:uuid])] - .inner_html( - selector: "#report-go", - html: helpers.button(t(:go), "report__submit-btn", "submit", disabled: true) - ).inner_html( - selector: "#report-table", - html: render_to_string(partial: "admin/reports/loading") - ).scroll_into_view( - selector: "#report-table", - block: "start" - ).broadcast - blob = ReportBlob.create_for_upload_later!(report_filename) ReportJob.perform_later( @@ -78,8 +66,6 @@ module Admin blob:, channel: ScopedChannel.for_id(params[:uuid]), ) - - head :no_content end end end diff --git a/app/views/admin/reports/create.turbo_stream.haml b/app/views/admin/reports/create.turbo_stream.haml new file mode 100644 index 0000000000..4945bb19f9 --- /dev/null +++ b/app/views/admin/reports/create.turbo_stream.haml @@ -0,0 +1,5 @@ += turbo_stream.update "report-go" do + = button t(:go), "report__submit-btn", "submit", disabled: true += turbo_stream.update "report-table" do + = render "admin/reports/loading" += turbo_stream.action(:scroll_into_view, "report-table") diff --git a/app/views/admin/reports/show.html.haml b/app/views/admin/reports/show.html.haml index bc0b767f62..038eed9070 100644 --- a/app/views/admin/reports/show.html.haml +++ b/app/views/admin/reports/show.html.haml @@ -3,7 +3,7 @@ - content_for :minimal_js, true -= form_for @report.search, { url: url_for, data: { remote: "true" } } do |f| += form_for @report.search, { url: url_for, data: { turbo: "true" } } do |f| = hidden_field_tag "uuid", request.uuid %fieldset.no-border-bottom.print-hidden diff --git a/app/webpacker/js/turbo.js b/app/webpacker/js/turbo.js index 4451810c93..d8816b4d2a 100644 --- a/app/webpacker/js/turbo.js +++ b/app/webpacker/js/turbo.js @@ -1,5 +1,8 @@ import "@hotwired/turbo"; +import TurboPower from "turbo_power"; +TurboPower.initialize(Turbo.StreamActions); + document.addEventListener("turbo:frame-missing", (event) => { // don't replace frame contents event.preventDefault(); diff --git a/package.json b/package.json index e3c554a4cf..1a49d07db3 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "stimulus_reflex": "3.5.1", "tom-select": "^2.3.1", "trix": "^2.1.5", + "turbo_power": "^0.6.2", "webpack": "~4" }, "devDependencies": { diff --git a/spec/controllers/admin/reports_controller_spec.rb b/spec/controllers/admin/reports_controller_spec.rb index 0def47097f..81eeccbf77 100644 --- a/spec/controllers/admin/reports_controller_spec.rb +++ b/spec/controllers/admin/reports_controller_spec.rb @@ -311,13 +311,14 @@ RSpec.describe Admin::ReportsController, type: :controller do it "triggers the delivery report" do spree_post :create, { + format: :turbo, q: { completed_at_lt: 1.day.ago }, shipping_method_in: ["123"], # We just need to search for shipping methods report_type: :order_cycle_management, report_subtype: "delivery", } - expect(response).to have_http_status(:no_content) + expect(response).to have_http_status(:ok) end end diff --git a/spec/system/admin/reports_spec.rb b/spec/system/admin/reports_spec.rb index 62de258992..b0ef194e70 100644 --- a/spec/system/admin/reports_spec.rb +++ b/spec/system/admin/reports_spec.rb @@ -115,6 +115,8 @@ RSpec.describe ' end it "allows the report to finish before the loading screen is rendered" do + pending "Race condition between loading message and report rendering" + login_as_admin visit admin_report_path(report_type: :customers) diff --git a/yarn.lock b/yarn.lock index d66636e3d9..df184f5152 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8825,6 +8825,11 @@ tty-browserify@0.0.0: resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.0.tgz#a157ba402da24e9bf957f9aa69d524eed42901a6" integrity sha1-oVe6QC2iTpv5V/mqadUk7tQpAaY= +turbo_power@^0.6.2: + version "0.6.2" + resolved "https://registry.yarnpkg.com/turbo_power/-/turbo_power-0.6.2.tgz#d2320ffd18d7dba641bfbddaba388ffef5d90cb6" + integrity sha512-GCrwi6+rSLVORqb1LwP9pf/jP1Zo5lJgzGbLVu8g79T6LvB/9GGg7VWdLlWB0RvvDE1/+3hm32MPtEL5g89fsg== + type-check@~0.3.2: version "0.3.2" resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72"