Merge pull request #12481 from mkllnk/connect-app-once

Avoid submitting duplicate Connected Apps
This commit is contained in:
Maikel
2024-05-23 08:50:34 +10:00
committed by GitHub
4 changed files with 47 additions and 57 deletions

View File

@@ -0,0 +1,43 @@
# frozen_string_literal: true
module Admin
class ConnectedAppsController < ApplicationController
def create
authorize! :admin, enterprise
app = ConnectedApp.create!(enterprise_id: enterprise.id)
ConnectAppJob.perform_later(
app, spree_current_user.spree_api_key,
channel: SessionChannel.for_request(request),
)
render_panel
end
def destroy
authorize! :admin, enterprise
app = enterprise.connected_apps.first
app.destroy
WebhookDeliveryJob.perform_later(
app.data["destroy"],
"disconnect-app",
nil
)
render_panel
end
private
def enterprise
@enterprise ||= Enterprise.find(params.require(:enterprise_id))
end
def render_panel
redirect_to "#{edit_admin_enterprise_path(enterprise)}#/connected_apps_panel"
end
end
end

View File

@@ -1,53 +0,0 @@
# frozen_string_literal: true
module Admin
class ConnectedAppReflex < ApplicationReflex
def create
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",
locals: { enterprise: },
)
# Avoid race condition by sending before enqueuing job:
cable_ready.morph(selector:, html:).broadcast
end
end
end

View File

@@ -6,16 +6,14 @@
%p= t ".tagline"
%div
- if enterprise.connected_apps.empty?
%button{ data: {reflex: "click->Admin::ConnectedApp#create", enterprise_id: enterprise.id} }
= t ".enable"
= button_to t(".enable"), admin_enterprise_connected_apps_path(enterprise.id), method: :post
- elsif enterprise.connected_apps.connecting.present?
%button{ disabled: true }
%i.spinner.fa.fa-spin.fa-circle-o-notch
&nbsp;
= t ".loading"
- else
%button{ data: {reflex: "click->Admin::ConnectedApp#destroy", enterprise_id: enterprise.id} }
= t ".disable"
= button_to t(".disable"), admin_enterprise_connected_app_path(0, enterprise_id: enterprise.id), method: :delete
.connected-app__connection
- if enterprise.connected_apps.ready.present?

View File

@@ -35,6 +35,8 @@ Openfoodnetwork::Application.routes.draw do
patch :register
end
resources :connected_apps, only: [:create, :destroy]
resources :producer_properties do
post :update_positions, on: :collection
end