Skip to content
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

[Admin] Tweaks for the product listing #5176

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,9 @@
rows: @page.records,
search_key: SolidusAdmin::Config[:product_search_key],
search_url: solidus_admin.products_path,
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) } }
],
prev_page_link: prev_page_link,
next_page_link: next_page_link,
)
%>
batch_actions: batch_actions,
columns: columns,
prev_page_link: prev_page_link,
next_page_link: next_page_link,
) %>
</div>
119 changes: 89 additions & 30 deletions admin/app/components/solidus_admin/products/index/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,106 @@ def next_page_link
@page.last? ? nil : solidus_admin.url_for(host: request.host, port: request.port, **request.params, page: @page.next_param)
end

def image_column(product)
image = product.gallery.images.first or return
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

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,
)
def columns
[
image_column,
name_column,
status_column,
price_column,
stock_column,
]
end

def name_column(product)
link_to product.name, spree.edit_admin_product_path(product)
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 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
def name_column
{
header: :name,
data: ->(product) do
link_to product.name, spree.edit_admin_product_path(product)
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 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
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
<% if @prev_page_link || @next_page_link %>
<tfoot>
<tr>
<td colspan="<%= @columns.size %>" class="py-4">
<td colspan="<%= @columns.size %>" class="py-4 bg-white">
<div class="flex justify-center">
<%= render @pagination_component.new(
prev_link: @prev_page_link,
Expand Down
26 changes: 4 additions & 22 deletions admin/app/components/solidus_admin/ui/table/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,8 @@ def render_batch_action_button(batch_action)
end

def render_header_cell(cell, **attrs)
cell =
case cell
when Symbol
@model_class.human_attribute_name(cell)
when Proc
cell.call
else
cell
end

# Allow component instances as cell content
cell = cell.call if cell.respond_to?(:call)
cell = @model_class.human_attribute_name(cell) if cell.is_a?(Symbol)
cell = cell.render_in(self) if cell.respond_to?(:render_in)

content_tag(:th, cell, class: %{
Expand All @@ -137,17 +128,8 @@ def render_header_cell(cell, **attrs)
end

def render_data_cell(cell, data)
cell =
case cell
when Symbol
data.public_send(cell)
when Proc
cell.call(data)
else
cell
end

# Allow component instances as cell content
cell = cell.call(data) if cell.respond_to?(:call)
cell = data.public_send(cell) if cell.is_a?(Symbol)
cell = cell.render_in(self) if cell.respond_to?(:render_in)

content_tag(:td, content_tag(:div, cell, class: "flex items-center gap-1.5"), class: "py-2 px-4 h-10 vertical-align-middle leading-none")
Expand Down