Skip to content

Commit

Permalink
Merge pull request #5819 from MadelineCollier/admin-update-refund-reason
Browse files Browse the repository at this point in the history
[Admin] Refund Reasons edit/update
  • Loading branch information
MadelineCollier authored Aug 12, 2024
2 parents 0db8839 + 72cf978 commit 8da6c2a
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= turbo_frame_tag :edit_refund_reason_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @refund_reason, url: solidus_admin.refund_reason_path(@refund_reason), 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">
<%= hidden_field_tag "#{f.object_name}[active]", "0" %>
<%= 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::RefundReason.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::Edit::Component < SolidusAdmin::BaseComponent
def initialize(page:, refund_reason:)
@page = page
@refund_reason = refund_reason
end

def form_id
dom_id(@refund_reason, "#{stimulus_id}_edit_refund_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: "Edit Refund Reason"
cancel: "Cancel"
submit: "Update Refund 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 @@ -14,11 +14,14 @@ def search_key
end

def row_url(refund_reason)
spree.edit_admin_refund_reason_path(refund_reason)
spree.edit_admin_refund_reason_path(refund_reason, _turbo_frame: :edit_refund_reason_modal)
end

def turbo_frames
%w[new_refund_reason_modal]
%w[
new_refund_reason_modal
edit_refund_reason_modal
]
end

def page_actions
Expand Down
39 changes: 39 additions & 0 deletions admin/app/controllers/solidus_admin/refund_reasons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module SolidusAdmin
class RefundReasonsController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

before_action :find_refund_reason, only: %i[edit update]

def index
set_index_page

Expand Down Expand Up @@ -49,6 +51,39 @@ def create
end
end

def edit
set_index_page

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

def update
if @refund_reason.update(refund_reason_params)
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/edit').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 @@ -65,6 +100,10 @@ def load_refund_reason
authorize! action_name, @refund_reason
end

def find_refund_reason
@refund_reason = Spree::RefundReason.find(params[:id])
end

def refund_reason_params
params.require(:refund_reason).permit(:name, :code, :active)
end
Expand Down
2 changes: 2 additions & 0 deletions admin/config/locales/refund_reasons.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ en:
success: "Refund Reasons were successfully removed."
create:
success: "Refund reason was successfully created."
update:
success: "Refund reason was successfully updated."
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, :new, :create, :destroy]
admin_resources :refund_reasons, except: [:show]
admin_resources :reimbursement_types, only: [:index]
admin_resources :return_reasons, only: [:index, :destroy]
admin_resources :adjustment_reasons, except: [:show]
Expand Down
30 changes: 30 additions & 0 deletions admin/spec/features/refund_reasons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,34 @@
end
end
end

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

before do
Spree::RefundReason.create(name: "Return process")
visit "/admin/refund_reasons#{query}"
find_row("Return process").click
expect(page).to have_content("Edit 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

it "successfully updates the existing refund reason" do
fill_in "Name", with: "Customer complaint"

click_on "Update Refund Reason"
expect(page).to have_content("Refund reason was successfully updated.")
expect(page).to have_content("Customer complaint")
expect(page).not_to have_content("Return process")
expect(Spree::RefundReason.find_by(name: "Customer complaint")).to be_present
expect(page.current_url).to include(query)
end
end
end

0 comments on commit 8da6c2a

Please sign in to comment.