Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Create new Refund Reasons #5696

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ def row_url(refund_reason)
spree.edit_admin_refund_reason_path(refund_reason)
end

def actions
def turbo_frames
%w[new_refund_reason_modal]
end

def page_actions
render component("ui/button").new(
tag: :a,
text: t('.add'),
href: spree.new_admin_refund_reason_path,
href: solidus_admin.new_refund_reason_path, data: { turbo_frame: :new_refund_reason_modal },
icon: "add-line",
class: "align-self-end w-full",
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= turbo_frame_tag :new_refund_reason_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @refund_reason, url: solidus_admin.refund_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("refund_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::RefundReasons::New::Component < SolidusAdmin::BaseComponent
def initialize(page:, refund_reason:)
@page = page
@refund_reason = refund_reason
end

def form_id
dom_id(@refund_reason, "#{stimulus_id}_new_refund_reason_form")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
title: "New Refund Reason"
cancel: "Cancel"
submit: "Add Refund Reason"
hints:
active: "When checked, this refund reason will be available for selection when refunding orders."
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,50 @@ class RefundReasonsController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

def index
refund_reasons = apply_search_to(
Spree::RefundReason.unscoped.order(id: :desc),
param: :q,
)

set_page_and_extract_portion_from(refund_reasons)
set_index_page

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

def new
@refund_reason = Spree::RefundReason.new

set_index_page

respond_to do |format|
format.html { render component('refund_reasons/new').new(page: @page, refund_reason: @refund_reason) }
end
end

def create
@refund_reason = Spree::RefundReason.new(refund_reason_params)

if @refund_reason.save
respond_to do |format|
flash[:notice] = t('.success')

format.html do
redirect_to solidus_admin.refund_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('refund_reasons/new').new(page: @page, refund_reason: @refund_reason)
render page_component, status: :unprocessable_entity
end
end
end
end

def destroy
@refund_reason = Spree::RefundReason.find_by!(id: params[:id])

Expand All @@ -34,7 +66,16 @@ def load_refund_reason
end

def refund_reason_params
params.require(:refund_reason).permit(:refund_reason_id, permitted_refund_reason_attributes)
params.require(:refund_reason).permit(:name, :code, :active)
end

def set_index_page
refund_reasons = apply_search_to(
Spree::RefundReason.unscoped.order(id: :desc),
param: :q,
)

set_page_and_extract_portion_from(refund_reasons)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ en:
title: "Refund Reasons"
destroy:
success: "Refund Reasons were successfully removed."
create:
success: "Refund 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 @@ -60,7 +60,7 @@
admin_resources :stock_locations, only: [:index, :destroy]
admin_resources :stores, only: [:index, :destroy]
admin_resources :zones, only: [:index, :destroy]
admin_resources :refund_reasons, only: [:index, :destroy]
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]
Expand Down
39 changes: 39 additions & 0 deletions admin/spec/features/refund_reasons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,43 @@
expect(Spree::RefundReason.count).to eq(0)
expect(page).to be_axe_clean
end

context "when creating a new refund reason" do
let(:query) { "?page=1&q%5Bname_or_description_cont%5D=Ret" }

before do
visit "/admin/refund_reasons/#{query}"
click_on "Add new"
expect(page).to have_content("New Refund 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 refund reason, keeping page and q params" do
fill_in "Name", with: "Return process"

click_on "Add Refund Reason"

expect(page).to have_content("Refund reason was successfully created.")
expect(Spree::RefundReason.find_by(name: "Return process")).to be_present
expect(page.current_url).to include(query)
end
end

context "with invalid data" do
it "fails to create a new refund reason, keeping page and q params" do
click_on "Add Refund Reason"

expect(page).to have_content "can't be blank"
expect(page.current_url).to include(query)
end
end
end
end
Loading