Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Implement bulk actions for Price Lists Products

Damian Legawiec569c8d83bde0
diff --git a/admin/app/assets/tailwind/spree/admin/components/_buttons.css b/admin/app/assets/tailwind/spree/admin/components/_buttons.css
index 23332679f9..c29af5fb74 100644
--- a/admin/app/assets/tailwind/spree/admin/components/_buttons.css
+++ b/admin/app/assets/tailwind/spree/admin/components/_buttons.css
@@ -43,7 +43,7 @@
.btn-secondary:hover,
.btn-secondary:focus {
- @apply border-blue-200;
+ @apply border-blue-100 bg-blue-100/75;
}
.btn-secondary .arrow {
diff --git a/admin/app/views/spree/admin/price_list_products/index.html.erb b/admin/app/views/spree/admin/price_list_products/index.html.erb
index e31a708eb6..2af50cf9b8 100644
--- a/admin/app/views/spree/admin/price_list_products/index.html.erb
+++ b/admin/app/views/spree/admin/price_list_products/index.html.erb
@@ -1 +1 @@
-<%= render_table @price_list.products, :price_list_products, container_class: 'card-body p-0' %>
+<%= render_table @collection, :price_list_products, container_class: 'card-body p-0', bulk_operations: true %>
diff --git a/admin/app/views/spree/admin/price_lists/_rules.html.erb b/admin/app/views/spree/admin/price_lists/_rules.html.erb
index 77c5ecdc4d..66dbd4a1d3 100644
--- a/admin/app/views/spree/admin/price_lists/_rules.html.erb
+++ b/admin/app/views/spree/admin/price_lists/_rules.html.erb
@@ -20,7 +20,7 @@
<div class="text-gray-600 p-12 flex items-center w-full justify-center">
<%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::PriceRule)) %>
<% if can?(:create, Spree::PriceRule.new(price_list: @price_list)) %>
- <%= link_to_with_icon 'plus', Spree.t(:add_one), spree.new_admin_price_list_price_rule_path(@price_list), class: 'btn btn-light btn-sm ml-6', data: { turbo_frame: 'dialog', action: 'dialog#open' } %>
+ <%= link_to_with_icon 'plus', Spree.t(:add_one), spree.new_admin_price_list_price_rule_path(@price_list), class: 'btn btn-secondary btn-sm ml-6', data: { turbo_frame: 'dialog', action: 'dialog#open' } %>
<% end %>
</div>
<% else %>
diff --git a/admin/app/views/spree/admin/price_lists/_table_row.html.erb b/admin/app/views/spree/admin/price_lists/_table_row.html.erb
deleted file mode 100644
index 8f5452b2d5..0000000000
--- a/admin/app/views/spree/admin/price_lists/_table_row.html.erb
+++ /dev/null
@@ -1,27 +0,0 @@
-<tr id="<%= spree_dom_id price_list %>" data-controller="row-link" data-sortable-update-url="<%= spree.admin_price_list_path(price_list) %>">
- <td class="w-5 move-handle text-center">
- <%= icon('grip-vertical', class: 'rounded-md hover:bg-gray-100 p-2') %>
- </td>
- <td class="w-30 p-0">
- <%= link_to price_list.name, spree.admin_price_list_path(price_list), class: 'font-bold block py-2 px-4 w-full h-full', data: { row_link_target: :link, turbo_frame: '_top' } %>
- </td>
- <td class="w-15 cursor-pointer" data-action="click->row-link#openLink">
- <span class="badge badge-<%= price_list.active? ? 'success' : (price_list.scheduled? ? 'info' : 'light') %>">
- <%= price_list.status.humanize %>
- </span>
- </td>
- <td class="w-20 cursor-pointer" data-action="click->row-link#openLink">
- <% if price_list.starts_at.present? %>
- <%= spree_date(price_list.starts_at) %>
- <% else %>
- <span class="text-gray-400">-</span>
- <% end %>
- </td>
- <td class="w-20 cursor-pointer" data-action="click->row-link#openLink">
- <% if price_list.ends_at.present? %>
- <%= spree_date(price_list.ends_at) %>
- <% else %>
- <span class="text-gray-400">-</span>
- <% end %>
- </td>
-</tr>
diff --git a/admin/app/views/spree/admin/price_lists/index.html.erb b/admin/app/views/spree/admin/price_lists/index.html.erb
index bb038a30ad..cbd2229712 100644
--- a/admin/app/views/spree/admin/price_lists/index.html.erb
+++ b/admin/app/views/spree/admin/price_lists/index.html.erb
@@ -9,4 +9,4 @@
<%= render_admin_partials(:price_lists_header_partials) %>
-<%= render 'spree/admin/shared/index_table', sortable: true %>
+<%= render_table @collection, :price_lists %>
diff --git a/admin/app/views/spree/admin/tables/columns/_price_list_status.html.erb b/admin/app/views/spree/admin/tables/columns/_price_list_status.html.erb
new file mode 100644
index 0000000000..c5154a1384
--- /dev/null
+++ b/admin/app/views/spree/admin/tables/columns/_price_list_status.html.erb
@@ -0,0 +1,9 @@
+<% if record.scheduled? && record.currently_active? %>
+ <span class="badge badge-success">
+ <%= Spree.t('price_list_statuses.scheduled_active') %>
+ </span>
+<% else %>
+ <span class="badge badge-<%= record.currently_active? ? 'success' : (record.scheduled? ? 'info' : 'light') %>">
+ <%= Spree.t("price_list_statuses.#{record.status}") %>
+ </span>
+<% end %>
diff --git a/admin/config/initializers/spree_admin_tables.rb b/admin/config/initializers/spree_admin_tables.rb
index 15a158f8c4..85acba4a86 100644
--- a/admin/config/initializers/spree_admin_tables.rb
+++ b/admin/config/initializers/spree_admin_tables.rb
@@ -1426,6 +1426,67 @@ Rails.application.config.after_initialize do
default: false,
position: 80
+ # ==========================================
+ # Price Lists Table
+ # ==========================================
+ Spree.admin.tables.register(:price_lists, model_class: Spree::PriceList, search_param: :name_cont, row_actions: true, link_to_action: :show)
+
+ Spree.admin.tables.price_lists.add :name,
+ label: :name,
+ type: :link,
+ sortable: true,
+ filterable: true,
+ default: true,
+ position: 10
+
+ Spree.admin.tables.price_lists.add :status,
+ label: :status,
+ type: :custom,
+ filter_type: :select,
+ sortable: true,
+ filterable: true,
+ default: true,
+ position: 20,
+ partial: 'spree/admin/tables/columns/price_list_status',
+ value_options: [
+ { value: 'draft', label: 'Draft' },
+ { value: 'active', label: 'Active' },
+ { value: 'scheduled', label: 'Scheduled' },
+ { value: 'inactive', label: 'Inactive' }
+ ]
+
+ Spree.admin.tables.price_lists.add :starts_at,
+ label: :starts_at,
+ type: :datetime,
+ sortable: true,
+ filterable: true,
+ default: true,
+ position: 30
+
+ Spree.admin.tables.price_lists.add :ends_at,
+ label: :ends_at,
+ type: :datetime,
+ sortable: true,
+ filterable: true,
+ default: true,
+ position: 40
+
+ Spree.admin.tables.price_lists.add :created_at,
+ label: :created_at,
+ type: :datetime,
+ sortable: true,
+ filterable: true,
+ default: false,
+ position: 50
+
+ Spree.admin.tables.price_lists.add :updated_at,
+ label: :updated_at,
+ type: :datetime,
+ sortable: true,
+ filterable: true,
+ default: false,
+ position: 60
+
# ==========================================
# Price List Products Table
# ==========================================
@@ -1476,4 +1537,14 @@ Rails.application.config.after_initialize do
filterable: true,
default: false,
position: 60
+
+ Spree.admin.tables.price_list_products.add_bulk_action :remove_from_price_list,
+ label: 'admin.bulk_ops.price_list_products.title.remove',
+ icon: 'trash',
+ modal_path: ->(view_context) { view_context.spree.bulk_modal_admin_price_list_products_path(view_context.instance_variable_get(:@price_list), kind: :remove_from_price_list) },
+ action_path: ->(view_context) { view_context.spree.bulk_destroy_admin_price_list_products_path(view_context.instance_variable_get(:@price_list)) },
+ title: 'admin.bulk_ops.price_list_products.title.remove',
+ body: 'admin.bulk_ops.price_list_products.body.remove',
+ method: :delete,
+ position: 10
end
diff --git a/admin/config/locales/en.yml b/admin/config/locales/en.yml
index 98cf695b6b..b5a0e4b5c8 100644
--- a/admin/config/locales/en.yml
+++ b/admin/config/locales/en.yml
@@ -7,6 +7,11 @@ en:
avg_order_value: Avg. Order Value
back_to_dashboard: Back to dashboard
bulk_ops:
+ price_list_products:
+ body:
+ remove: Are you sure you want to remove the selected products from this price list? This will also remove their prices.
+ title:
+ remove: Remove from Price List
products:
body:
add_tags: 'Select tags to add to the products:'
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index 9959df9fcf..46c70516e8 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -38,6 +38,7 @@ Spree::Core::Engine.add_routes do
resources :products, only: [:index], controller: 'price_list_products' do
collection do
get :bulk_new
+ get :bulk_modal
post :bulk_create
delete :bulk_destroy
end
diff --git a/admin/spec/controllers/spree/admin/price_list_products_controller_spec.rb b/admin/spec/controllers/spree/admin/price_list_products_controller_spec.rb
index bafcc6b2f0..4ad6dee75c 100644
--- a/admin/spec/controllers/spree/admin/price_list_products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/price_list_products_controller_spec.rb
@@ -53,7 +53,7 @@ RSpec.describe Spree::Admin::PriceListProductsController, type: :controller do
let(:params) do
{
price_list_id: price_list.id,
- product_ids: [product1.id, product2.id]
+ ids: [product1.id, product2.id]
}
end
@@ -110,7 +110,7 @@ RSpec.describe Spree::Admin::PriceListProductsController, type: :controller do
let(:params) do
{
price_list_id: price_list.id,
- product_ids: [product_with_variants.id]
+ ids: [product_with_variants.id]
}
end
@@ -126,7 +126,7 @@ RSpec.describe Spree::Admin::PriceListProductsController, type: :controller do
let(:params) do
{
price_list_id: price_list.id,
- product_ids: [product_master_only.id]
+ ids: [product_master_only.id]
}
end
@@ -139,8 +139,8 @@ RSpec.describe Spree::Admin::PriceListProductsController, type: :controller do
end
end
- context 'with empty product_ids' do
- let(:params) { { price_list_id: price_list.id, product_ids: [] } }
+ context 'with empty ids' do
+ let(:params) { { price_list_id: price_list.id, ids: [] } }
it 'does not create any prices' do
expect { bulk_create }.not_to change(Spree::Price.for_price_list(price_list), :count)
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 31f76b60cb..dc06880623 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1691,14 +1691,16 @@ en:
previous: Previous
previously_used_card: Previously Used Cards
price: Price
- price_list_statuses:
- active: Active
- inactive: Inactive
- scheduled: Scheduled
price_list_form:
match_policies:
all: Match all of these rules
any: Match any of these rules
+ price_list_statuses:
+ active: Active
+ draft: Draft
+ inactive: Inactive
+ scheduled: Scheduled
+ scheduled_active: Scheduled (Active)
price_lists: Price Lists
price_range: Price Range
price_rule_types: