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

Closed
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ddf543d
send to method
m7madmagdy Apr 4, 2025
3f2d413
Dynamically resolve and instantiate view component class
a7madM Apr 4, 2025
1feefdd
refactor: enhance component instantiation and improve grid item rende…
m7madmagdy Apr 4, 2025
e0591f4
remove logger from update product action
m7madmagdy Apr 4, 2025
2045880
wipe: lint
m7madmagdy Apr 4, 2025
1f17d9b
wipe: lint
m7madmagdy Apr 4, 2025
5bde5b6
fix: ensure send default view type in action
m7madmagdy Apr 5, 2025
5300b06
Refactor: define ROW_COMPONENTS_BY_VIEW to centralize row component m…
m7madmagdy Apr 5, 2025
4d97810
refactor: update component view assignment to use virtual path
m7madmagdy Apr 5, 2025
b70f902
used name.underscore instead of virtual_path
m7madmagdy Apr 6, 2025
a132c8d
support map view
m7madmagdy Apr 6, 2025
1c15e5f
possible approach
Paul-Bob Apr 7, 2025
8ee23c0
rm raise
Paul-Bob Apr 7, 2025
7524438
lint
Paul-Bob Apr 7, 2025
60e3d0b
Merge branch 'main' into fix/reload-records-with-gridview
m7madmagdy Apr 8, 2025
cf8f538
fix: lint
m7madmagdy Apr 8, 2025
a9a9e0d
Merge branch 'main' into fix/reload-records-with-gridview
a7madM Apr 12, 2025
fd9002b
test: permit the view type params on action controller
m7madmagdy Apr 23, 2025
d916ed8
Merge branch 'main' into fix/reload-records-with-gridview
m7madmagdy Apr 23, 2025
a232523
lint
m7madmagdy Apr 23, 2025
051b8c9
fix lint
m7madmagdy Apr 25, 2025
3d706d1
Merge branch 'main' into fix/reload-records-with-gridview
m7madmagdy Apr 25, 2025
9e6001a
refactor view_type
Paul-Bob Apr 28, 2025
1d6546b
separate methods
Paul-Bob Apr 28, 2025
cc75a2b
Merge branch 'main' into fix/reload-records-with-gridview
Paul-Bob Apr 28, 2025
8b6cccd
Merge branch 'main' into fix/reload-records-with-gridview
m7madmagdy Apr 29, 2025
ff962cd
removed unnecessary update product action
m7madmagdy Apr 29, 2025
ddef1d8
Merge branch 'fix/reload-records-with-gridview' of github.com:m7madma…
m7madmagdy Apr 29, 2025
9da6753
removed updated_at field from product resource
m7madmagdy Apr 29, 2025
9650dcd
Merge branch 'main' into fix/reload-records-with-gridview
m7madmagdy May 11, 2025
3358458
Merge branch 'main' into fix/reload-records-with-gridview
Paul-Bob May 28, 2025
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
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
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
3 changes: 2 additions & 1 deletion app/components/avo/resource_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/views/avo/actions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 15 additions & 9 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
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
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
end
end
Loading