Skip to content

Commit 052c89a

Browse files
committed
Set country and state
1 parent 7ba63f5 commit 052c89a

File tree

7 files changed

+169
-10
lines changed

7 files changed

+169
-10
lines changed

admin/app/components/solidus_admin/orders/customer/component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="<%= stimulus_id %>" data-controller="<%= stimulus_id %>">
22
<%= render component('ui/panel').new(title: "Customer") do %>
33
<div class="border-b border-gray-100 w-full mb-0 pb-0">
4-
<strong><%= @order.user.addresses.first.name %></strong><br/>
4+
<strong><%= @order.user.ship_address.name %></strong><br/>
55
<%= @order.user.email %><br/>
66
<%= pluralize(@order.user.orders.count, 'order') %>
77
</div>

admin/app/components/solidus_admin/orders/show/component.html.erb

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
<%= render component("ui/forms/field").text_field(f, :email) %>
2020

2121
<div class="w-full flex items-center mb-4 border-b pb-4">
22-
<%= render component('ui/forms/checkbox').new(
22+
<%#= render component('ui/forms/checkbox').new(
23+
id: "#{form_id}--guest-checkout",
2324
name: "#{f.object_name}[guest_checkout]",
24-
checked: f.object.guest_token,
25+
checked: f.object.user_id.nil?,
2526
) %>
26-
<%= label_tag nil, 'Guest checkout', class: "ml-2 text-sm text-gray-700" %>
27+
<%= label_tag "#{form_id}--guest-checkout", 'Guest checkout', class: "ml-2 text-sm text-gray-700" %>
2728
</div>
2829

2930
<h2 class="text-xl mb-4 font-semibold"><%= t(".shipping_address") %></h2>
@@ -36,8 +37,24 @@
3637
<%= render component("ui/forms/field").text_field(ba_form, :city, class: "flex-1") %>
3738
<%= render component("ui/forms/field").text_field(ba_form, :zipcode, class: "flex-1") %>
3839
</div>
39-
<%= render component("ui/forms/field").select(ba_form, :country, Spree::Country.all.map { |c| [c.name, c.id] }) %>
40-
<%= render component("ui/forms/field").select(ba_form, :state, Spree::State.all.map { |s| [s.name, s.id] }) %>
40+
41+
<%= render component("ui/forms/field").select(
42+
ba_form,
43+
:country_id,
44+
Spree::Country.all.map { |c| [c.name, c.id] },
45+
value: @order.ship_address.try(:country_id),
46+
"data-#{stimulus_id}-target": "country",
47+
"data-action": "change->#{stimulus_id}#loadStates"
48+
) %>
49+
50+
<%= render component("ui/forms/field").select(
51+
ba_form,
52+
:state_id,
53+
[],
54+
value: @order.ship_address.try(:state_id),
55+
"data-#{stimulus_id}-target": "state"
56+
) %>
57+
4158
<%= render component("ui/forms/field").text_field(ba_form, :phone) %>
4259
<% end %>
4360
</div>
@@ -52,8 +69,24 @@
5269
<%= render component("ui/forms/field").text_field(ba_form, :city, class: "flex-1") %>
5370
<%= render component("ui/forms/field").text_field(ba_form, :zipcode, class: "flex-1") %>
5471
</div>
55-
<%= render component("ui/forms/field").select(ba_form, :country, Spree::Country.all.map { |c| [c.name, c.id] }) %>
56-
<%= render component("ui/forms/field").select(ba_form, :state, Spree::State.all.map { |s| [s.name, s.id] }) %>
72+
73+
<%= render component("ui/forms/field").select(
74+
ba_form,
75+
:country_id,
76+
Spree::Country.all.map { |c| [c.name, c.id] },
77+
value: @order.bill_address.try(:country_id),
78+
"data-#{stimulus_id}-target": "country",
79+
"data-action": "change->#{stimulus_id}#loadStates"
80+
) %>
81+
82+
<%= render component("ui/forms/field").select(
83+
ba_form,
84+
:state_id,
85+
[],
86+
value: @order.bill_address.try(:state_id),
87+
"data-#{stimulus_id}-target": "state"
88+
) %>
89+
5790
<%= render component("ui/forms/field").text_field(ba_form, :phone) %>
5891
<% end %>
5992
</div>
@@ -62,7 +95,7 @@
6295
<% end %>
6396

6497
<%= page_with_sidebar_aside do %>
65-
<%= render component("orders/customer").new(order: @order) %>
98+
<%= render component("orders/customer").new(order: @order) unless @order.user.nil? %>
6699
<% end %>
67100
<% end %>
68101

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
import { Controller } from '@hotwired/stimulus'
22

33
export default class extends Controller {
4+
static targets = ["country", "state"]
45

6+
connect() {
7+
this.loadStates()
8+
}
9+
10+
loadStates() {
11+
const countryId = this.countryTarget.value
12+
13+
fetch(`/admin/countries/${countryId}/states`)
14+
.then(response => response.json())
15+
.then(data => {
16+
this.updateStateOptions(data)
17+
})
18+
}
19+
20+
updateStateOptions(data) {
21+
const stateSelect = this.stateTarget
22+
23+
stateSelect.innerHTML = ""
24+
25+
data.forEach(state => {
26+
const option = document.createElement("option")
27+
28+
option.value = state.id
29+
option.innerText = state.name
30+
stateSelect.appendChild(option)
31+
})
32+
}
533
}

admin/app/components/solidus_admin/orders/show/component.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ class SolidusAdmin::Orders::Show::Component < SolidusAdmin::BaseComponent
44
include SolidusAdmin::Layout::PageHelpers
55

66
def initialize(order:)
7-
@order = Spree::Order.complete.last #order
7+
#@order = Spree::Order.complete.last
8+
@order = order
89
end
910

1011
def form_id
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module SolidusAdmin
4+
class CountriesController < SolidusAdmin::BaseController
5+
skip_before_action :authorize_solidus_admin_user!
6+
7+
def states
8+
@states = Spree::State.where(country_id: params[:country_id])
9+
render json: @states.select(:id, :name)
10+
end
11+
end
12+
end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# frozen_string_literal: true
2+
3+
module SolidusAdmin
4+
class CustomersController < SolidusAdmin::BaseController
5+
skip_before_action :authorize_solidus_admin_user!
6+
7+
before_action :load_order
8+
9+
def show
10+
edit
11+
end
12+
13+
def edit
14+
country_id = default_country_id
15+
16+
@order.build_bill_address(country_id: country_id) if @order.bill_address.nil?
17+
@order.build_ship_address(country_id: country_id) if @order.ship_address.nil?
18+
19+
@order.bill_address.country_id = country_id if @order.bill_address.country.nil?
20+
@order.ship_address.country_id = country_id if @order.ship_address.country.nil?
21+
22+
respond_to do |format|
23+
format.html { render component('orders/show').new(order: @order) }
24+
end
25+
end
26+
27+
def update
28+
binding.irb
29+
if @order.contents.update_cart(order_params)
30+
31+
if should_associate_user?
32+
requested_user = Spree.user_class.find(params[:user_id])
33+
@order.associate_user!(requested_user, @order.email.blank?)
34+
end
35+
36+
unless @order.completed?
37+
@order.contents.advance
38+
@order.refresh_shipment_rates
39+
end
40+
41+
flash[:success] = t('spree.customer_details_updated')
42+
render action: :edit
43+
end
44+
end
45+
46+
private
47+
48+
def default_country_id
49+
country_id = Spree::Country.default.id
50+
51+
country_id if Spree::Country.available.pluck(:id).include?(country_id)
52+
end
53+
54+
def order_params
55+
address_attributes = [
56+
:id, :name, :address1, :address2, :city, :country_id, :state_id,
57+
:zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company,
58+
country: [:iso, :name, :iso3, :iso_name],
59+
state: [:name, :abbr]
60+
]
61+
62+
params.require(:order).permit(
63+
:email,
64+
:use_billing,
65+
bill_address_attributes: address_attributes,
66+
ship_address_attributes: address_attributes
67+
)
68+
end
69+
70+
def should_associate_user?
71+
params[:guest_checkout] == "false" && params[:user_id] && params[:user_id].to_i != @order.user_id
72+
end
73+
74+
def load_order
75+
@order = Spree::Order.find_by!(number: params[:order_id])
76+
authorize! action_name, @order
77+
end
78+
end
79+
end

admin/config/routes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313
put :activate
1414
end
1515
end
16+
17+
resources :countries, only: [] do
18+
get 'states', to: 'countries#states'
19+
end
20+
1621
resources :orders, only: [:index] do
1722
resources :line_items, only: [:destroy, :create, :update]
23+
resource :customer
1824

1925
member do
2026
get :cart, to: "orders#show"

0 commit comments

Comments
 (0)