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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion app/components/avo/actions_component.rb
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ class Avo::ActionsComponent < Avo::BaseComponent
end
prop :resource
prop :view
prop :view_type
prop :host_component

delegate_missing_to :@host_component
@@ -85,7 +86,7 @@ def render_item(action)
private

def render_action_link(action, icon: nil)
link_to action.link_arguments(resource: @resource, arguments: action.arguments).first,
link_to action.link_arguments(resource: @resource, arguments: action.arguments, view_type: @view_type).first,
data: action_data_attributes(action),
title: action.action_name,
class: action_css_class(action) do
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,
3 changes: 2 additions & 1 deletion app/components/avo/resource_component.rb
Original file line number Diff line number Diff line change
@@ -161,7 +161,8 @@ def render_actions_list(actions_list)
size: actions_list.size,
icon: actions_list.icon,
title: actions_list.title,
as_row_control: instance_of?(Avo::Index::ResourceControlsComponent)
as_row_control: instance_of?(Avo::Index::ResourceControlsComponent),
view_type: try(:view_type)
)
end

2 changes: 1 addition & 1 deletion app/views/avo/actions/show.html.erb
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
class="hidden text-slate-800"
>
<%= form_with scope: 'fields',
url: @action.link_arguments(resource: @resource).first,
url: @action.link_arguments(resource: @resource, view_type: params[:view_type]).first,
local: true,
html: {
novalidate: true,
24 changes: 15 additions & 9 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
@@ -8,6 +8,12 @@ class BaseAction
DATA_ATTRIBUTES = {turbo_frame: Avo::MODAL_FRAME_ID}
end

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

class_attribute :name, default: nil
class_attribute :message
class_attribute :confirm_button_label
@@ -294,39 +300,39 @@ def reload
end

def reload_record(records)
return if params[:view_type].blank?
# 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 = ROW_COMPONENTS_BY_VIEW[params[:view_type].to_sym].safe_constantize
component_view = component_class.name.underscore

@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

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("#{component_view}_#{@action.records_to_reload[index].to_param}", component)
end
}
end
13 changes: 13 additions & 0 deletions lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
@@ -655,6 +655,19 @@ 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)

case klass.to_s
when "Avo::Index::TableRowComponent"
klass.new(**args)
when "Avo::Index::GridItemComponent"
klass.new(resource: self)
else
raise "Unknown component class #{klass}"
end
end

def get_external_link
return unless record.persisted?

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

def handle(**args)
records, _resource = args.values_at(:records, :resource)

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

reload_records(records)
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
@@ -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"
@@ -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
end
end