Skip to content

Commit 05dabbd

Browse files
committed
Make redirects from edit and new routes work with turbo frame
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.
1 parent f4c8983 commit 05dabbd

File tree

11 files changed

+62
-51
lines changed

11 files changed

+62
-51
lines changed

admin/app/components/solidus_admin/base_component.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def self.stimulus_id
3737
end
3838

3939
delegate :stimulus_id, to: :class
40+
delegate :turbo_frame_request?, :search_filter_params, to: :helpers
4041

4142
class << self
4243
private

admin/app/components/solidus_admin/return_reasons/edit/component.html.erb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<%= turbo_frame_tag :edit_return_reason_modal do %>
2-
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3-
<%= form_for @return_reason, url: solidus_admin.return_reason_path(@return_reason), html: { id: form_id } do |f| %>
1+
<%= turbo_frame_tag :edit_return_reason_modal, target: "_top" do %>
2+
<%= render component("ui/modal").new(title: t(".title"), closable: closable?) do |modal| %>
3+
<%= form_for @return_reason, url: form_url, html: { id: form_id } do |f| %>
44
<div class="flex flex-col gap-6 pb-4">
55
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
66
<label class="flex gap-2 items-center">
@@ -15,9 +15,18 @@
1515
</label>
1616
</div>
1717
<% modal.with_actions do %>
18-
<form method="dialog">
19-
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
20-
</form>
18+
<% if closable? %>
19+
<form method="dialog">
20+
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
21+
</form>
22+
<% else %>
23+
<%= render component("ui/button").new(
24+
tag: :a,
25+
href: back_url,
26+
scheme: :secondary,
27+
text: t('.cancel')
28+
) %>
29+
<% end %>
2130
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
2231
<% end %>
2332
<% end %>
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
# frozen_string_literal: true
22

3-
class SolidusAdmin::ReturnReasons::Edit::Component < SolidusAdmin::BaseComponent
3+
class SolidusAdmin::ReturnReasons::Edit::Component < SolidusAdmin::Resources::Edit::Component
44
def initialize(return_reason:)
55
@return_reason = return_reason
6-
end
7-
8-
def form_id
9-
dom_id(@return_reason, "#{stimulus_id}_edit_return_reason_form")
6+
super(return_reason)
107
end
118
end

admin/app/components/solidus_admin/return_reasons/index/component.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def search_key
1414
end
1515

1616
def edit_path(return_reason)
17-
spree.edit_admin_return_reason_path(return_reason)
17+
spree.edit_admin_return_reason_path(return_reason, **search_filter_params)
1818
end
1919

2020
def turbo_frames
@@ -28,7 +28,7 @@ def page_actions
2828
render component("ui/button").new(
2929
tag: :a,
3030
text: t('.add'),
31-
href: solidus_admin.new_return_reason_path,
31+
href: solidus_admin.new_return_reason_path(**search_filter_params),
3232
data: { turbo_frame: :new_return_reason_modal, turbo_prefetch: false },
3333
icon: "add-line",
3434
class: "align-self-end w-full",

admin/app/components/solidus_admin/return_reasons/new/component.html.erb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<%= turbo_frame_tag :new_return_reason_modal do %>
2-
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3-
<%= form_for @return_reason, url: solidus_admin.return_reasons_path, html: { id: form_id } do |f| %>
1+
<%= turbo_frame_tag :new_return_reason_modal, target: "_top" do %>
2+
<%= render component("ui/modal").new(title: t(".title"), closable: closable?) do |modal| %>
3+
<%= form_for @return_reason, url: form_url, html: { id: form_id } do |f| %>
44
<div class="flex flex-col gap-6 pb-4">
55
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
66
<label class="flex gap-2 items-center">
@@ -15,9 +15,18 @@
1515
</label>
1616
</div>
1717
<% modal.with_actions do %>
18-
<form method="dialog">
19-
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
20-
</form>
18+
<% if closable? %>
19+
<form method="dialog">
20+
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
21+
</form>
22+
<% else %>
23+
<%= render component("ui/button").new(
24+
tag: :a,
25+
href: back_url,
26+
scheme: :secondary,
27+
text: t('.cancel')
28+
) %>
29+
<% end %>
2130
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
2231
<% end %>
2332
<% end %>
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
# frozen_string_literal: true
22

3-
class SolidusAdmin::ReturnReasons::New::Component < SolidusAdmin::BaseComponent
3+
class SolidusAdmin::ReturnReasons::New::Component < SolidusAdmin::Resources::New::Component
44
def initialize(return_reason:)
55
@return_reason = return_reason
6-
end
7-
8-
def form_id
9-
dom_id(@return_reason, "#{stimulus_id}_new_return_reason_form")
6+
super(return_reason)
107
end
118
end

admin/app/components/solidus_admin/ui/pages/index/component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@
3939
<% end %>
4040

4141
<% turbo_frames.each do |frame| %>
42-
<%= turbo_frame_tag frame %>
42+
<%= turbo_frame_tag frame, target: "_top" %>
4343
<% end %>
4444
<% end %>

admin/app/controllers/solidus_admin/base_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ class BaseController < ApplicationController
2020
helper 'solidus_admin/components'
2121
helper 'solidus_admin/layout'
2222

23+
helper_method :search_filter_params
24+
2325
private
2426

27+
def search_filter_params
28+
request.params.slice(:q, :page)
29+
end
30+
2531
def set_layout
2632
if turbo_frame_request?
2733
false

admin/app/controllers/solidus_admin/return_reasons_controller.rb

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,38 @@ def create
2626
@return_reason = Spree::ReturnReason.new(return_reason_params)
2727

2828
if @return_reason.save
29-
respond_to do |format|
30-
flash[:notice] = t('.success')
31-
32-
format.html do
33-
redirect_to solidus_admin.return_reasons_path, status: :see_other
34-
end
35-
36-
format.turbo_stream do
37-
render turbo_stream: '<turbo-stream action="refresh" />'
38-
end
39-
end
29+
flash[:notice] = t('.success')
30+
redirect_to solidus_admin.return_reasons_path(**search_filter_params), status: :see_other
4031
else
4132
respond_to do |format|
4233
format.html do
4334
page_component = component('return_reasons/new').new(return_reason: @return_reason)
4435
render page_component, status: :unprocessable_entity
4536
end
37+
format.turbo_stream do
38+
render status: :unprocessable_entity
39+
end
4640
end
4741
end
4842
end
4943

5044
def edit
51-
respond_to do |format|
52-
format.html { render component('return_reasons/edit').new(return_reason: @return_reason) }
53-
end
45+
render component('return_reasons/edit').new(return_reason: @return_reason)
5446
end
5547

5648
def update
5749
if @return_reason.update(return_reason_params)
58-
respond_to do |format|
59-
flash[:notice] = t('.success')
60-
61-
format.html do
62-
redirect_to solidus_admin.return_reasons_path, status: :see_other
63-
end
64-
65-
format.turbo_stream do
66-
render turbo_stream: '<turbo-stream action="refresh" />'
67-
end
68-
end
50+
flash[:notice] = t('.success')
51+
redirect_to solidus_admin.return_reasons_path(**search_filter_params), status: :see_other
6952
else
7053
respond_to do |format|
7154
format.html do
7255
page_component = component('return_reasons/edit').new(return_reason: @return_reason)
7356
render page_component, status: :unprocessable_entity
7457
end
58+
format.turbo_stream do
59+
render status: :unprocessable_entity
60+
end
7561
end
7662
end
7763
end
@@ -82,7 +68,7 @@ def destroy
8268
Spree::ReturnReason.transaction { @return_reason.destroy }
8369

8470
flash[:notice] = t('.success')
85-
redirect_back_or_to return_reasons_path, status: :see_other
71+
redirect_back_or_to return_reasons_path(**search_filter_params), status: :see_other
8672
end
8773

8874
private
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<%= turbo_stream.replace :new_return_reason_modal do %>
2+
<%= render component('return_reasons/new').new(return_reason: @return_reason) %>
3+
<% end %>

0 commit comments

Comments
 (0)