From 4e7004e5bc9aa4344eb1b181b52bea48429a8be0 Mon Sep 17 00:00:00 2001 From: Rainer Dema Date: Fri, 3 Nov 2023 18:43:23 +0100 Subject: [PATCH] Incorporate `ui/forms/address` component in new `orders/address` component Leverage the existing `ui/forms/address` component to render the address forms within the admin order process. --- .../orders/address/component.html.erb | 70 +++++++++++++++++++ .../solidus_admin/orders/address/component.rb | 26 +++++++ .../orders/address/component.yml | 10 +++ .../solidus_admin/addresses_controller.rb | 2 + admin/config/locales/orders.en.yml | 1 + .../orders/address/component_preview.rb | 39 +++++++++++ .../component_preview/overview.html.erb | 1 + .../orders/address/component_spec.rb | 9 +++ 8 files changed, 158 insertions(+) create mode 100644 admin/app/components/solidus_admin/orders/address/component.html.erb create mode 100644 admin/app/components/solidus_admin/orders/address/component.rb create mode 100644 admin/app/components/solidus_admin/orders/address/component.yml create mode 100644 admin/spec/components/previews/solidus_admin/orders/address/component_preview.rb create mode 100644 admin/spec/components/previews/solidus_admin/orders/address/component_preview/overview.html.erb create mode 100644 admin/spec/components/solidus_admin/orders/address/component_spec.rb diff --git a/admin/app/components/solidus_admin/orders/address/component.html.erb b/admin/app/components/solidus_admin/orders/address/component.html.erb new file mode 100644 index 00000000000..6b81a0b8be7 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/address/component.html.erb @@ -0,0 +1,70 @@ +
+ <%= page do %> + <%= page_header do %> + <%= page_header_back(solidus_admin.edit_order_path(@order)) %> + <%= page_header_title(t(".title", address: t(".#{address_type_action}_address"))) %> + <%= page_header_actions do %> + <%= render component("ui/button").new( + tag: :a, + scheme: :secondary, + text: t(".cancel"), + href: solidus_admin.edit_order_path(@order) + ) %> + + <%= render component("ui/button").new( + tag: :button, + text: t(".save"), + form: form_id + ) %> + <% end %> + <% end %> + + <%= page_with_sidebar do %> + <%= page_with_sidebar_main do %> + <%= render component('ui/panel').new do %> + <%= form_for @order, url: solidus_admin.send("order_#{@type}_address_path", @order), html: { id: form_id } do |form| %> +
+

<%= t(".#{address_type_action}_address") %>

+
+ <%= form.fields_for :"#{@type}_address" do |address_form| %> + <%= render component('ui/forms/address').new(form: address_form, disabled: false) %> + <% end %> +
+ + +
+ <% end %> + <% end %> + <% end %> + <% end %> + + <%= page_footer do %> + <%= page_footer_actions do %> + <%= render component("ui/button").new( + tag: :a, + scheme: :secondary, + text: t(".cancel"), + href: solidus_admin.edit_order_path(@order) + ) %> + + <%= render component("ui/button").new( + tag: :button, + text: t(".save"), + form: form_id + ) %> + <% end %> + <% end %> + <% end %> +
diff --git a/admin/app/components/solidus_admin/orders/address/component.rb b/admin/app/components/solidus_admin/orders/address/component.rb new file mode 100644 index 00000000000..5f51d47e980 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/address/component.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class SolidusAdmin::Orders::Address::Component < SolidusAdmin::BaseComponent + include SolidusAdmin::Layout::PageHelpers + + VALID_TYPES = ['ship', 'bill'].freeze + + def initialize(order:, type: 'ship') + @order = order + @type = validate_address_type(type) + end + + def form_id + @form_id ||= "#{stimulus_id}--form-#{@type}-#{@order.id}" + end + + def address_type_action + (@type.end_with?('l') ? "#{@type}ing" : "#{@type}ping").to_s + end + + private + + def validate_address_type(type) + VALID_TYPES.include?(type) ? type : raise(ArgumentError, "Invalid address type: #{type}") + end +end diff --git a/admin/app/components/solidus_admin/orders/address/component.yml b/admin/app/components/solidus_admin/orders/address/component.yml new file mode 100644 index 00000000000..42c4ebe78cb --- /dev/null +++ b/admin/app/components/solidus_admin/orders/address/component.yml @@ -0,0 +1,10 @@ +# Add your component translations here. +# Use the translation in the example in your template with `t(".hello")`. +en: + save: Save + cancel: Cancel + back: Back + title: "Edit %{address}" + shipping_address: Shipping Address + billing_address: Billing Address + use_this_address: "Use this address also for %{type}" diff --git a/admin/app/controllers/solidus_admin/addresses_controller.rb b/admin/app/controllers/solidus_admin/addresses_controller.rb index dedc0849049..01446061cc8 100644 --- a/admin/app/controllers/solidus_admin/addresses_controller.rb +++ b/admin/app/controllers/solidus_admin/addresses_controller.rb @@ -14,6 +14,7 @@ def new address.country_id ||= default_country_id if address.country.nil? respond_to do |format| + format.html { render component('orders/address').new(order: @order, type: address_type) } end end @@ -25,6 +26,7 @@ def update flash.now[:error] = @order.errors.full_messages.join(", ") respond_to do |format| + format.html { render component('orders/address').new(order: @order, type: address_type), status: :unprocessable_entity } end end end diff --git a/admin/config/locales/orders.en.yml b/admin/config/locales/orders.en.yml index db0f93840d7..cd23845c05e 100644 --- a/admin/config/locales/orders.en.yml +++ b/admin/config/locales/orders.en.yml @@ -3,6 +3,7 @@ en: orders: title: "Orders" addresses: + title: "Addresses" address_type_invalid: "Invalid address type. Please select either billing or shipping address." update: success: "The address has been successfully updated." diff --git a/admin/spec/components/previews/solidus_admin/orders/address/component_preview.rb b/admin/spec/components/previews/solidus_admin/orders/address/component_preview.rb new file mode 100644 index 00000000000..8e77bae6b0b --- /dev/null +++ b/admin/spec/components/previews/solidus_admin/orders/address/component_preview.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +# @component "orders/address" +class SolidusAdmin::Orders::Address::ComponentPreview < ViewComponent::Preview + include SolidusAdmin::Preview + + def overview + type = "ship" + order = fake_order(type) + + render_with_template( + locals: { + order: order, + type: type + } + ) + end + + # @param type select :type_options + def playground(type: "ship") + order = fake_order(type) + render component("orders/address").new(order: order, type: type) + end + + private + + def fake_order(type) + order = Spree::Order.new + order.define_singleton_method(:id) { 1 } + order.define_singleton_method(:persisted?) { true } + order.define_singleton_method(:to_param) { id.to_s } + order.send("build_#{type}_address") + order + end + + def type_options + current_component::VALID_TYPES + end +end diff --git a/admin/spec/components/previews/solidus_admin/orders/address/component_preview/overview.html.erb b/admin/spec/components/previews/solidus_admin/orders/address/component_preview/overview.html.erb new file mode 100644 index 00000000000..1f2f4e68235 --- /dev/null +++ b/admin/spec/components/previews/solidus_admin/orders/address/component_preview/overview.html.erb @@ -0,0 +1 @@ +<%= render current_component.new(order: order, type: type) %> diff --git a/admin/spec/components/solidus_admin/orders/address/component_spec.rb b/admin/spec/components/solidus_admin/orders/address/component_spec.rb new file mode 100644 index 00000000000..31c61a726d2 --- /dev/null +++ b/admin/spec/components/solidus_admin/orders/address/component_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe SolidusAdmin::Orders::Address::Component, type: :component do + it "renders the overview preview" do + render_preview(:overview) + end +end