Broadcast connected app to all user tabs

This commit is contained in:
Maikel Linke
2023-12-14 16:11:03 +11:00
parent e1730f25d6
commit 1d7f96e965
4 changed files with 26 additions and 8 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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