From 05dabbd6fa5ef0fd5d328a2b76e2b1c9b3467e2f Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 23 Dec 2024 10:44:41 +0100 Subject: [PATCH] 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. --- .../solidus_admin/base_component.rb | 1 + .../return_reasons/edit/component.html.erb | 21 +++++++--- .../return_reasons/edit/component.rb | 7 +--- .../return_reasons/index/component.rb | 4 +- .../return_reasons/new/component.html.erb | 21 +++++++--- .../return_reasons/new/component.rb | 7 +--- .../ui/pages/index/component.html.erb | 2 +- .../solidus_admin/base_controller.rb | 6 +++ .../return_reasons_controller.rb | 38 ++++++------------- .../return_reasons/create.turbo_stream.erb | 3 ++ .../return_reasons/update.turbo_stream.erb | 3 ++ 11 files changed, 62 insertions(+), 51 deletions(-) create mode 100644 admin/app/views/solidus_admin/return_reasons/create.turbo_stream.erb create mode 100644 admin/app/views/solidus_admin/return_reasons/update.turbo_stream.erb diff --git a/admin/app/components/solidus_admin/base_component.rb b/admin/app/components/solidus_admin/base_component.rb index e3adf62c02f..6b9fea1a584 100644 --- a/admin/app/components/solidus_admin/base_component.rb +++ b/admin/app/components/solidus_admin/base_component.rb @@ -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 diff --git a/admin/app/components/solidus_admin/return_reasons/edit/component.html.erb b/admin/app/components/solidus_admin/return_reasons/edit/component.html.erb index 5e14b95e423..f9d013ff490 100644 --- a/admin/app/components/solidus_admin/return_reasons/edit/component.html.erb +++ b/admin/app/components/solidus_admin/return_reasons/edit/component.html.erb @@ -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| %>
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
<% modal.with_actions do %> -
- <%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %> -
+ <% if closable? %> +
+ <%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %> +
+ <% 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 %> diff --git a/admin/app/components/solidus_admin/return_reasons/edit/component.rb b/admin/app/components/solidus_admin/return_reasons/edit/component.rb index a73bca5670a..336860e4277 100644 --- a/admin/app/components/solidus_admin/return_reasons/edit/component.rb +++ b/admin/app/components/solidus_admin/return_reasons/edit/component.rb @@ -1,11 +1,8 @@ # frozen_string_literal: true -class SolidusAdmin::ReturnReasons::Edit::Component < SolidusAdmin::BaseComponent +class SolidusAdmin::ReturnReasons::Edit::Component < SolidusAdmin::Resources::Edit::Component def initialize(return_reason:) @return_reason = return_reason - end - - def form_id - dom_id(@return_reason, "#{stimulus_id}_edit_return_reason_form") + super(return_reason) end end diff --git a/admin/app/components/solidus_admin/return_reasons/index/component.rb b/admin/app/components/solidus_admin/return_reasons/index/component.rb index 8a6ee9f80ef..2de0504de2b 100644 --- a/admin/app/components/solidus_admin/return_reasons/index/component.rb +++ b/admin/app/components/solidus_admin/return_reasons/index/component.rb @@ -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 @@ -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", diff --git a/admin/app/components/solidus_admin/return_reasons/new/component.html.erb b/admin/app/components/solidus_admin/return_reasons/new/component.html.erb index 237315fb3e8..d39c623b934 100644 --- a/admin/app/components/solidus_admin/return_reasons/new/component.html.erb +++ b/admin/app/components/solidus_admin/return_reasons/new/component.html.erb @@ -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| %>
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
<% modal.with_actions do %> -
- <%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %> -
+ <% if closable? %> +
+ <%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %> +
+ <% 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 %> diff --git a/admin/app/components/solidus_admin/return_reasons/new/component.rb b/admin/app/components/solidus_admin/return_reasons/new/component.rb index 35ae39d7dbe..b2578425a2f 100644 --- a/admin/app/components/solidus_admin/return_reasons/new/component.rb +++ b/admin/app/components/solidus_admin/return_reasons/new/component.rb @@ -1,11 +1,8 @@ # frozen_string_literal: true -class SolidusAdmin::ReturnReasons::New::Component < SolidusAdmin::BaseComponent +class SolidusAdmin::ReturnReasons::New::Component < SolidusAdmin::Resources::New::Component def initialize(return_reason:) @return_reason = return_reason - end - - def form_id - dom_id(@return_reason, "#{stimulus_id}_new_return_reason_form") + super(return_reason) end end diff --git a/admin/app/components/solidus_admin/ui/pages/index/component.html.erb b/admin/app/components/solidus_admin/ui/pages/index/component.html.erb index 69916f08dd4..682a0123b61 100644 --- a/admin/app/components/solidus_admin/ui/pages/index/component.html.erb +++ b/admin/app/components/solidus_admin/ui/pages/index/component.html.erb @@ -39,6 +39,6 @@ <% end %> <% turbo_frames.each do |frame| %> - <%= turbo_frame_tag frame %> + <%= turbo_frame_tag frame, target: "_top" %> <% end %> <% end %> diff --git a/admin/app/controllers/solidus_admin/base_controller.rb b/admin/app/controllers/solidus_admin/base_controller.rb index 98905a1f603..e8d63898272 100644 --- a/admin/app/controllers/solidus_admin/base_controller.rb +++ b/admin/app/controllers/solidus_admin/base_controller.rb @@ -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 diff --git a/admin/app/controllers/solidus_admin/return_reasons_controller.rb b/admin/app/controllers/solidus_admin/return_reasons_controller.rb index 7effdd9f213..fefda2faec3 100644 --- a/admin/app/controllers/solidus_admin/return_reasons_controller.rb +++ b/admin/app/controllers/solidus_admin/return_reasons_controller.rb @@ -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: '' - 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: '' - 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 @@ -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 diff --git a/admin/app/views/solidus_admin/return_reasons/create.turbo_stream.erb b/admin/app/views/solidus_admin/return_reasons/create.turbo_stream.erb new file mode 100644 index 00000000000..3acfcfcce03 --- /dev/null +++ b/admin/app/views/solidus_admin/return_reasons/create.turbo_stream.erb @@ -0,0 +1,3 @@ +<%= turbo_stream.replace :new_return_reason_modal do %> + <%= render component('return_reasons/new').new(return_reason: @return_reason) %> +<% end %> diff --git a/admin/app/views/solidus_admin/return_reasons/update.turbo_stream.erb b/admin/app/views/solidus_admin/return_reasons/update.turbo_stream.erb new file mode 100644 index 00000000000..55760e8d270 --- /dev/null +++ b/admin/app/views/solidus_admin/return_reasons/update.turbo_stream.erb @@ -0,0 +1,3 @@ +<%= turbo_stream.replace :edit_return_reason_modal do %> + <%= render component('return_reasons/edit').new(return_reason: @return_reason) %> +<% end %>