diff --git a/app/jobs/connect_app_job.rb b/app/jobs/connect_app_job.rb index f439f3b46e..5be6b4f56a 100644 --- a/app/jobs/connect_app_job.rb +++ b/app/jobs/connect_app_job.rb @@ -1,15 +1,28 @@ # frozen_string_literal: true class ConnectAppJob < ApplicationJob - def perform(app, token) + include CableReady::Broadcaster + + def perform(app, token, channel: nil) url = "https://n8n.openfoodnetwork.org.uk/webhook/regen/connect-enterprise" event = "connect-app" + enterprise = app.enterprise payload = { - enterprise_id: app.enterprise_id, + enterprise_id: enterprise.id, access_token: token, } response = WebhookDeliveryJob.perform_now(url, event, payload) app.update!(data: JSON.parse(response)) + + return unless channel + + selector = "#edit_enterprise_#{enterprise.id} #connected_apps_panel div" + html = ApplicationController.render( + partial: "admin/enterprises/form/connected_apps", + locals: { enterprise: }, + ) + + cable_ready[channel].morph(selector:, html:).broadcast end end diff --git a/app/reflexes/admin/connected_app_reflex.rb b/app/reflexes/admin/connected_app_reflex.rb index 9f125f18eb..50f0df0e32 100644 --- a/app/reflexes/admin/connected_app_reflex.rb +++ b/app/reflexes/admin/connected_app_reflex.rb @@ -6,7 +6,11 @@ module Admin enterprise = Enterprise.find(element.dataset.enterprise_id) authorize! :admin, enterprise app = ConnectedApp.create!(enterprise_id: enterprise.id) - ConnectAppJob.perform_later(app, current_user.spree_api_key) + + ConnectAppJob.perform_later( + app, current_user.spree_api_key, + channel: SessionChannel.for_request(request), + ) end end end diff --git a/app/views/admin/enterprises/form/_connected_apps.html.haml b/app/views/admin/enterprises/form/_connected_apps.html.haml index 8298c1307c..a370b91380 100644 --- a/app/views/admin/enterprises/form/_connected_apps.html.haml +++ b/app/views/admin/enterprises/form/_connected_apps.html.haml @@ -1,16 +1,17 @@ +- enterprise ||= f.object %div{ data: {reflex: {root: ".connected-app__head, .connected-app__connection"}} } .connected-app__head %div %h3= t ".title" %p= t ".tagline" %div - - if @enterprise.connected_apps.empty? - %button{ data: {reflex: "click->Admin::ConnectedApp#create", enterprise_id: @enterprise.id} } + - if enterprise.connected_apps.empty? + %button{ data: {reflex: "click->Admin::ConnectedApp#create", enterprise_id: enterprise.id} } = t ".action" .connected-app__connection - - if @enterprise.connected_apps.present? + - if enterprise.connected_apps.present? .connected-app__note - - link = @enterprise.connected_apps[0].data&.fetch("link", false) + - link = enterprise.connected_apps[0].data&.fetch("link", false) - if link %p= t ".note" %div diff --git a/spec/system/admin/enterprises/connected_apps_spec.rb b/spec/system/admin/enterprises/connected_apps_spec.rb index f7b2ddb81e..b10ef466cb 100644 --- a/spec/system/admin/enterprises/connected_apps_spec.rb +++ b/spec/system/admin/enterprises/connected_apps_spec.rb @@ -40,7 +40,7 @@ describe "Connected Apps", feature: :connected_apps, vcr: true do expect(page).to have_content "Saving changes" perform_enqueued_jobs(only: ConnectAppJob) - page.refresh # TODO: update via cable_ready + expect(page).to_not have_content "Saving changes" expect(page).to have_content "include regenerative details" expect(page).to have_link "Update details" end