Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid submitting duplicate Connected Apps #12481

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/controllers/admin/connected_apps_controller.rb
Original file line number Diff line number Diff line change
@@ -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
53 changes: 0 additions & 53 deletions app/reflexes/admin/connected_app_reflex.rb

This file was deleted.

6 changes: 2 additions & 4 deletions app/views/admin/enterprises/form/_connected_apps.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, the new buttons are a form within a form which is invalid HTML, but it works.

That doesn't seem ideal to me, and might have unexpected drawbacks. But I can't think of a better, so if it works, it works!

I'm guessing if you have other unsaved changes on the Enterprise (from another tab), it will display a confirmation as usual? That could be a little confusing for users, but I think that would be rare and fine for this case.
I'm not sure if the enterprise fields would also be submitted by this button, but even if they are, I expect they'd be silently ignored.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be the cause for the latest bug:

My tests show that it was this commit.

- 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?
Expand Down
2 changes: 2 additions & 0 deletions config/routes/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
patch :register
end

resources :connected_apps, only: [:create, :destroy]

resources :producer_properties do
post :update_positions, on: :collection
end
Expand Down
Loading