Skip to content

Commit

Permalink
Create adjustment reasons via new admin UI modal
Browse files Browse the repository at this point in the history
  • Loading branch information
MadelineCollier committed Aug 7, 2024
1 parent f2a32d6 commit c73925a
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
<%= render component("ui/forms/field").text_field(f, :code, class: "required") %>
<label class="flex gap-2 items-center">
<%= render component("ui/forms/checkbox").new(
name: "#{f.object_name}[active]",
value: "1",
checked: f.object.active
) %>
<span class="font-semibold text-xs ml-2"><%= Spree::TaxCategory.human_attribute_name :active %></span>
<%= render component("ui/toggletip").new(text: t(".hints.active")) %>
</label>
</div>
<% modal.with_actions do %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<%= 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) %>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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."
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<turbo-stream action="refresh" />'
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])

Expand All @@ -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
2 changes: 2 additions & 0 deletions admin/config/locales/adjustment_reasons.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ en:
title: "Adjustment Reasons"
destroy:
success: "Adjustment Reasons were successfully removed."
create:
success: "Adjustment reason was successfully created."
2 changes: 1 addition & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions admin/spec/features/adjustment_reasons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c73925a

Please sign in to comment.