From d662498260f76b6a592719cf3324874354888ac9 Mon Sep 17 00:00:00 2001 From: Chris Todorov Date: Thu, 2 May 2024 11:17:39 -0700 Subject: [PATCH 1/2] Add new UI component for creating return reasons This implements the new UI for creating a return reason. Co-authored-by: Kendra Riga --- .../return_reasons/index/component.rb | 10 +++++++ .../return_reasons/new/component.html.erb | 26 +++++++++++++++++ .../return_reasons/new/component.rb | 12 ++++++++ .../return_reasons/new/component.yml | 6 ++++ .../return_reasons_controller.rb | 29 +++++++++++++++---- admin/config/routes.rb | 2 +- admin/spec/features/return_reasons_spec.rb | 16 ++++++++++ 7 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 admin/app/components/solidus_admin/return_reasons/new/component.html.erb create mode 100644 admin/app/components/solidus_admin/return_reasons/new/component.rb create mode 100644 admin/app/components/solidus_admin/return_reasons/new/component.yml diff --git a/admin/app/components/solidus_admin/return_reasons/index/component.rb b/admin/app/components/solidus_admin/return_reasons/index/component.rb index 75ba35f2b95..b11d9db6311 100644 --- a/admin/app/components/solidus_admin/return_reasons/index/component.rb +++ b/admin/app/components/solidus_admin/return_reasons/index/component.rb @@ -17,6 +17,16 @@ def row_url(return_reason) spree.edit_admin_return_reason_path(return_reason) end + def page_actions + render component("ui/button").new( + tag: :a, + text: t('.add'), + href: solidus_admin.new_return_reason_path, + icon: "add-line", + class: "align-self-end w-full", + ) + end + def batch_actions [ { diff --git a/admin/app/components/solidus_admin/return_reasons/new/component.html.erb b/admin/app/components/solidus_admin/return_reasons/new/component.html.erb new file mode 100644 index 00000000000..4d8b3ae6412 --- /dev/null +++ b/admin/app/components/solidus_admin/return_reasons/new/component.html.erb @@ -0,0 +1,26 @@ +<%= turbo_frame_tag :new_return_reason_modal do %> + <%= render component("ui/modal").new(title: t(".title")) do |modal| %> + <%= form_for @return_reason, url: solidus_admin.return_reasons_path, html: { id: form_id } do |f| %> +
+ <%= render component("ui/forms/field").text_field(f, :name, class: "required") %> + +
+ <% modal.with_actions do %> +
+ <%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %> +
+ <%= 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) %> diff --git a/admin/app/components/solidus_admin/return_reasons/new/component.rb b/admin/app/components/solidus_admin/return_reasons/new/component.rb new file mode 100644 index 00000000000..7efe494f634 --- /dev/null +++ b/admin/app/components/solidus_admin/return_reasons/new/component.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class SolidusAdmin::ReturnReasons::New::Component < SolidusAdmin::BaseComponent + def initialize(page:, return_reason:) + @page = page + @return_reason = return_reason + end + + def form_id + dom_id(@return_reason, "#{stimulus_id}_new_return_reason_form") + end +end diff --git a/admin/app/components/solidus_admin/return_reasons/new/component.yml b/admin/app/components/solidus_admin/return_reasons/new/component.yml new file mode 100644 index 00000000000..bc0ad4a8aae --- /dev/null +++ b/admin/app/components/solidus_admin/return_reasons/new/component.yml @@ -0,0 +1,6 @@ +en: + title: "New Return Reason" + cancel: "Cancel" + submit: "Add Return Reason" + hints: + active: "When checked, this retun reason will be available for selection when creating a customer return for an order." diff --git a/admin/app/controllers/solidus_admin/return_reasons_controller.rb b/admin/app/controllers/solidus_admin/return_reasons_controller.rb index ad3a7883218..a1d6408bd00 100644 --- a/admin/app/controllers/solidus_admin/return_reasons_controller.rb +++ b/admin/app/controllers/solidus_admin/return_reasons_controller.rb @@ -5,18 +5,26 @@ class ReturnReasonsController < SolidusAdmin::BaseController include SolidusAdmin::ControllerHelpers::Search def index - return_reasons = apply_search_to( - Spree::ReturnReason.unscoped.order(id: :desc), - param: :q, - ) - - set_page_and_extract_portion_from(return_reasons) + set_index_page respond_to do |format| format.html { render component('return_reasons/index').new(page: @page) } end end + def new + @return_reason = Spree::ReturnReason.new + + set_index_page + + respond_to do |format| + format.html { + render component('return_reasons/new') + .new(page: @page, return_reason: @return_reason) + } + end + end + def destroy @return_reason = Spree::ReturnReason.find_by!(id: params[:id]) @@ -28,6 +36,15 @@ def destroy private + def set_index_page + return_reasons = apply_search_to( + Spree::ReturnReason.unscoped.order(id: :desc), + param: :q, + ) + + set_page_and_extract_portion_from(return_reasons) + end + def load_return_reason @return_reason = Spree::ReturnReason.find_by!(id: params[:id]) authorize! action_name, @return_reason diff --git a/admin/config/routes.rb b/admin/config/routes.rb index d4545ac603d..4a01e20aaa0 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -62,7 +62,7 @@ admin_resources :zones, 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 :return_reasons, only: [:index, :new, :destroy] admin_resources :adjustment_reasons, only: [:index, :destroy] admin_resources :store_credit_reasons, only: [:index, :destroy] end diff --git a/admin/spec/features/return_reasons_spec.rb b/admin/spec/features/return_reasons_spec.rb index 3e929cd1dd4..da9de56b609 100644 --- a/admin/spec/features/return_reasons_spec.rb +++ b/admin/spec/features/return_reasons_spec.rb @@ -19,4 +19,20 @@ expect(Spree::ReturnReason.count).to eq(0) expect(page).to be_axe_clean end + + it "allows an admin to create a new RMA reason" do + visit "/admin/return_reasons" + + expect(page).to have_content("Add new") + click_on "Add new" + + expect(page).to have_content("New Return Reason") + fill_in "Name", with: "Size too small" + + expect(page).to have_css("input[type=checkbox][name='return_reason[active]'][checked='checked']") + + click_on "Add Return Reason" + + expect(page).to have_content("Return reason has been successfully created!") + end end From 9bbdc09178fb9c3aa4df01b9ac2fb750613e8350 Mon Sep 17 00:00:00 2001 From: Chris Todorov Date: Wed, 10 Jul 2024 13:57:15 -0700 Subject: [PATCH 2/2] Added create action for return reasons admin This completes the new UI for creating return reasons by adding the create action and wiring the turbo modal for it. Co-authored-by: Andrew Stewart --- .../return_reasons/index/component.rb | 5 +++ .../return_reasons_controller.rb | 31 ++++++++++++++++++- admin/config/locales/return_reasons.en.yml | 2 ++ admin/config/routes.rb | 2 +- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/admin/app/components/solidus_admin/return_reasons/index/component.rb b/admin/app/components/solidus_admin/return_reasons/index/component.rb index b11d9db6311..fc645306c84 100644 --- a/admin/app/components/solidus_admin/return_reasons/index/component.rb +++ b/admin/app/components/solidus_admin/return_reasons/index/component.rb @@ -17,11 +17,16 @@ def row_url(return_reason) spree.edit_admin_return_reason_path(return_reason) end + def turbo_frames + %w[new_return_reason_modal] + end + def page_actions render component("ui/button").new( tag: :a, text: t('.add'), href: solidus_admin.new_return_reason_path, + data: { turbo_frame: :new_return_reason_modal }, icon: "add-line", class: "align-self-end w-full", ) diff --git a/admin/app/controllers/solidus_admin/return_reasons_controller.rb b/admin/app/controllers/solidus_admin/return_reasons_controller.rb index a1d6408bd00..5298dca24c6 100644 --- a/admin/app/controllers/solidus_admin/return_reasons_controller.rb +++ b/admin/app/controllers/solidus_admin/return_reasons_controller.rb @@ -25,6 +25,35 @@ def new end end + def create + @return_reason = Spree::ReturnReason.new(return_reason_params) + + if @return_reason.save + 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: '' + end + end + else + set_index_page + + respond_to do |format| + format.html do + page_component = component('return_reasons/new') + .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]) @@ -51,7 +80,7 @@ def load_return_reason end def return_reason_params - params.require(:return_reason).permit(:return_reason_id, permitted_return_reason_attributes) + params.require(:return_reason).permit(:name, :active) end end end diff --git a/admin/config/locales/return_reasons.en.yml b/admin/config/locales/return_reasons.en.yml index 248dc9769d7..f50843b6590 100644 --- a/admin/config/locales/return_reasons.en.yml +++ b/admin/config/locales/return_reasons.en.yml @@ -2,5 +2,7 @@ en: solidus_admin: return_reasons: title: "Return Reasons" + create: + success: "Return reason has been successfully created!" destroy: success: "Return Reasons were successfully removed." diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 4a01e20aaa0..6bf26d2d8d2 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -62,7 +62,7 @@ admin_resources :zones, only: [:index, :destroy] admin_resources :refund_reasons, only: [:index, :new, :create, :destroy] admin_resources :reimbursement_types, only: [:index] - admin_resources :return_reasons, only: [:index, :new, :destroy] + admin_resources :return_reasons, only: [:index, :new, :create, :destroy] admin_resources :adjustment_reasons, only: [:index, :destroy] admin_resources :store_credit_reasons, only: [:index, :destroy] end