diff --git a/admin/app/components/solidus_admin/refunds_and_returns/component.rb b/admin/app/components/solidus_admin/refunds_and_returns/component.rb index 21a46a1928c..8d24892c73d 100644 --- a/admin/app/components/solidus_admin/refunds_and_returns/component.rb +++ b/admin/app/components/solidus_admin/refunds_and_returns/component.rb @@ -9,12 +9,12 @@ def initialize(current_class:) end def tabs - {} { Spree::RefundReason => solidus_admin.refund_reasons_path, Spree::ReimbursementType => solidus_admin.reimbursement_types_path, Spree::ReturnReason => solidus_admin.return_reasons_path, Spree::AdjustmentReason => solidus_admin.adjustment_reasons_path, + Spree::StoreCreditReason => solidus_admin.store_credit_reasons_path, } end end diff --git a/admin/app/components/solidus_admin/store_credit_reasons/index/component.html.erb b/admin/app/components/solidus_admin/store_credit_reasons/index/component.html.erb new file mode 100644 index 00000000000..a4e56ee9501 --- /dev/null +++ b/admin/app/components/solidus_admin/store_credit_reasons/index/component.html.erb @@ -0,0 +1,31 @@ +<%= render component('refunds_and_returns').new(current_class: Spree::StoreCreditReason) do |layout| %> + <% layout.with_actions do %> + <%= render component("ui/button").new( + tag: :a, + text: t('.add'), + href: spree.new_admin_store_credit_reason_path, + icon: "add-line", + class: "align-self-end w-full", + ) %> + <% end %> + <%= render component('ui/table').new( + id: stimulus_id, + data: { + class: Spree::StoreCreditReason, + rows: @page.records, + url: ->(store_credit_reason) { spree.edit_admin_store_credit_reason_path(store_credit_reason) }, + prev: prev_page_path, + next: next_page_path, + columns: columns, + batch_actions: batch_actions, + }, + search: { + name: :q, + value: params[:q], + url: solidus_admin.store_credit_reasons_path, + searchbar_key: :name_cont, + filters: filters, + scopes: scopes, + }, + ) %> +<% end %> diff --git a/admin/app/components/solidus_admin/store_credit_reasons/index/component.rb b/admin/app/components/solidus_admin/store_credit_reasons/index/component.rb new file mode 100644 index 00000000000..18898313bac --- /dev/null +++ b/admin/app/components/solidus_admin/store_credit_reasons/index/component.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +class SolidusAdmin::StoreCreditReasons::Index::Component < SolidusAdmin::BaseComponent + include SolidusAdmin::Layout::PageHelpers + + def initialize(page:) + @page = page + end + + def title + Spree::StoreCreditReason.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.store_credit_reasons_path, + method: :delete, + icon: 'delete-bin-7-line', + }, + ] + end + + def filters + [] + end + + def scopes + [] + end + + def columns + [ + :name, + { + header: :active, + data: ->(store_credit_reason) do + store_credit_reason.active? ? component('ui/badge').yes : component('ui/badge').no + end + }, + ] + end +end diff --git a/admin/app/components/solidus_admin/store_credit_reasons/index/component.yml b/admin/app/components/solidus_admin/store_credit_reasons/index/component.yml new file mode 100644 index 00000000000..54418702ad3 --- /dev/null +++ b/admin/app/components/solidus_admin/store_credit_reasons/index/component.yml @@ -0,0 +1,4 @@ +en: + add: 'Add new' + batch_actions: + delete: 'Delete' diff --git a/admin/app/controllers/solidus_admin/store_credit_reasons_controller.rb b/admin/app/controllers/solidus_admin/store_credit_reasons_controller.rb new file mode 100644 index 00000000000..6596857ca25 --- /dev/null +++ b/admin/app/controllers/solidus_admin/store_credit_reasons_controller.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module SolidusAdmin + class StoreCreditReasonsController < SolidusAdmin::BaseController + include SolidusAdmin::ControllerHelpers::Search + + def index + store_credit_reasons = apply_search_to( + Spree::StoreCreditReason.order(id: :desc), + param: :q, + ) + + set_page_and_extract_portion_from(store_credit_reasons) + + respond_to do |format| + format.html { render component('store_credit_reasons/index').new(page: @page) } + end + end + + def destroy + @store_credit_reason = Spree::StoreCreditReason.find_by!(id: params[:id]) + + Spree::StoreCreditReason.transaction { @store_credit_reason.destroy } + + flash[:notice] = t('.success') + redirect_back_or_to store_credit_reasons_path, status: :see_other + end + + private + + def load_store_credit_reason + @store_credit_reason = Spree::StoreCreditReason.find_by!(id: params[:id]) + authorize! action_name, @store_credit_reason + end + + def store_credit_reason_params + params.require(:store_credit_reason).permit(:store_credit_reason_id, permitted_store_credit_reason_attributes) + end + end +end diff --git a/admin/config/locales/store_credit_reasons.en.yml b/admin/config/locales/store_credit_reasons.en.yml new file mode 100644 index 00000000000..7d4812eacc4 --- /dev/null +++ b/admin/config/locales/store_credit_reasons.en.yml @@ -0,0 +1,6 @@ +en: + solidus_admin: + store_credit_reasons: + title: "Store Credit Reasons" + destroy: + success: "Store Credit Reasons were successfully removed." diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 685df83afbc..8d1fb7c545a 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -48,4 +48,5 @@ admin_resources :reimbursement_types, only: [:index] admin_resources :return_reasons, only: [:index, :destroy] admin_resources :adjustment_reasons, only: [:index, :destroy] + admin_resources :store_credit_reasons, only: [:index, :destroy] end diff --git a/admin/spec/features/store_credit_reasons_spec.rb b/admin/spec/features/store_credit_reasons_spec.rb new file mode 100644 index 00000000000..5a8ffff0e20 --- /dev/null +++ b/admin/spec/features/store_credit_reasons_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe "Store Credit Reasons", :js, type: :feature do + before { sign_in create(:admin_user, email: 'admin@example.com') } + + it "lists Store Credit Reasons and allows deleting them" do + create(:store_credit_reason, name: "Default-store-credit-reason") + + visit "/admin/store_credit_reasons" + expect(page).to have_content("Default-store-credit-reason") + expect(page).to be_axe_clean + + select_row("Default-store-credit-reason") + click_on "Delete" + expect(page).to have_content("Store Credit Reasons were successfully removed.") + expect(page).not_to have_content("Default-store-credit-reason") + expect(Spree::StoreCreditReason.count).to eq(0) + expect(page).to be_axe_clean + end +end