-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add Order#use_shipping and address management to the admin dashboard
#5461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rainerdema
merged 8 commits into
solidusio:main
from
nebulab:rainerd/admin/order/address-form
Nov 8, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8dcd0ba
Implement `use_shipping` to sync addresses in Order
rainerdema 619250b
Implement routes for address creation and update
rainerdema 704763a
Incorporate address component in new `orders/show/address` component
rainerdema c8bc9ba
Adjust field styles in `ui/forms/address` component
rainerdema b58646b
Implement server-side state preloading for address form
rainerdema 2dd113b
Disable state field for countries without states
rainerdema ae29975
Remove max height for modal content to accommodate larger forms
rainerdema 3a5daf8
Fix turbo cache event handling for details dropdown
rainerdema File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
admin/app/components/solidus_admin/orders/show/address/component.html.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <div class="<%= stimulus_id %>"> | ||
| <%= render component("orders/show").new(order: @order) %> | ||
| <%= render component("ui/modal").new(title: t(".title.#{@type}"), close_path: solidus_admin.order_path(@order)) do |modal| %> | ||
| <%= form_for @order, url: solidus_admin.send("order_#{@type}_address_path", @order), html: { id: form_id } do |form| %> | ||
| <div class="w-full flex flex-col mb-4"> | ||
| <h2 class="text-sm mb-4 font-semibold"><%= t(".subtitle.#{@type}") %></h2> | ||
| <div class="w-full flex gap-4"> | ||
| <%= form.fields_for :"#{@type}_address" do |address_form| %> | ||
| <%= render component('ui/forms/address').new(form: address_form, disabled: false) %> | ||
| <% end %> | ||
| </div> | ||
|
|
||
| <label class="flex gap-2 items-center"> | ||
| <%= form.hidden_field use_attribute, value: '0', id: false %> | ||
|
|
||
| <%= render component("ui/forms/checkbox").new( | ||
| name: "#{form.object_name}[#{use_attribute}]", | ||
| checked: form.object.send("#{@type}_address").new_record? || form.object.bill_address == form.object.ship_address, | ||
| value: '1' | ||
| ) %> | ||
|
|
||
| <span class="body-text-sm"> | ||
| <%= t(".use_this_address.#{@type}") %> | ||
| </span> | ||
| </label> | ||
| </div> | ||
| <% end %> | ||
|
|
||
| <% modal.with_actions do %> | ||
| <%= render component("ui/button").new( | ||
| tag: :a, | ||
| scheme: :secondary, | ||
| text: t(".cancel"), | ||
| href: solidus_admin.order_path(@order) | ||
| ) %> | ||
|
|
||
| <%= render component("ui/button").new( | ||
| tag: :button, | ||
| text: t(".save"), | ||
| form: form_id | ||
| ) %> | ||
| <% end %> | ||
| <% end %> | ||
| </div> |
29 changes: 29 additions & 0 deletions
29
admin/app/components/solidus_admin/orders/show/address/component.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class SolidusAdmin::Orders::Show::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 use_attribute | ||
| case @type | ||
| when 'ship' | ||
| 'use_shipping' | ||
| when 'bill' | ||
| 'use_billing' | ||
| end | ||
| end | ||
|
|
||
| def validate_address_type(type) | ||
| VALID_TYPES.include?(type) ? type : raise(ArgumentError, "Invalid address type: #{type}") | ||
| end | ||
| end |
15 changes: 15 additions & 0 deletions
15
admin/app/components/solidus_admin/orders/show/address/component.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # 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: | ||
| ship: Edit Shipping Address | ||
| bill: Edit Billing Address | ||
| subtitle: | ||
| ship: Shipping Address | ||
| bill: Billing Address | ||
| use_this_address: | ||
| ship: Use this address also for Billing | ||
| bill: Use this address also for Shipping |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
admin/app/controllers/solidus_admin/addresses_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SolidusAdmin | ||
| class AddressesController < BaseController | ||
| include Spree::Core::ControllerHelpers::StrongParameters | ||
|
|
||
| before_action :load_order | ||
| before_action :validate_address_type | ||
|
|
||
| def new | ||
| address = @order.send("#{address_type}_address") | ||
| @order.send("build_#{address_type}_address", country_id: default_country_id) if address.nil? | ||
| address ||= @order.send("#{address_type}_address") | ||
| address.country_id ||= default_country_id if address.country.nil? | ||
|
|
||
| respond_to do |format| | ||
| format.html { render component('orders/show/address').new(order: @order, type: address_type) } | ||
| end | ||
| end | ||
|
|
||
| def update | ||
| if @order.contents.update_cart(order_params) | ||
| redirect_to order_path(@order), status: :see_other, notice: t('.success') | ||
| else | ||
| flash.now[:error] = @order.errors[:base].join(", ") if @order.errors[:base].any? | ||
|
|
||
| respond_to do |format| | ||
| format.html { render component('orders/show/address').new(order: @order, type: address_type), status: :unprocessable_entity } | ||
| end | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def address_type | ||
| params[:type].presence_in(%w[bill ship]) | ||
| end | ||
|
|
||
| def validate_address_type | ||
| unless address_type | ||
| flash[:error] = t('.errors.address_type_invalid') | ||
| redirect_to spree.admin_order_url(@order) | ||
| end | ||
| end | ||
|
|
||
| def default_country_id | ||
| @default_country_id ||= begin | ||
| country = Spree::Country.default | ||
| country.id if Spree::Country.available.exists?(id: country.id) | ||
| end | ||
elia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| def load_order | ||
| @order = Spree::Order.find_by!(number: params[:order_id]) | ||
| authorize! action_name, @order | ||
| end | ||
|
|
||
| def order_params | ||
| params.require(:order).permit( | ||
| :use_billing, | ||
| :use_shipping, | ||
| bill_address_attributes: permitted_address_attributes, | ||
| ship_address_attributes: permitted_address_attributes | ||
| ) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
admin/spec/components/previews/solidus_admin/orders/show/address/component_preview.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # @component "orders/show/address" | ||
| class SolidusAdmin::Orders::Show::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 current_component.new(order: order, type: type) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def fake_order(type) | ||
| order = Spree::Order.new | ||
| country = Spree::Country.find_or_initialize_by(iso: Spree::Config.default_country_iso) | ||
|
|
||
| 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", { country: country }) | ||
| order | ||
| end | ||
|
|
||
| def type_options | ||
| current_component::VALID_TYPES | ||
| end | ||
| end |
1 change: 1 addition & 0 deletions
1
...components/previews/solidus_admin/orders/show/address/component_preview/overview.html.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <%= render current_component.new(order: order, type: type) %> |
9 changes: 9 additions & 0 deletions
9
admin/spec/components/solidus_admin/orders/show/address/component_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| RSpec.describe SolidusAdmin::Orders::Show::Address::Component, type: :component do | ||
| it "renders the overview preview" do | ||
| render_preview(:overview) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.