diff --git a/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb b/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb
index e6d297e9904..8017a880cf1 100644
--- a/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb
+++ b/admin/app/components/solidus_admin/adjustment_reasons/index/component.rb
@@ -13,6 +13,20 @@ def search_key
:name_or_code_cont
end
+ def page_actions
+ render component("ui/button").new(
+ tag: :a,
+ text: t('.add'),
+ href: solidus_admin.new_adjustment_reason_path, data: { turbo_frame: :new_adjustment_reason_modal },
+ icon: "add-line",
+ class: "align-self-end w-full",
+ )
+ end
+
+ def turbo_frames
+ %w[new_adjustment_reason_modal]
+ end
+
def row_url(adjustment_reason)
spree.edit_admin_adjustment_reason_path(adjustment_reason)
end
diff --git a/admin/app/components/solidus_admin/adjustment_reasons/new/component.html.erb b/admin/app/components/solidus_admin/adjustment_reasons/new/component.html.erb
new file mode 100644
index 00000000000..1a3e58fda9b
--- /dev/null
+++ b/admin/app/components/solidus_admin/adjustment_reasons/new/component.html.erb
@@ -0,0 +1,27 @@
+<%= turbo_frame_tag :new_adjustment_reason_modal do %>
+ <%= render component("ui/modal").new(title: t(".title")) do |modal| %>
+ <%= form_for @adjustment_reason, url: solidus_admin.adjustment_reasons_path, html: { id: form_id } do |f| %>
+
+ <%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
+ <%= render component("ui/forms/field").text_field(f, :code, class: "required") %>
+
+
+ <% modal.with_actions do %>
+
+ <%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
+ <% end %>
+ <% end %>
+ <% end %>
+<% end %>
+
+<%= render component("adjustment_reasons/index").new(page: @page) %>
diff --git a/admin/app/components/solidus_admin/adjustment_reasons/new/component.rb b/admin/app/components/solidus_admin/adjustment_reasons/new/component.rb
new file mode 100644
index 00000000000..dddf9ddb8a0
--- /dev/null
+++ b/admin/app/components/solidus_admin/adjustment_reasons/new/component.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+class SolidusAdmin::AdjustmentReasons::New::Component < SolidusAdmin::BaseComponent
+ def initialize(page:, adjustment_reason:)
+ @page = page
+ @adjustment_reason = adjustment_reason
+ end
+
+ def form_id
+ dom_id(@adjustment_reason, "#{stimulus_id}_new_adjustment_reason_form")
+ end
+end
diff --git a/admin/app/components/solidus_admin/adjustment_reasons/new/component.yml b/admin/app/components/solidus_admin/adjustment_reasons/new/component.yml
new file mode 100644
index 00000000000..6b4b6f08c44
--- /dev/null
+++ b/admin/app/components/solidus_admin/adjustment_reasons/new/component.yml
@@ -0,0 +1,8 @@
+# Add your component translations here.
+# Use the translation in the example in your template with `t(".hello")`.
+en:
+ title: "New Adjustment Reason"
+ cancel: "Cancel"
+ submit: "Add Adjustment Reason"
+ hints:
+ active: "When checked, this adjustment reason will be available for selection when adding adjustments to orders."
diff --git a/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb b/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb
index 6957634863d..ff548569c12 100644
--- a/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb
+++ b/admin/app/controllers/solidus_admin/adjustment_reasons_controller.rb
@@ -5,18 +5,50 @@ class AdjustmentReasonsController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search
def index
- adjustment_reasons = apply_search_to(
- Spree::AdjustmentReason.order(id: :desc),
- param: :q,
- )
-
- set_page_and_extract_portion_from(adjustment_reasons)
+ set_index_page
respond_to do |format|
format.html { render component('adjustment_reasons/index').new(page: @page) }
end
end
+ def new
+ @adjustment_reason = Spree::AdjustmentReason.new
+
+ set_index_page
+
+ respond_to do |format|
+ format.html { render component('adjustment_reasons/new').new(page: @page, adjustment_reason: @adjustment_reason) }
+ end
+ end
+
+ def create
+ @adjustment_reason = Spree::AdjustmentReason.new(adjustment_reason_params)
+
+ if @adjustment_reason.save
+ respond_to do |format|
+ flash[:notice] = t('.success')
+
+ format.html do
+ redirect_to solidus_admin.adjustment_reasons_path, status: :see_other
+ end
+
+ format.turbo_stream do
+ render turbo_stream: ''
+ end
+ end
+ else
+ set_index_page
+
+ respond_to do |format|
+ format.html do
+ page_component = component('adjustment_reasons/new').new(page: @page, adjustment_reason: @adjustment_reason)
+ render page_component, status: :unprocessable_entity
+ end
+ end
+ end
+ end
+
def destroy
@adjustment_reason = Spree::AdjustmentReason.find_by!(id: params[:id])
@@ -34,7 +66,16 @@ def load_adjustment_reason
end
def adjustment_reason_params
- params.require(:adjustment_reason).permit(:adjustment_reason_id, permitted_adjustment_reason_attributes)
+ params.require(:adjustment_reason).permit(:name, :code, :active)
+ end
+
+ def set_index_page
+ adjustment_reasons = apply_search_to(
+ Spree::AdjustmentReason.order(id: :desc),
+ param: :q,
+ )
+
+ set_page_and_extract_portion_from(adjustment_reasons)
end
end
end
diff --git a/admin/config/locales/adjustment_reasons.en.yml b/admin/config/locales/adjustment_reasons.en.yml
index 90b05a067ce..e812cfa42e3 100644
--- a/admin/config/locales/adjustment_reasons.en.yml
+++ b/admin/config/locales/adjustment_reasons.en.yml
@@ -4,3 +4,5 @@ en:
title: "Adjustment Reasons"
destroy:
success: "Adjustment Reasons were successfully removed."
+ create:
+ success: "Adjustment reason was successfully created."
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index e4cc29b6711..737fc602c2d 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -63,6 +63,6 @@
admin_resources :refund_reasons, only: [:index, :new, :create, :destroy]
admin_resources :reimbursement_types, only: [:index]
admin_resources :return_reasons, only: [:index, :destroy]
- admin_resources :adjustment_reasons, only: [:index, :destroy]
+ admin_resources :adjustment_reasons, only: [:index, :new, :create, :destroy]
admin_resources :store_credit_reasons, only: [:index, :destroy]
end
diff --git a/admin/spec/features/adjustment_reasons_spec.rb b/admin/spec/features/adjustment_reasons_spec.rb
index 4f0dc85a3fb..9b8452d9693 100644
--- a/admin/spec/features/adjustment_reasons_spec.rb
+++ b/admin/spec/features/adjustment_reasons_spec.rb
@@ -19,4 +19,44 @@
expect(Spree::AdjustmentReason.count).to eq(0)
expect(page).to be_axe_clean
end
+
+ context "when creating a new adjustment reason" do
+ let(:query) { "?page=1&q%5Bname_or_code_cont%5D=new" }
+
+ before do
+ visit "/admin/adjustment_reasons#{query}"
+ click_on "Add new"
+ expect(page).to have_content("New Adjustment Reason")
+ expect(page).to be_axe_clean
+ end
+
+ it "opens a modal" do
+ expect(page).to have_selector("dialog")
+ within("dialog") { click_on "Cancel" }
+ expect(page).not_to have_selector("dialog")
+ expect(page.current_url).to include(query)
+ end
+
+ context "with valid data" do
+ it "successfully creates a new adjustment reason, keeping page and q params" do
+ fill_in "Name", with: "New Reason"
+ fill_in "Code", with: "1234"
+
+ click_on "Add Adjustment Reason"
+
+ expect(page).to have_content("Adjustment reason was successfully created.")
+ expect(Spree::AdjustmentReason.find_by(name: "New Reason")).to be_present
+ expect(page.current_url).to include(query)
+ end
+ end
+
+ context "with invalid data" do
+ it "fails to create a new adjustment reason, keeping page and q params" do
+ click_on "Add Adjustment Reason"
+
+ expect(page).to have_content("can't be blank").twice
+ expect(page.current_url).to include(query)
+ end
+ end
+ end
end