Skip to content

Commit

Permalink
Make redirects from edit and new routes work with turbo frame
Browse files Browse the repository at this point in the history
In order to make redirects to the index route from an edit or new
form and being able to re-display the form with errors in case of
failed validation, we need to explicitely set the request format to html, otherwise turbo will refresh the edit/new page.
  • Loading branch information
tvdeyen committed Dec 23, 2024
1 parent 6df1a03 commit 6770fa3
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 41 deletions.
1 change: 1 addition & 0 deletions admin/app/components/solidus_admin/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def self.stimulus_id
end

delegate :stimulus_id, to: :class
delegate :turbo_frame_request?, :search_filter_params, to: :helpers

class << self
private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= turbo_frame_tag :edit_return_reason_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @return_reason, url: solidus_admin.return_reason_path(@return_reason), html: { id: form_id } do |f| %>
<%= turbo_frame_tag :edit_return_reason_modal, target: "_top" do %>
<%= render component("ui/modal").new(title: t(".title"), closable: closable?) do |modal| %>
<%= form_for @return_reason, url: form_url, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
<label class="flex gap-2 items-center">
Expand All @@ -15,9 +15,18 @@
</label>
</div>
<% modal.with_actions do %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<% if closable? %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<% else %>
<%= render component("ui/button").new(
tag: :a,
href: back_url,
scheme: :secondary,
text: t('.cancel')
) %>
<% end %>
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
<% end %>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,18 @@ def initialize(return_reason:)
def form_id
dom_id(@return_reason, "#{stimulus_id}_edit_return_reason_form")
end

def form_url
solidus_admin.return_reason_path(@return_reason, **search_filter_params)
end

def closable?
return false if request.referer.include? "/edit"

turbo_frame_request? || @return_reason.errors.any?
end

def back_url
request.referer || solidus_admin.return_reasons_path
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def search_key
end

def edit_path(return_reason)
spree.edit_admin_return_reason_path(return_reason)
spree.edit_admin_return_reason_path(return_reason, **search_filter_params)
end

def turbo_frames
Expand All @@ -28,7 +28,7 @@ def page_actions
render component("ui/button").new(
tag: :a,
text: t('.add'),
href: solidus_admin.new_return_reason_path,
href: solidus_admin.new_return_reason_path(**search_filter_params),
data: { turbo_frame: :new_return_reason_modal, turbo_prefetch: false },
icon: "add-line",
class: "align-self-end w-full",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= turbo_frame_tag :new_return_reason_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @return_reason, url: solidus_admin.return_reasons_path, html: { id: form_id } do |f| %>
<%= turbo_frame_tag :new_return_reason_modal, target: "_top" do %>
<%= render component("ui/modal").new(title: t(".title"), closable: closable?) do |modal| %>
<%= form_for @return_reason, url: form_url, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
<label class="flex gap-2 items-center">
Expand All @@ -15,9 +15,18 @@
</label>
</div>
<% modal.with_actions do %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<% if closable? %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<% else %>
<%= render component("ui/button").new(
tag: :a,
href: back_url,
scheme: :secondary,
text: t('.cancel')
) %>
<% end %>
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
<% end %>
<% end %>
Expand Down
14 changes: 14 additions & 0 deletions admin/app/components/solidus_admin/return_reasons/new/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,18 @@ def initialize(return_reason:)
def form_id
dom_id(@return_reason, "#{stimulus_id}_new_return_reason_form")
end

def form_url
solidus_admin.return_reasons_path(**search_filter_params)
end

def closable?
return false if request.referer.include? "/new"

turbo_frame_request? || @return_reason.errors.any?
end

def back_url
request.referer || solidus_admin.return_reasons_path
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
<% end %>

<% turbo_frames.each do |frame| %>
<%= turbo_frame_tag frame %>
<%= turbo_frame_tag frame, target: "_top" %>
<% end %>
<% end %>
6 changes: 6 additions & 0 deletions admin/app/controllers/solidus_admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ class BaseController < ApplicationController
helper 'solidus_admin/components'
helper 'solidus_admin/layout'

helper_method :search_filter_params

private

def search_filter_params
request.params.slice(:q, :page)
end

def set_layout
if turbo_frame_request?
false
Expand Down
38 changes: 12 additions & 26 deletions admin/app/controllers/solidus_admin/return_reasons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,38 @@ def create
@return_reason = Spree::ReturnReason.new(return_reason_params)

if @return_reason.save
respond_to do |format|
flash[:notice] = t('.success')

format.html do
redirect_to solidus_admin.return_reasons_path, status: :see_other
end

format.turbo_stream do
render turbo_stream: '<turbo-stream action="refresh" />'
end
end
flash[:notice] = t('.success')
redirect_to solidus_admin.return_reasons_path(**search_filter_params), status: :see_other
else
respond_to do |format|
format.html do
page_component = component('return_reasons/new').new(return_reason: @return_reason)
render page_component, status: :unprocessable_entity
end
format.turbo_stream do
render status: :unprocessable_entity
end
end
end
end

def edit
respond_to do |format|
format.html { render component('return_reasons/edit').new(return_reason: @return_reason) }
end
render component('return_reasons/edit').new(return_reason: @return_reason)
end

def update
if @return_reason.update(return_reason_params)
respond_to do |format|
flash[:notice] = t('.success')

format.html do
redirect_to solidus_admin.return_reasons_path, status: :see_other
end

format.turbo_stream do
render turbo_stream: '<turbo-stream action="refresh" />'
end
end
flash[:notice] = t('.success')
redirect_to solidus_admin.return_reasons_path(**search_filter_params), status: :see_other
else
respond_to do |format|
format.html do
page_component = component('return_reasons/edit').new(return_reason: @return_reason)
render page_component, status: :unprocessable_entity
end
format.turbo_stream do
render status: :unprocessable_entity
end
end
end
end
Expand All @@ -82,7 +68,7 @@ def destroy
Spree::ReturnReason.transaction { @return_reason.destroy }

flash[:notice] = t('.success')
redirect_back_or_to return_reasons_path, status: :see_other
redirect_back_or_to return_reasons_path(**search_filter_params), status: :see_other
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_stream.replace :new_return_reason_modal do %>
<%= render component('return_reasons/new').new(return_reason: @return_reason) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_stream.replace :edit_return_reason_modal do %>
<%= render component('return_reasons/edit').new(return_reason: @return_reason) %>
<% end %>

0 comments on commit 6770fa3

Please sign in to comment.