diff --git a/app/reflexes/admin/connected_app_reflex.rb b/app/reflexes/admin/connected_app_reflex.rb index 8bfb83d562..ecfb974d96 100644 --- a/app/reflexes/admin/connected_app_reflex.rb +++ b/app/reflexes/admin/connected_app_reflex.rb @@ -3,10 +3,43 @@ module Admin class ConnectedAppReflex < ApplicationReflex def create - enterprise = Enterprise.find(element.dataset.enterprise_id) authorize! :admin, enterprise + app = ConnectedApp.create!(enterprise_id: enterprise.id) + # Avoid race condition by sending before enqueuing job: + broadcast_partial + + ConnectAppJob.perform_later( + app, current_user.spree_api_key, + channel: SessionChannel.for_request(request), + ) + morph :nothing + end + + def destroy + authorize! :admin, enterprise + + app = enterprise.connected_apps.first + app.destroy + + broadcast_partial + + WebhookDeliveryJob.perform_later( + app.data["destroy"], + "disconnect-app", + nil + ) + morph :nothing + end + + private + + def enterprise + @enterprise ||= Enterprise.find(element.dataset.enterprise_id) + end + + def broadcast_partial selector = "#edit_enterprise_#{enterprise.id} #connected-app-discover-regen" html = ApplicationController.render( partial: "admin/enterprises/form/connected_apps", @@ -15,12 +48,6 @@ module Admin # Avoid race condition by sending before enqueuing job: cable_ready.morph(selector:, html:).broadcast - - ConnectAppJob.perform_later( - app, current_user.spree_api_key, - channel: SessionChannel.for_request(request), - ) - morph :nothing 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 ae816b9999..9559d72bd9 100644 --- a/app/views/admin/enterprises/form/_connected_apps.html.haml +++ b/app/views/admin/enterprises/form/_connected_apps.html.haml @@ -7,7 +7,11 @@ %div - if enterprise.connected_apps.empty? %button{ data: {reflex: "click->Admin::ConnectedApp#create", enterprise_id: enterprise.id} } - = t ".action" + = t ".enable" + - else + %button{ data: {reflex: "click->Admin::ConnectedApp#destroy", enterprise_id: enterprise.id} } + = t ".disable" + .connected-app__connection - if enterprise.connected_apps.present? .connected-app__note diff --git a/config/locales/en.yml b/config/locales/en.yml index b9df0ba1f9..3adaab685c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1283,7 +1283,8 @@ en: legend: "Connected apps" title: "Discover Regenerative" tagline: "Allow website to publish your enterprise information." - action: "Share data" + enable: "Share data" + disable: "Disconnect" saving_changes: "Saving changes" note: | In order for this enterprise to be published, you need to include diff --git a/spec/fixtures/vcr_cassettes/Connected_Apps/can_be_enabled.yml b/spec/fixtures/vcr_cassettes/Connected_Apps/can_be_enabled_and_disabled.yml similarity index 100% rename from spec/fixtures/vcr_cassettes/Connected_Apps/can_be_enabled.yml rename to spec/fixtures/vcr_cassettes/Connected_Apps/can_be_enabled_and_disabled.yml diff --git a/spec/system/admin/enterprises/connected_apps_spec.rb b/spec/system/admin/enterprises/connected_apps_spec.rb index b10ef466cb..1885d2959b 100644 --- a/spec/system/admin/enterprises/connected_apps_spec.rb +++ b/spec/system/admin/enterprises/connected_apps_spec.rb @@ -28,7 +28,7 @@ describe "Connected Apps", feature: :connected_apps, vcr: true do expect(page).to have_content "CONNECTED APPS" end - it "can be enabled" do + it "can be enabled and disabled" do visit edit_admin_enterprise_path(enterprise) scroll_to :bottom @@ -43,5 +43,11 @@ describe "Connected Apps", feature: :connected_apps, vcr: true do expect(page).to_not have_content "Saving changes" expect(page).to have_content "include regenerative details" expect(page).to have_link "Update details" + + click_button "Disconnect" + expect(page).to have_button "Share data" + expect(page).to_not have_button "Disconnect" + expect(page).to_not have_content "include regenerative details" + expect(page).to_not have_link "Update details" end end