mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Simple Rails forms prevent double-clicking on submit already. Converting the StimulusReflex interaction to a simple form submit to a controller solves the race condition. The UX is slightly worse because the whole page is reloaded instead rendering only the connected app panel. But we can solve that when we add more apps and want to activate them independently. By then, we may have good patterns for working with Turbo. Technically, the new buttons are a form within a form which is invalid HTML, but it works.
44 lines
883 B
Ruby
44 lines
883 B
Ruby
# 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
|