From 86cd7991a1b5e4831b9803aa79e9323b192a8bd7 Mon Sep 17 00:00:00 2001 From: Rainer Dema Date: Tue, 5 Dec 2023 13:01:46 +0100 Subject: [PATCH] Add `stores/index` component with dedicated actions --- .../stores/index/component.html.erb | 32 ++++++++++++ .../solidus_admin/stores/index/component.rb | 51 +++++++++++++++++++ .../solidus_admin/stores/index/component.yml | 4 ++ .../solidus_admin/stores_controller.rb | 27 ++++++++++ admin/config/locales/stores.en.yml | 6 +++ admin/config/routes.rb | 1 + admin/spec/features/stores_spec.rb | 26 ++++++++++ 7 files changed, 147 insertions(+) create mode 100644 admin/app/components/solidus_admin/stores/index/component.html.erb create mode 100644 admin/app/components/solidus_admin/stores/index/component.rb create mode 100644 admin/app/components/solidus_admin/stores/index/component.yml create mode 100644 admin/app/controllers/solidus_admin/stores_controller.rb create mode 100644 admin/config/locales/stores.en.yml create mode 100644 admin/spec/features/stores_spec.rb diff --git a/admin/app/components/solidus_admin/stores/index/component.html.erb b/admin/app/components/solidus_admin/stores/index/component.html.erb new file mode 100644 index 00000000000..1eca023a3f3 --- /dev/null +++ b/admin/app/components/solidus_admin/stores/index/component.html.erb @@ -0,0 +1,32 @@ +<%= page do %> + <%= page_header do %> + <%= page_header_title title %> + <%= page_header_actions do %> + <%= render component("ui/button").new( + tag: :a, + text: t('.add'), + href: spree.new_admin_store_path, + icon: "add-line", + ) %> + <% end %> + <% end %> + + <%= render component('ui/table').new( + id: stimulus_id, + data: { + class: Spree::Store, + rows: @stores, + url: ->(store) { spree.edit_admin_store_path(store) }, + columns: columns, + batch_actions: batch_actions, + }, + search: { + name: :q, + value: params[:q], + url: solidus_admin.stores_path, + searchbar_key: :name_or_url_or_code_cont, + filters: filters, + scopes: scopes, + }, + ) %> +<% end %> diff --git a/admin/app/components/solidus_admin/stores/index/component.rb b/admin/app/components/solidus_admin/stores/index/component.rb new file mode 100644 index 00000000000..191ae37da41 --- /dev/null +++ b/admin/app/components/solidus_admin/stores/index/component.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +class SolidusAdmin::Stores::Index::Component < SolidusAdmin::BaseComponent + include SolidusAdmin::Layout::PageHelpers + + def initialize(stores:) + @stores = stores + end + + def title + Spree::Store.model_name.human.pluralize + end + + def batch_actions + [ + { + display_name: t('.batch_actions.delete'), + action: solidus_admin.stores_path, + method: :delete, + icon: 'delete-bin-7-line', + }, + ] + end + + def filters + [] + end + + def scopes + [] + end + + def columns + [ + :name, + :url, + { + header: :slug, + data: ->(store) do + content_tag :div, store.code + end + }, + { + header: :default, + data: ->(store) do + store.default? ? component('ui/badge').yes : component('ui/badge').no + end + }, + ] + end +end diff --git a/admin/app/components/solidus_admin/stores/index/component.yml b/admin/app/components/solidus_admin/stores/index/component.yml new file mode 100644 index 00000000000..54418702ad3 --- /dev/null +++ b/admin/app/components/solidus_admin/stores/index/component.yml @@ -0,0 +1,4 @@ +en: + add: 'Add new' + batch_actions: + delete: 'Delete' diff --git a/admin/app/controllers/solidus_admin/stores_controller.rb b/admin/app/controllers/solidus_admin/stores_controller.rb new file mode 100644 index 00000000000..92ddb8d5e4f --- /dev/null +++ b/admin/app/controllers/solidus_admin/stores_controller.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module SolidusAdmin + class StoresController < SolidusAdmin::BaseController + include SolidusAdmin::ControllerHelpers::Search + + def index + @stores = apply_search_to( + Spree::Store.order(created_at: :desc, id: :desc), + param: :q, + ) + + respond_to do |format| + format.html { render component('stores/index').new(stores: @stores) } + end + end + + def destroy + @stores = Spree::Store.where(id: params[:id]) + + Spree::Store.transaction { @stores.destroy_all } + + flash[:notice] = t('.success') + redirect_back_or_to stores_path, status: :see_other + end + end +end diff --git a/admin/config/locales/stores.en.yml b/admin/config/locales/stores.en.yml new file mode 100644 index 00000000000..4f901a50d6d --- /dev/null +++ b/admin/config/locales/stores.en.yml @@ -0,0 +1,6 @@ +en: + solidus_admin: + stores: + title: "Stores" + destroy: + success: "Stores were successfully removed." diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 3380e1fb21d..fcafd50fbfc 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -41,4 +41,5 @@ admin_resources :tax_rates, only: [:index, :destroy] admin_resources :payment_methods, only: [:index, :destroy], sortable: true admin_resources :stock_items, only: [:index] + admin_resources :stores, only: [:index, :destroy] end diff --git a/admin/spec/features/stores_spec.rb b/admin/spec/features/stores_spec.rb new file mode 100644 index 00000000000..a30f49bb5eb --- /dev/null +++ b/admin/spec/features/stores_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe "Stores", :js, type: :feature do + before { sign_in create(:admin_user, email: 'admin@example.com') } + + it "lists stores and allows deleting them" do + store1 = create(:store) + store2 = create(:store) + + visit "/admin/stores" + + expect(page).to have_content(store1.name) + expect(page).to have_content(store2.name) + + expect(page).to be_axe_clean + + select_row(store2.name) + click_on "Delete" + + expect(page).to have_content("Stores were successfully removed.") + expect(page).not_to have_content(store2.name) + expect(Spree::Store.count).to eq(1) + end +end