Skip to content

Commit

Permalink
Add an index page for shipping categories
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Dec 5, 2023
1 parent 3be4d3b commit ee90498
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions admin/app/components/solidus_admin/shipping/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(current_class:)
def tabs
{
Spree::ShippingMethod => solidus_admin.shipping_methods_path,
Spree::ShippingCategory => solidus_admin.shipping_categories_path,
}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= render component('shipping').new(current_class: Spree::ShippingCategory) do |layout| %>
<% layout.with_actions do %>
<%= render component("ui/button").new(
tag: :a,
text: t('.add'),
href: spree.new_admin_shipping_category_path,
icon: "add-line",
class: "align-self-end w-full",
) %>
<% end %>

<%= render component('ui/table').new(
id: stimulus_id,
data: {
class: Spree::ShippingCategory,
rows: @page.records,
url: ->(shipping_category) { spree.edit_admin_shipping_category_path(shipping_category) },
prev: prev_page_path,
next: next_page_path,
columns: columns,
batch_actions: batch_actions,
},
search: {
name: :q,
value: params[:q],
url: solidus_admin.shipping_categories_path,
searchbar_key: :name_or_description_cont,
filters: filters,
scopes: scopes,
},
) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

class SolidusAdmin::ShippingCategories::Index::Component < SolidusAdmin::BaseComponent
include SolidusAdmin::Layout::PageHelpers

def initialize(page:)
@page = page
end

def title
Spree::ShippingCategory.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
[
{
display_name: t('.batch_actions.delete'),
action: solidus_admin.shipping_categories_path,
method: :delete,
icon: 'delete-bin-7-line',
},
]
end

def filters
[]
end

def scopes
[]
end

def columns
[
:name
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
add: 'Add new'
batch_actions:
delete: 'Delete'
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module SolidusAdmin
class ShippingCategoriesController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

def index
shipping_categories = apply_search_to(
Spree::ShippingCategory.order(id: :desc),
param: :q,
)

set_page_and_extract_portion_from(shipping_categories)

respond_to do |format|
format.html { render component('shipping_categories/index').new(page: @page) }
end
end

def destroy
@shipping_category = Spree::ShippingCategory.find_by!(id: params[:id])

Spree::ShippingCategory.transaction { @shipping_category.destroy }

flash[:notice] = t('.success')
redirect_back_or_to shipping_categories_path, status: :see_other
end

private

def load_shipping_category
@shipping_category = Spree::ShippingCategory.find_by!(id: params[:id])
authorize! action_name, @shipping_category
end

def shipping_category_params
params.require(:shipping_category).permit(:shipping_category_id, permitted_shipping_category_attributes)
end
end
end
5 changes: 5 additions & 0 deletions admin/config/locales/shipping_categories.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
en:
shipping_categories:
title: "Shipping Categories"
destroy:
success: "Shipping categories were successfully removed."
1 change: 1 addition & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@
admin_resources :tax_rates, only: [:index, :destroy]
admin_resources :payment_methods, only: [:index, :destroy], sortable: true
admin_resources :shipping_methods, only: [:index, :destroy]
admin_resources :shipping_categories, only: [:index, :destroy]
end

0 comments on commit ee90498

Please sign in to comment.