diff --git a/app/models/connected_app.rb b/app/models/connected_app.rb index 0553bbffa2..3edc7c4147 100644 --- a/app/models/connected_app.rb +++ b/app/models/connected_app.rb @@ -5,4 +5,7 @@ # Here we store keys and links to access the app. class ConnectedApp < ApplicationRecord belongs_to :enterprise + + scope :connecting, -> { where(data: nil) } + scope :ready, -> { where.not(data: nil) } end 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..c60a7da1a1 100644 --- a/app/views/admin/enterprises/form/_connected_apps.html.haml +++ b/app/views/admin/enterprises/form/_connected_apps.html.haml @@ -7,21 +7,24 @@ %div - if enterprise.connected_apps.empty? %button{ data: {reflex: "click->Admin::ConnectedApp#create", enterprise_id: enterprise.id} } - = t ".action" + = t ".enable" + - elsif enterprise.connected_apps.connecting.present? + %button{ disabled: true } + %i.spinner.fa.fa-spin.fa-circle-o-notch + + = t ".loading" + - else + %button{ data: {reflex: "click->Admin::ConnectedApp#destroy", enterprise_id: enterprise.id} } + = t ".disable" + .connected-app__connection - - if enterprise.connected_apps.present? + - if enterprise.connected_apps.ready.present? .connected-app__note - - link = enterprise.connected_apps[0].data&.fetch("link", false) - - if link - %p= t ".note" - %div - %a{ href: link, target: "_blank", class: "button" } - = t ".link_label" - - else - %p - %i.spinner.fa.fa-spin.fa-circle-o-notch - - = t ".saving_changes" + - link = enterprise.connected_apps[0].data["link"] + %p= t ".note" + %div + %a{ href: link, target: "_blank", class: "button secondary" } + = t ".link_label" %hr .connected-app__description diff --git a/app/webpacker/css/admin/connected_apps.scss b/app/webpacker/css/admin/connected_apps.scss index efcf8d16f3..c2cb2c9421 100644 --- a/app/webpacker/css/admin/connected_apps.scss +++ b/app/webpacker/css/admin/connected_apps.scss @@ -1,3 +1,7 @@ +#connected_apps_panel { + max-width: 615px; +} + .connected-app__head { display: flex; justify-content: space-between; diff --git a/app/webpacker/css/admin_v3/globals/variables.scss b/app/webpacker/css/admin_v3/globals/variables.scss index 48dc0ab62d..f9e487290d 100644 --- a/app/webpacker/css/admin_v3/globals/variables.scss +++ b/app/webpacker/css/admin_v3/globals/variables.scss @@ -12,7 +12,7 @@ $base-font-family: "Open Sans", "Helvetica Neue", "Helvetica", Helvetica, Arial, // Body base colors $color-body-bg: $white !default; $color-body-text: $near-black !default; -$color-headers: $dark-blue !default; +$color-headers: $near-black !default; $color-link: $teal !default; $color-link-hover: $orient !default; $color-link-active: $dark-blue !default; diff --git a/app/webpacker/css/admin_v3/shared/layout.scss b/app/webpacker/css/admin_v3/shared/layout.scss index addd363521..794e51f907 100644 --- a/app/webpacker/css/admin_v3/shared/layout.scss +++ b/app/webpacker/css/admin_v3/shared/layout.scss @@ -11,10 +11,6 @@ padding: 15px 0; margin-top: 25px; - h1 { - color: $near-black; - } - .ofn-drop-down { border: 0; background-color: $spree-blue; diff --git a/config/locales/en.yml b/config/locales/en.yml index ef94e36501..ef1f1f4595 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1286,21 +1286,26 @@ en: connected_apps: legend: "Connected apps" title: "Discover Regenerative" - tagline: "Allow website to publish your enterprise information." - action: "Share data" - saving_changes: "Saving changes" + tagline: "Allow Discover Regenerative to publish your enterprise information." + enable: "Allow data sharing" + disable: "Stop sharing" + loading: "Loading" note: | - In order for this enterprise to be published, you need to include - regenerative details and accept the Terms and Conditions. - link_label: "Update details" + Your Open Food Network account is connected to Discover Regenerative. + Add or update information on your Discover Regenerative listing here. + link_label: "Manage listing" description_html: |
- Discover Regenerative makes it easier for buyers to discover - regenerative produce for their procurement, showcase producers that - are using regenerative farming practices, support connection - between buyers and producers within a trusted network. + Eligible producers can showcase their regenerative credentials, + farming practices and more through a profile listing. + Simplifying how buyers can find regenerative produce and connect + with producers of interest. +
+ - actions: edit_profile: Settings properties: Properties 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..2b61f6466f 100644 --- a/spec/system/admin/enterprises/connected_apps_spec.rb +++ b/spec/system/admin/enterprises/connected_apps_spec.rb @@ -28,20 +28,26 @@ 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 click_link "Connected apps" expect(page).to have_content "Discover Regenerative" - click_button "Share data" - expect(page).to_not have_button "Share data" - expect(page).to have_content "Saving changes" + click_button "Allow data sharing" + expect(page).to_not have_button "Allow data sharing" + expect(page).to have_button "Loading", disabled: true perform_enqueued_jobs(only: ConnectAppJob) - expect(page).to_not have_content "Saving changes" - expect(page).to have_content "include regenerative details" - expect(page).to have_link "Update details" + expect(page).to_not have_button "Loading", disabled: true + expect(page).to have_content "account is connected" + expect(page).to have_link "Manage listing" + + click_button "Stop sharing" + expect(page).to have_button "Allow data sharing" + expect(page).to_not have_button "Stop sharing" + expect(page).to_not have_content "account is connected" + expect(page).to_not have_link "Manage listing" end end