From 088c8954acffdc2ed499510a7a7b88fedefb5360 Mon Sep 17 00:00:00 2001 From: Rainer Dema Date: Fri, 1 Dec 2023 12:44:53 +0100 Subject: [PATCH] [wip] Add `stock_items/index` component with dedicated actions --- .../stock_items/index/component.html.erb | 24 ++++ .../stock_items/index/component.rb | 118 ++++++++++++++++++ .../stock_items/index/component.yml | 3 + .../solidus_admin/stock_items_controller.rb | 23 ++++ admin/config/locales/stock_items.en.yml | 4 + admin/config/routes.rb | 2 + admin/spec/features/stock_items_spec.rb | 12 ++ 7 files changed, 186 insertions(+) create mode 100644 admin/app/components/solidus_admin/stock_items/index/component.html.erb create mode 100644 admin/app/components/solidus_admin/stock_items/index/component.rb create mode 100644 admin/app/components/solidus_admin/stock_items/index/component.yml create mode 100644 admin/app/controllers/solidus_admin/stock_items_controller.rb create mode 100644 admin/config/locales/stock_items.en.yml create mode 100644 admin/spec/features/stock_items_spec.rb diff --git a/admin/app/components/solidus_admin/stock_items/index/component.html.erb b/admin/app/components/solidus_admin/stock_items/index/component.html.erb new file mode 100644 index 00000000000..1c8f9db172e --- /dev/null +++ b/admin/app/components/solidus_admin/stock_items/index/component.html.erb @@ -0,0 +1,24 @@ +<%= page do %> + <%= page_header do %> + <%= page_header_title title %> + <% end %> + + <%= render component('ui/table').new( + id: stimulus_id, + data: { + class: Spree::StockItem, + rows: @page.records, + prev: prev_page_path, + next: next_page_path, + columns: columns, + batch_actions: batch_actions, + }, + search: { + name: :q, + value: params[:q], + url: solidus_admin.stock_items_path, + searchbar_key: nil, + filters: filters, + }, + ) %> +<% end %> diff --git a/admin/app/components/solidus_admin/stock_items/index/component.rb b/admin/app/components/solidus_admin/stock_items/index/component.rb new file mode 100644 index 00000000000..484f8f590da --- /dev/null +++ b/admin/app/components/solidus_admin/stock_items/index/component.rb @@ -0,0 +1,118 @@ +# frozen_string_literal: true + +class SolidusAdmin::StockItems::Index::Component < SolidusAdmin::BaseComponent + include SolidusAdmin::Layout::PageHelpers + + def initialize(page:) + @page = page + end + + def title + Spree::StockItem.model_name.human.pluralize + end + + def prev_page_path + solidus_admin.url_for(**request.params, page: @page.number - 1, only_path: true) unless @page.first? + end + + def next_page_path + solidus_admin.url_for(**request.params, page: @page.next_param, only_path: true) unless @page.last? + end + + def batch_actions + [] + end + + def filters + [] + end + + def columns + [ + image_column, + name_column, + sku_column, + variant_column, + stock_location_column, + back_orderable_column, + count_on_hand_column, + ] + end + + def image_column + { + col: { class: "w-[72px]" }, + header: tag.span('aria-label': t('.image'), role: 'text'), + data: ->(stock_item) do + image = stock_item.variant.gallery.images.first or return + + render( + component('ui/thumbnail').new( + src: image.url(:small), + alt: stock_item.variant.name + ) + ) + end + } + end + + def name_column + { + header: :name, + data: ->(stock_item) do + content_tag :div, stock_item.variant.name + end + } + end + + def sku_column + { + header: :sku, + data: ->(stock_item) do + content_tag :div, stock_item.variant.sku + end + } + end + + def variant_column + { + header: :variant, + data: ->(stock_item) do + stock_item.variant.option_values.sort_by(&:option_type_name).map do |option_value| + "#{option_value.option_type_presentation} #{option_value.presentation}" + end + end + } + end + + def stock_location_column + { + header: :stock_location, + data: ->(stock_item) do + link_to stock_item.stock_location.name, spree.admin_stock_location_stock_movements_path(stock_item.stock_location.id, q: { variant_sku_eq: stock_item.variant.sku }) + end + } + end + + def back_orderable_column + { + header: :back_orderable, + data: ->(stock_item) do + if stock_item.backorderable? + component('ui/badge').new(name: t('.yes'), color: :green) + else + component('ui/badge').new(name: t('.no'), color: :graphite_light) + end + end + } + end + + def count_on_hand_column + { + header: :count_on_hand, + data: ->(stock_item) do + content_tag :div, stock_item.count_on_hand + end + } + end +end diff --git a/admin/app/components/solidus_admin/stock_items/index/component.yml b/admin/app/components/solidus_admin/stock_items/index/component.yml new file mode 100644 index 00000000000..b52b7f94a2d --- /dev/null +++ b/admin/app/components/solidus_admin/stock_items/index/component.yml @@ -0,0 +1,3 @@ +en: + "yes": "Yes" + "no": "No" diff --git a/admin/app/controllers/solidus_admin/stock_items_controller.rb b/admin/app/controllers/solidus_admin/stock_items_controller.rb new file mode 100644 index 00000000000..9d88277d8a8 --- /dev/null +++ b/admin/app/controllers/solidus_admin/stock_items_controller.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module SolidusAdmin + class StockItemsController < SolidusAdmin::BaseController + include SolidusAdmin::ControllerHelpers::Search + + def index + stock_items = apply_search_to( + Spree::StockItem.order(created_at: :desc, id: :desc), + param: :q, + ) + + set_page_and_extract_portion_from( + stock_items, + per_page: 20 + ) + + respond_to do |format| + format.html { render component('stock_items/index').new(page: @page) } + end + end + end +end diff --git a/admin/config/locales/stock_items.en.yml b/admin/config/locales/stock_items.en.yml new file mode 100644 index 00000000000..b1ad9f018e2 --- /dev/null +++ b/admin/config/locales/stock_items.en.yml @@ -0,0 +1,4 @@ +en: + solidus_admin: + stock_items: + title: "Stock Items" diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 7513e01a28e..b7bb27ac46e 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -93,4 +93,6 @@ patch :move end end + + resources :stock_items, only: [:index] end diff --git a/admin/spec/features/stock_items_spec.rb b/admin/spec/features/stock_items_spec.rb new file mode 100644 index 00000000000..9c555899296 --- /dev/null +++ b/admin/spec/features/stock_items_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe "Stock Items", :js, type: :feature do + before { sign_in create(:admin_user, email: 'admin@example.com') } + + it "lists stock items" do + visit "/admin/stock_items" + expect(page).to be_axe_clean + end +end