Skip to content

Commit

Permalink
wip (#3372)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob authored Nov 5, 2024
1 parent 40abfed commit 5e9b99c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/components/avo/index/table_row_component.html.erb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
43 changes: 43 additions & 0 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion spec/dummy/app/avo/actions/city/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions spec/dummy/app/avo/actions/toggle_inactive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ def handle(**args)
end

silent

reload_records(query)
end
end

0 comments on commit 5e9b99c

Please sign in to comment.