From 3580b27f71721fdf879a30d4a22409d353150598 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 17 Oct 2024 15:20:19 +0300 Subject: [PATCH 1/4] enhancement: wrap index table on a `turbo_frame` --- app/components/avo/resource_component.rb | 3 ++- .../avo/turbo_frame_wrapper_component.html.erb | 11 +---------- .../avo/turbo_frame_wrapper_component.rb | 16 +++++++++++++++- app/controllers/avo/base_controller.rb | 1 + app/views/avo/base/index.html.erb | 4 ++-- spec/dummy/app/avo/actions/toggle_inactive.rb | 4 ++++ 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/app/components/avo/resource_component.rb b/app/components/avo/resource_component.rb index 0cf8b59272..cc94a61ab2 100644 --- a/app/components/avo/resource_component.rb +++ b/app/components/avo/resource_component.rb @@ -232,7 +232,8 @@ def render_create_button(control) style: :primary, icon: "heroicons/outline/plus", data: { - target: :create + target: :create, + turbo_frame: :_top } do control.label end diff --git a/app/components/avo/turbo_frame_wrapper_component.html.erb b/app/components/avo/turbo_frame_wrapper_component.html.erb index d3b2ad3cc0..dbcf7adeaf 100644 --- a/app/components/avo/turbo_frame_wrapper_component.html.erb +++ b/app/components/avo/turbo_frame_wrapper_component.html.erb @@ -1,10 +1 @@ -<% if @name.present? %><% end %> -<% - # When rendering the frames the flashed content gets lost. - # We're appending it back if it's a turbo_frame_request. -%> -<% if helpers.turbo_frame_request? %> - <%= helpers.turbo_stream_action_tag :append, target: "alerts", template: render(Avo::FlashAlertsComponent.new(flashes: helpers.flash)) %> -<% end %> -<%= content %> -<% if @name.present? %><% end %> +<%= render_content %> diff --git a/app/components/avo/turbo_frame_wrapper_component.rb b/app/components/avo/turbo_frame_wrapper_component.rb index d21e0197f1..feafffa11e 100644 --- a/app/components/avo/turbo_frame_wrapper_component.rb +++ b/app/components/avo/turbo_frame_wrapper_component.rb @@ -1,5 +1,19 @@ # frozen_string_literal: true class Avo::TurboFrameWrapperComponent < Avo::BaseComponent - prop :name, kind: :positional + prop :frame_id, kind: :positional + + def render_content + return content if @frame_id.blank? + + turbo_frame_tag @frame_id do + # When rendering the frames the flashed content gets lost. + # We're appending it back if it's a turbo_frame_request. + if helpers.turbo_frame_request? + helpers.turbo_stream_action_tag :append, target: "alerts", template: render(Avo::FlashAlertsComponent.new(flashes: helpers.flash)) + end + + content + end + end end diff --git a/app/controllers/avo/base_controller.rb b/app/controllers/avo/base_controller.rb index a19bdece03..c372b0f6ea 100644 --- a/app/controllers/avo/base_controller.rb +++ b/app/controllers/avo/base_controller.rb @@ -17,6 +17,7 @@ class BaseController < ApplicationController before_action :set_pagy_locale, only: :index def index + @turbo_frame = params[:turbo_frame] || @resource.route_key @page_title = @resource.plural_name.humanize if @reflection.present? && !turbo_frame_request? diff --git a/app/views/avo/base/index.html.erb b/app/views/avo/base/index.html.erb index 396798883e..d144acef2a 100644 --- a/app/views/avo/base/index.html.erb +++ b/app/views/avo/base/index.html.erb @@ -1,4 +1,4 @@ -<%= render Avo::TurboFrameWrapperComponent.new(params[:turbo_frame]) do %> +<%= render Avo::TurboFrameWrapperComponent.new(@turbo_frame) do %> <%= render @component.new( resource: @resource, resources: @resources, @@ -8,7 +8,7 @@ filters: @filters, actions: @actions, reflection: @reflection, - turbo_frame: params[:turbo_frame], + turbo_frame: @turbo_frame, parent_record: @parent_record, parent_resource: @parent_resource, applied_filters: @applied_filters, diff --git a/spec/dummy/app/avo/actions/toggle_inactive.rb b/spec/dummy/app/avo/actions/toggle_inactive.rb index 84760248bf..533f6a0591 100644 --- a/spec/dummy/app/avo/actions/toggle_inactive.rb +++ b/spec/dummy/app/avo/actions/toggle_inactive.rb @@ -34,5 +34,9 @@ def handle(**args) end silent + close_modal + append_to_response -> { + turbo_stream.turbo_frame_set_src(@resource.model_key, src: avo.resources_users_path) + } end end From aba194cf05e69dd51479f9992f7fbf343f20e14c Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 17 Oct 2024 15:21:53 +0300 Subject: [PATCH 2/4] break line --- app/components/avo/turbo_frame_wrapper_component.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/components/avo/turbo_frame_wrapper_component.rb b/app/components/avo/turbo_frame_wrapper_component.rb index feafffa11e..946d128b16 100644 --- a/app/components/avo/turbo_frame_wrapper_component.rb +++ b/app/components/avo/turbo_frame_wrapper_component.rb @@ -10,7 +10,9 @@ def render_content # When rendering the frames the flashed content gets lost. # We're appending it back if it's a turbo_frame_request. if helpers.turbo_frame_request? - helpers.turbo_stream_action_tag :append, target: "alerts", template: render(Avo::FlashAlertsComponent.new(flashes: helpers.flash)) + helpers.turbo_stream_action_tag :append, + target: "alerts", + template: render(Avo::FlashAlertsComponent.new(flashes: helpers.flash)) end content From 17b0020251d8ae80b2de3c797845cf93d742c3e0 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 17 Oct 2024 16:49:50 +0300 Subject: [PATCH 3/4] WIP just to keep the current work --- app/components/avo/paginator_component.html.erb | 2 +- app/components/avo/paginator_component.rb | 8 ++++++++ .../avo/turbo_frame_wrapper_component.rb | 2 +- .../js/controllers/filter_controller.js | 5 ++--- .../avo/partials/_view_toggle_button.html.erb | 15 ++++++++++----- spec/dummy/app/avo/actions/toggle_inactive.rb | 2 +- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/components/avo/paginator_component.html.erb b/app/components/avo/paginator_component.html.erb index c5c8b649b7..bf39e296aa 100644 --- a/app/components/avo/paginator_component.html.erb +++ b/app/components/avo/paginator_component.html.erb @@ -31,7 +31,7 @@ <% if @pagy.pages > 1 %> <%# @todo: add first & last page. make the first and last buttons rounded %> - <%== helpers.pagy_nav @pagy, anchor_string: "data-turbo-frame=\"#{@turbo_frame}\"" %> + <%== helpers.pagy_nav @pagy, anchor_string: anchor_string %> <% end %> diff --git a/app/components/avo/paginator_component.rb b/app/components/avo/paginator_component.rb index 318197b7fa..e85e8221db 100644 --- a/app/components/avo/paginator_component.rb +++ b/app/components/avo/paginator_component.rb @@ -48,4 +48,12 @@ def pagy_major_version version end + + def anchor_string + if @parent_record.present? + "data-turbo-frame=\"#{@turbo_frame}\"" + else + "data-turbo-frame=\"#{@turbo_frame}\", data-turbo-action=\"advance\"" + end + end end diff --git a/app/components/avo/turbo_frame_wrapper_component.rb b/app/components/avo/turbo_frame_wrapper_component.rb index 946d128b16..6a27c0fe46 100644 --- a/app/components/avo/turbo_frame_wrapper_component.rb +++ b/app/components/avo/turbo_frame_wrapper_component.rb @@ -6,7 +6,7 @@ class Avo::TurboFrameWrapperComponent < Avo::BaseComponent def render_content return content if @frame_id.blank? - turbo_frame_tag @frame_id do + turbo_frame_tag @frame_id do # When rendering the frames the flashed content gets lost. # We're appending it back if it's a turbo_frame_request. if helpers.turbo_frame_request? diff --git a/app/javascript/js/controllers/filter_controller.js b/app/javascript/js/controllers/filter_controller.js index 6ac35cdbd3..f2381cb99e 100644 --- a/app/javascript/js/controllers/filter_controller.js +++ b/app/javascript/js/controllers/filter_controller.js @@ -1,4 +1,4 @@ -import { Controller } from '@hotwired/stimulus' +import { Controller, Turbo } from '@hotwired/stimulus' import URI from 'urijs' export default class extends Controller { @@ -109,7 +109,6 @@ export default class extends Controller { url.query(query) - this.urlRedirectTarget.href = url.readable().toString() - this.urlRedirectTarget.click() + Turbo.visit(url.readable().toString()) } } diff --git a/app/views/avo/partials/_view_toggle_button.html.erb b/app/views/avo/partials/_view_toggle_button.html.erb index 3171c5ceec..fd1bb532a7 100644 --- a/app/views/avo/partials/_view_toggle_button.html.erb +++ b/app/views/avo/partials/_view_toggle_button.html.erb @@ -26,6 +26,15 @@
<% available_view_types.each do |type| %> <% is_active_view = view_type.to_s == type.to_s %> + <% + data = { + tippy: 'tooltip', + 'turbo-frame': turbo_frame, + control: "view-type-toggle-#{type}" + } + + data.merge(turbo_action: :advance) if @parent_record.blank? + %> <%= a_link url_for(params.permit!.merge(view_type: type)).to_s, icon: info[type][:new_icon], @@ -34,11 +43,7 @@ size: :sm, class: is_active_view ? "z-20" : "bg-gray-100 border-gray-300", title: t('avo.switch_to_view', view_type: type), - data: { - tippy: 'tooltip', - 'turbo-frame': turbo_frame, - control: "view-type-toggle-#{type}" - } + data: data %> <% end %>
diff --git a/spec/dummy/app/avo/actions/toggle_inactive.rb b/spec/dummy/app/avo/actions/toggle_inactive.rb index 533f6a0591..dea909ee90 100644 --- a/spec/dummy/app/avo/actions/toggle_inactive.rb +++ b/spec/dummy/app/avo/actions/toggle_inactive.rb @@ -36,7 +36,7 @@ def handle(**args) silent close_modal append_to_response -> { - turbo_stream.turbo_frame_set_src(@resource.model_key, src: avo.resources_users_path) + turbo_stream.turbo_frame_set_src(@resource.model_key, src: @resource.records_path) } end end From ec7d502e732c638b84a0121363b5eaa1457174ce Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 17 Oct 2024 16:54:32 +0300 Subject: [PATCH 4/4] WIP just to keep the current work --- app/javascript/js/controllers/filter_controller.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/javascript/js/controllers/filter_controller.js b/app/javascript/js/controllers/filter_controller.js index f2381cb99e..fc272cecc5 100644 --- a/app/javascript/js/controllers/filter_controller.js +++ b/app/javascript/js/controllers/filter_controller.js @@ -1,4 +1,5 @@ -import { Controller, Turbo } from '@hotwired/stimulus' +import { Controller } from '@hotwired/stimulus' +import { Turbo } from '@hotwired/turbo-rails' import URI from 'urijs' export default class extends Controller {