Skip to content

Commit

Permalink
Use turbo frames for remote forms
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Feb 2, 2024
1 parent b7f1b0d commit d57c989
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 31 deletions.
23 changes: 11 additions & 12 deletions app/assets/javascripts/alchemy/alchemy.dialog.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,20 @@ class window.Alchemy.Dialog
# Initializes the Dialog body
init: ->
Alchemy.GUI.init(@dialog_body)
@watch_remote_forms()
@watch_turbo_frames()

# Watches ajax requests inside of dialog body and replaces the content accordingly
watch_remote_forms: ->
form = $('[data-remote="true"]', @dialog_body)
form.bind "ajax:success", (event) =>
xhr = event.detail[2]
content_type = xhr.getResponseHeader('Content-Type')
if content_type.match(/javascript/)
return
else
@dialog_body.html(xhr.responseText)
@init()
watch_turbo_frames: ->
frame = $('turbo-frame[id="alchemy-form"]', @dialog_body)
frame.bind "turbo:before-fetch-response", (event) =>
response = event.detail.fetchResponse.response
if response.redirected
event.preventDefault()
@options.closed = => Turbo.visit(response.url)
@close()
return
form.bind "ajax:error", (event, b, c) =>
return
frame.bind "turbo:fetch-request-error", (event, b, c) =>
statusText = event.detail[1]
xhr = event.detail[2]
@show_error(xhr, statusText)
Expand Down
9 changes: 3 additions & 6 deletions app/controllers/alchemy/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def leave

private

# Disable layout rendering for xhr requests.
# Disable layout rendering for xhr or turbo frame requests.
def set_layout
request.xhr? ? false : "alchemy/admin"
(request.xhr? || turbo_frame_request?) ? false : "alchemy/admin"
end

# Handles exceptions
Expand Down Expand Up @@ -108,10 +108,7 @@ def render_errors_or_redirect(object, redirect_url, flash_notice)
#
def do_redirect_to(url_or_path)
respond_to do |format|
format.js {
@redirect_url = url_or_path
render :redirect
}
format.turbo_stream { redirect_to url_or_path }
format.html { redirect_to url_or_path }
end
end
Expand Down
5 changes: 3 additions & 2 deletions app/helpers/alchemy/admin/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ module FormHelper
def alchemy_form_for(object, *args, &block)
options = args.extract_options!
options[:builder] = Alchemy::Forms::Builder
options[:remote] = request.xhr?
options[:html] = {
id: options.delete(:id),
class: ["alchemy", options.delete(:class)].compact.join(" ")
}
simple_form_for(object, *(args << options), &block)
turbo_frame_tag("alchemy-form") do
simple_form_for(object, *(args << options), &block)
end
end
end
end
Expand Down
11 changes: 0 additions & 11 deletions app/views/alchemy/base/redirect.js.erb

This file was deleted.

0 comments on commit d57c989

Please sign in to comment.