From 1677f2498404bfb54dc30d477cd9cf69ca1c7c8e Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Fri, 3 Nov 2023 16:00:29 +0100 Subject: [PATCH] Allow editing the contact email in orders admin section --- .../orders/show/component.html.erb | 4 ++-- .../orders/show/email/component.html.erb | 18 ++++++++++++++++ .../orders/show/email/component.rb | 15 +++++++++++++ .../orders/show/email/component.yml | 8 +++++++ .../solidus_admin/customers_controller.rb | 19 +++++++++++++++++ .../solidus_admin/orders_controller.rb | 21 +++++++++++++++++++ admin/config/locales/orders.en.yml | 2 ++ admin/config/routes.rb | 3 ++- admin/spec/features/order_spec.rb | 20 ++++++++++++++++-- 9 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 admin/app/components/solidus_admin/orders/show/email/component.html.erb create mode 100644 admin/app/components/solidus_admin/orders/show/email/component.rb create mode 100644 admin/app/components/solidus_admin/orders/show/email/component.yml create mode 100644 admin/app/controllers/solidus_admin/customers_controller.rb diff --git a/admin/app/components/solidus_admin/orders/show/component.html.erb b/admin/app/components/solidus_admin/orders/show/component.html.erb index 83578b71a2f..cfd54df8481 100644 --- a/admin/app/components/solidus_admin/orders/show/component.html.erb +++ b/admin/app/components/solidus_admin/orders/show/component.html.erb @@ -15,10 +15,10 @@ <%= page_with_sidebar_aside do %> <%= render component('ui/panel').new(title: panel_title_with_more_links(t(".customer"), [ - link_to(t(".edit_email"), "#", class: "p-2 hover:bg-gray-25 rounded-sm text-black"), + link_to(t(".edit_email"), solidus_admin.order_customer_path(@order), class: "p-2 hover:bg-gray-25 rounded-sm text-black"), link_to(t(".edit_shipping"), "#", class: "p-2 hover:bg-gray-25 rounded-sm text-black"), link_to(t(".edit_billing"), "#", class: "p-2 hover:bg-gray-25 rounded-sm text-black"), - link_to(t(".remove_customer"), "#", 'data-turbo-method': :delete, class: "p-2 hover:bg-gray-25 rounded-sm text-red-500"), + link_to(t(".remove_customer"), solidus_admin.order_customer_path(@order), 'data-turbo-method': :delete, class: "p-2 hover:bg-gray-25 rounded-sm text-red-500"), ])) do %>
<%# CUSTOMER %> diff --git a/admin/app/components/solidus_admin/orders/show/email/component.html.erb b/admin/app/components/solidus_admin/orders/show/email/component.html.erb new file mode 100644 index 00000000000..5ade14b6bc4 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/email/component.html.erb @@ -0,0 +1,18 @@ +
+ <%= render component("orders/show").new(order: @order) %> + <%= render component("ui/modal").new(title: t(".title"), close_path: close_path) do |modal| %> + <%= form_for @order, url: close_path, html: { id: form_id} do |f| %> + <%= render component("ui/forms/field").text_field(f, :email) %> + + <% end %> + + <% modal.with_actions do %> + <%= render component("ui/button").new(tag: :a, scheme: :secondary, href: close_path, type: :submit, text: t('.cancel')) %> + <%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %> + <% end %> + <% end %> +
diff --git a/admin/app/components/solidus_admin/orders/show/email/component.rb b/admin/app/components/solidus_admin/orders/show/email/component.rb new file mode 100644 index 00000000000..02dc30f313c --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/email/component.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class SolidusAdmin::Orders::Show::Email::Component < SolidusAdmin::BaseComponent + def initialize(order:) + @order = order + end + + def form_id + dom_id(@order, "#{stimulus_id}_email_form") + end + + def close_path + @close_path ||= solidus_admin.order_path(@order) + end +end diff --git a/admin/app/components/solidus_admin/orders/show/email/component.yml b/admin/app/components/solidus_admin/orders/show/email/component.yml new file mode 100644 index 00000000000..eccfb756719 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/email/component.yml @@ -0,0 +1,8 @@ +en: + title: "Edit order email" + submit: "Save" + cancel: "Cancel" + guest_checkout: "Guest checkout" + guest_checkout_tip: "An order without an associated user account is considered a guest checkout." + "yes": "Yes" + "no": "No" diff --git a/admin/app/controllers/solidus_admin/customers_controller.rb b/admin/app/controllers/solidus_admin/customers_controller.rb new file mode 100644 index 00000000000..9b7fbd5af09 --- /dev/null +++ b/admin/app/controllers/solidus_admin/customers_controller.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class SolidusAdmin::CustomersController < SolidusAdmin::BaseController + before_action :load_order, only: :show + + def show + render component('orders/show/email').new(order: @order) + end + + private + + def load_order + @order = Spree::Order.find_by!(number: params[:order_id]) + end + + def authorization_subject + @order || Spree::Order + end +end diff --git a/admin/app/controllers/solidus_admin/orders_controller.rb b/admin/app/controllers/solidus_admin/orders_controller.rb index c4150222572..71044becabc 100644 --- a/admin/app/controllers/solidus_admin/orders_controller.rb +++ b/admin/app/controllers/solidus_admin/orders_controller.rb @@ -2,6 +2,8 @@ module SolidusAdmin class OrdersController < SolidusAdmin::BaseController + include Spree::Core::ControllerHelpers::StrongParameters + def index orders = Spree::Order .order(created_at: :desc, id: :desc) @@ -26,6 +28,21 @@ def show end end + def update + load_order + + @order.assign_attributes(order_params) + @order.email ||= @order.user.email if @order.user && @order.user.changed? + + if @order.save + flash[:notice] = t('.success') + else + flash[:error] = t('.error') + end + + redirect_to spree.edit_admin_order_path(@order) + end + def edit redirect_to action: :show end @@ -63,5 +80,9 @@ def load_order @order = Spree::Order.find_by!(number: params[:id]) authorize! action_name, @order end + + def order_params + params.require(:order).permit(:user_id, permitted_order_attributes) + end end end diff --git a/admin/config/locales/orders.en.yml b/admin/config/locales/orders.en.yml index bae628e0ddf..09682ccbfc7 100644 --- a/admin/config/locales/orders.en.yml +++ b/admin/config/locales/orders.en.yml @@ -2,3 +2,5 @@ en: solidus_admin: orders: title: "Orders" + update: + success: "Order was updated successfully" diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 98db04921c4..e2364fbc9ca 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -18,8 +18,9 @@ get 'states', to: 'countries#states' end - resources :orders, only: [:index, :show, :edit] do + resources :orders, only: [:index, :show, :edit, :update] do resources :line_items, only: [:destroy, :create, :update] + resource :customer member do get :variants_for diff --git a/admin/spec/features/order_spec.rb b/admin/spec/features/order_spec.rb index 827f29a9ac9..c4243fa3e07 100644 --- a/admin/spec/features/order_spec.rb +++ b/admin/spec/features/order_spec.rb @@ -2,11 +2,27 @@ require 'spec_helper' -describe "Order", type: :feature do +describe "Order", :js, type: :feature do before { sign_in create(:admin_user, email: 'admin@example.com') } + it "allows changing the order email" do + create(:order, number: "R123456789", total: 19.99) + + visit "/admin/orders/R123456789/edit" + + expect(page).to have_content("Order R123456789") + find("summary", text: "Customer").click + click_on "Edit order email" + within("dialog") do + fill_in "Customer Email", with: "a@b.c" + click_on "Save" + end + expect(page).to have_content("Order was updated successfully") + expect(page).to have_content("Order contact email a@b.c", normalize_ws: true) + end + context "in cart state" do - it "allows managing the cart", :js do + it "allows managing the cart" do create(:product, name: "Just a product", slug: 'just-a-prod', price: 19.99) create(:product, name: "Just another product", slug: 'just-another-prod', price: 29.99) create(:order, number: "R123456789", total: 19.99, state: "cart")