Skip to content

Commit

Permalink
Edit/Update return reasons via new admin UI
Browse files Browse the repository at this point in the history
  • Loading branch information
MadelineCollier committed Aug 16, 2024
1 parent 00869ec commit 10eb257
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%= turbo_frame_tag :edit_return_reason_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @return_reason, url: solidus_admin.return_reason_path(@return_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") %>
<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::ReturnReason.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("return_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::ReturnReasons::Edit::Component < SolidusAdmin::BaseComponent
def initialize(page:, return_reason:)
@page = page
@return_reason = return_reason
end

def form_id
dom_id(@return_reason, "#{stimulus_id}_edit_return_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 Return Reason"
cancel: "Cancel"
submit: "Update Return Reason"
hints:
active: "When checked, this return reason will be available for selection when returning orders."
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ def search_key
end

def row_url(return_reason)
spree.edit_admin_return_reason_path(return_reason)
spree.edit_admin_return_reason_path(return_reason, _turbo_frame: :edit_return_reason_modal)
end

def turbo_frames
%w[
new_return_reason_modal
edit_return_reason_modal
]
end

Expand Down
34 changes: 34 additions & 0 deletions admin/app/controllers/solidus_admin/return_reasons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SolidusAdmin
class ReturnReasonsController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

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

def index
set_index_page
Expand Down Expand Up @@ -50,6 +51,39 @@ def create
end
end

def edit
set_index_page

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

def update
if @return_reason.update(return_reason_params)
respond_to do |format|
flash[:notice] = t('.success')

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

def destroy
@return_reason = Spree::ReturnReason.find_by!(id: params[:id])

Expand Down
2 changes: 2 additions & 0 deletions admin/config/locales/return_reasons.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ en:
success: "Return reasons were successfully removed."
create:
success: "Return reason was successfully created."
update:
success: "Return 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 @@ -62,7 +62,7 @@
admin_resources :zones, only: [:index, :destroy]
admin_resources :refund_reasons, except: [:show]
admin_resources :reimbursement_types, only: [:index]
admin_resources :return_reasons, only: [:index, :destroy, :new, :create]
admin_resources :return_reasons, except: [:show]
admin_resources :adjustment_reasons, except: [:show]
admin_resources :store_credit_reasons, except: [:show]
end
32 changes: 32 additions & 0 deletions admin/spec/features/return_reasons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,36 @@
end
end
end

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

before do
Spree::ReturnReason.create(name: "Good Reason")
visit "/admin/return_reasons#{query}"
find_row("Good Reason").click
expect(page).to have_content("Edit Return 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 return reason" do
fill_in "Name", with: "Better Reason"
page.uncheck "return_reason[active]"

click_on "Update Return Reason"
expect(page).to have_content("Return reason was successfully updated.")
expect(page).to have_content("Better Reason")
expect(page).not_to have_content("Good Reason")
expect(Spree::ReturnReason.find_by(name: "Better Reason")).to be_present
expect(Spree::ReturnReason.find_by(name: "Better Reason").active).to be_falsey
expect(page.current_url).to include(query)
end
end
end
48 changes: 48 additions & 0 deletions admin/spec/requests/solidus_admin/return_reasons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,54 @@
end
end

describe "GET /edit" do
it "renders the edit template with a 200 OK status" do
get solidus_admin.edit_return_reason_path(return_reason)
expect(response).to have_http_status(:ok)
end
end

describe "PATCH /update" do
context "with valid parameters" do
let(:valid_attributes) { { name: "Updated Return Reason", active: false } }

it "updates the return reason" do
patch solidus_admin.return_reason_path(return_reason), params: { return_reason: valid_attributes }
return_reason.reload
expect(return_reason.name).to eq("Updated Return Reason")
expect(return_reason.active).to be(false)
end

it "redirects to the index page with a 303 See Other status" do
patch solidus_admin.return_reason_path(return_reason), params: { return_reason: valid_attributes }
expect(response).to redirect_to(solidus_admin.return_reasons_path)
expect(response).to have_http_status(:see_other)
end

it "displays a success flash message" do
patch solidus_admin.return_reason_path(return_reason), params: { return_reason: valid_attributes }
follow_redirect!
expect(response.body).to include("Return reason was successfully updated.")
end
end

context "with invalid parameters" do
let(:invalid_attributes) { { name: "", active: false } }

it "does not update the return reason" do
original_name = return_reason.name
patch solidus_admin.return_reason_path(return_reason), params: { return_reason: invalid_attributes }
return_reason.reload
expect(return_reason.name).to eq(original_name)
end

it "renders the edit template with unprocessable_entity status" do
patch solidus_admin.return_reason_path(return_reason), params: { return_reason: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

describe "POST /create" do
context "with valid parameters" do
let(:valid_attributes) { { name: "Damaged item", active: true } }
Expand Down

0 comments on commit 10eb257

Please sign in to comment.