diff --git a/app/components/avo/index/table_row_component.html.erb b/app/components/avo/index/table_row_component.html.erb index 0dff9248bf..1f682ab548 100644 --- a/app/components/avo/index/table_row_component.html.erb +++ b/app/components/avo/index/table_row_component.html.erb @@ -1,6 +1,7 @@ <%# hover:z-[21] removed from tr class to solve flickering actions component on row controls and z-20 changed to z-21%> <%= content_tag :tr, + id: "#{self.class.to_s.underscore}_#{@resource.record.to_param}", class: class_names("bg-white hover:bg-gray-50 hover:shadow-row z-21 border-b", {"cursor-pointer": click_row_to_view_record}), data: { component_name: self.class.to_s.underscore, diff --git a/lib/avo/base_action.rb b/lib/avo/base_action.rb index 5a6b3665e4..e6c3cf2b02 100644 --- a/lib/avo/base_action.rb +++ b/lib/avo/base_action.rb @@ -24,6 +24,7 @@ class BaseAction attr_reader :arguments attr_reader :icon attr_reader :appended_turbo_streams + attr_reader :records_to_reload # TODO: find a differnet way to delegate this to the uninitialized Current variable delegate :context, to: Avo::Current @@ -237,6 +238,7 @@ def close_modal self end + # def do_nothing alias_method :do_nothing, :close_modal # Add a placeholder silent message from when a user wants to do a redirect action or something similar @@ -264,6 +266,47 @@ def reload self end + def reload_record(records) + # Force close modal to avoid default redirect to + # Redirect is 100% not wanted when using reload_record + close_modal + + @records_to_reload = Array(records) + + append_to_response -> { + table_row_components = [] + header_fields = [] + + @action.records_to_reload.each do |record| + resource = @resource.dup + resource.hydrate(record:, view: :index) + resource.detect_fields + row_fields = resource.get_fields(only_root: true) + header_fields.concat row_fields + table_row_components << resource.resolve_component(Avo::Index::TableRowComponent).new( + resource: resource, + header_fields: row_fields.map(&:table_header_label), + fields: row_fields + ) + end + + header_fields.uniq!(&:table_header_label) + + header_fields_ids = header_fields.map(&:table_header_label) + + table_row_components.map.with_index do |table_row_component, index| + table_row_component.header_fields = header_fields_ids + turbo_stream.replace( + "avo/index/table_row_component_#{@action.records_to_reload[index].to_param}", + table_row_component + ) + end + } + end + + # def reload_records + alias_method :reload_records, :reload_record + def navigate_to_action(action, **kwargs) response[:type] = :navigate_to_action response[:action] = action diff --git a/spec/dummy/app/avo/actions/city/update.rb b/spec/dummy/app/avo/actions/city/update.rb index 7eea1c3246..333fd66db4 100644 --- a/spec/dummy/app/avo/actions/city/update.rb +++ b/spec/dummy/app/avo/actions/city/update.rb @@ -8,10 +8,14 @@ def fields end def handle(**args) - City.find(arguments[:cities]).each do |city| + cities = City.find(arguments[:cities]) + + cities.each do |city| city.update! args[:fields] end succeed "City updated!" + + reload_records(cities) end end diff --git a/spec/dummy/app/avo/actions/toggle_inactive.rb b/spec/dummy/app/avo/actions/toggle_inactive.rb index 84760248bf..10eb150529 100644 --- a/spec/dummy/app/avo/actions/toggle_inactive.rb +++ b/spec/dummy/app/avo/actions/toggle_inactive.rb @@ -34,5 +34,7 @@ def handle(**args) end silent + + reload_records(query) end end