Skip to content

Fix: extend reload_records to support grid view #3780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/components/avo/index/grid_item_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= content_tag :div,
id: "#{self.class.to_s.underscore}_#{@resource.record_param}",
class: "relative bg-white rounded shadow-modal flex flex-col group",
data: {
component_name: self.class.to_s.underscore,
Expand Down
29 changes: 19 additions & 10 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ class BaseAction
DATA_ATTRIBUTES = {turbo_frame: Avo::MODAL_FRAME_ID}
end

VIEW_ITEM_NAME_BY_TYPE = {
table: "avo/index/table_row_component",
grid: "avo/index/grid_item_component"
}.freeze

COMPONENT_ROW_TYPES = {
table: "Avo::Index::TableRowComponent",
grid: "Avo::Index::GridItemComponent"
}.freeze

class_attribute :name, default: nil
class_attribute :message
class_attribute :confirm_button_label
Expand Down Expand Up @@ -293,40 +303,39 @@ def reload
self
end

def reload_record(records)
def reload_record(records, view_type: :table)
# 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 = []
row_components = []
header_fields = []
component_class = COMPONENT_ROW_TYPES[view_type.to_sym].safe_constantize

@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(
row_components << resource.instantiate_component(
component_class,
resource: resource,
header_fields: row_fields.map(&:table_header_label),
fields: row_fields
)
end

row_view = VIEW_ITEM_NAME_BY_TYPE[view_type.to_sym]
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
)
row_components.map.with_index do |component, index|
component.header_fields = header_fields_ids if component.respond_to?(:header_fields)
turbo_stream.replace("#{row_view}_#{@action.records_to_reload[index].to_param}", component)
end
}
end
Expand Down
9 changes: 9 additions & 0 deletions lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,15 @@ def resolve_component(original_component)
custom_components.dig(original_component.to_s)&.to_s&.safe_constantize || original_component
end

def instantiate_component(component_class, **args)
klass = resolve_component(component_class)

return klass.new(**args) if klass == Avo::Index::TableRowComponent
return klass.new(resource: self) if klass == Avo::Index::GridItemComponent

raise "Unknown component class #{klass}"
end

def get_external_link
return unless record.persisted?

Expand Down
14 changes: 14 additions & 0 deletions spec/dummy/app/avo/actions/update_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Avo::Actions::UpdateProduct < Avo::BaseAction
self.name = "Update Product"

def handle(**args)
records, resource = args.values_at(:records, :resource)
view_type = arguments[:view_type] || resource.default_view_type

records.each do |record|
record.update!(updated_at: Time.current)
end

reload_records(records, view_type: view_type)
end
end
8 changes: 7 additions & 1 deletion spec/dummy/app/avo/resources/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Avo::Resources::Product < Avo::BaseResource
{
cover_url: record.image.attached? ? main_app.url_for(record.image.variant(resize_to_fill: [300, 300])) : nil,
title: record.title,
body: simple_format(record.description),
body: simple_format(record.description) + " \n #{record.updated_at.to_datetime.strftime("%d %b %Y %H:%M:%S")}",
badge_label: (record.status == :new) ? "New" : "Updated",
badge_color: (record.status == :new) ? "green" : "orange",
badge_title: (record.status == :new) ? "New product here" : "Updated product here"
Expand Down Expand Up @@ -59,5 +59,11 @@ def fields
field :image, as: :file, is_image: true
field :category, as: :select, enum: ::Product.categories
field :sizes, as: :select, multiple: true, options: {Large: :large, Medium: :medium, Small: :small}

field :updated_at, as: :date_time
end

def actions
action Avo::Actions::UpdateProduct, arguments: {view_type: params[:view_type]}
end
end
Loading