Skip to content

Commit

Permalink
Move product/index columns and batch actions to the component class
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Jul 26, 2023
1 parent 6280ded commit 52e7038
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,7 @@

<%= render @table_component.new(
page: @page,
batch_actions: [
{
display_name: t('.batch_actions.delete'),
action: solidus_admin.products_path,
method: :delete,
icon: 'delete-bin-7-line',
},
{
display_name: t('.batch_actions.discontinue'),
action: solidus_admin.discontinue_products_path,
method: :put,
icon: 'pause-circle-line',
},
{
display_name: t('.batch_actions.activate'),
action: solidus_admin.activate_products_path,
method: :put,
icon: 'play-circle-line',
},
],
columns: [
{
class_name: "w-[72px]",
header: tag.span('aria-label': t('.product_image'), role: 'text'),
data: -> { image_column(_1) },
},
{ header: :name, data: -> { name_column(_1) } },
{ header: :status, data: -> { status_column(_1) } },
{ header: :price, data: -> { price_column(_1) } },
{ header: :stock, data: -> { stock_column(_1) } }
]
)
%>
batch_actions: batch_actions,
columns: columns,
) %>
</div>
134 changes: 97 additions & 37 deletions admin/app/components/solidus_admin/products/index/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,107 @@ def initialize(
@button_component = button_component
end

def image_column(product)
image = product.gallery.images.first or return

link_to(
image_tag(image.url(:small), class: 'h-10 w-10 max-w-min rounded border border-gray-100', alt: product.name),
spree.edit_admin_product_path(product),
class: 'inline-flex overflow-hidden',
tabindex: -1,
)
end

def name_column(product)
link_to product.name, spree.edit_admin_product_path(product)
end

def status_column(product)
if product.available?
@badge_component.new(name: t('.status.available'), color: :green)
else
@badge_component.new(name: t('.status.discontinued'), color: :red)
end
end

def stock_column(product)
stock_info =
case (on_hand = product.total_on_hand)
when Float::INFINITY
content_tag :span, t('.stock.in_stock', on_hand: t('.stock.infinity')), class: 'text-forest'
when 1..Float::INFINITY
content_tag :span, t('.stock.in_stock', on_hand: on_hand), class: 'text-forest'
else
content_tag :span, t('.stock.in_stock', on_hand: on_hand), class: 'text-red-500'

def batch_actions
[
{
display_name: t('.batch_actions.delete'),
action: solidus_admin.products_path,
method: :delete,
icon: 'delete-bin-7-line',
},
{
display_name: t('.batch_actions.discontinue'),
action: solidus_admin.discontinue_products_path,
method: :put,
icon: 'pause-circle-line',
},
{
display_name: t('.batch_actions.activate'),
action: solidus_admin.activate_products_path,
method: :put,
icon: 'play-circle-line',
},
]
end

def columns
[
image_column,
name_column,
status_column,
price_column,
stock_column,
]
end

def image_column
{
class_name: "w-[72px]",
header: tag.span('aria-label': t('.product_image'), role: 'text'),
data: ->(product) do
image = product.gallery.images.first or return

link_to(
image_tag(image.url(:small), class: 'h-10 w-10 max-w-min rounded border border-gray-100', alt: product.name),
spree.edit_admin_product_path(product),
class: 'inline-flex overflow-hidden',
tabindex: -1,
)
end
}
end

def name_column
{
header: :name,
data: ->(product) do
link_to product.name, spree.edit_admin_product_path(product)
end
}
end

def status_column
{
header: :status,
data: ->(product) do
if product.available?
@badge_component.new(name: t('.status.available'), color: :green)
else
@badge_component.new(name: t('.status.discontinued'), color: :red)
end
end
}
end

def stock_column
{
header: :stock,
data: ->(product) do
stock_info =
case (on_hand = product.total_on_hand)
when Float::INFINITY
content_tag :span, t('.stock.in_stock', on_hand: t('.stock.infinity')), class: 'text-forest'
when 1..Float::INFINITY
content_tag :span, t('.stock.in_stock', on_hand: on_hand), class: 'text-forest'
else
content_tag :span, t('.stock.in_stock', on_hand: on_hand), class: 'text-red-500'
end

variant_info =
t('.for_variants', count: product.variants.count)
variant_info =
t('.for_variants', count: product.variants.count)

safe_join([stock_info, variant_info], ' ')
content_tag :div, safe_join([stock_info, variant_info], ' ')
end
}
end

def price_column(product)
content_tag :div, product.master.display_price.to_html
def price_column
{
header: :price,
data: ->(product) do
content_tag :div, product.master.display_price.to_html
end
}
end
end

0 comments on commit 52e7038

Please sign in to comment.