Skip to content

Commit

Permalink
Set country and state
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerdema committed Oct 26, 2023
1 parent 7ba63f5 commit 052c89a
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="<%= stimulus_id %>" data-controller="<%= stimulus_id %>">
<%= render component('ui/panel').new(title: "Customer") do %>
<div class="border-b border-gray-100 w-full mb-0 pb-0">
<strong><%= @order.user.addresses.first.name %></strong><br/>
<strong><%= @order.user.ship_address.name %></strong><br/>
<%= @order.user.email %><br/>
<%= pluralize(@order.user.orders.count, 'order') %>
</div>
Expand Down
49 changes: 41 additions & 8 deletions admin/app/components/solidus_admin/orders/show/component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
<%= render component("ui/forms/field").text_field(f, :email) %>

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

<h2 class="text-xl mb-4 font-semibold"><%= t(".shipping_address") %></h2>
Expand All @@ -36,8 +37,24 @@
<%= render component("ui/forms/field").text_field(ba_form, :city, class: "flex-1") %>
<%= render component("ui/forms/field").text_field(ba_form, :zipcode, class: "flex-1") %>
</div>
<%= render component("ui/forms/field").select(ba_form, :country, Spree::Country.all.map { |c| [c.name, c.id] }) %>
<%= render component("ui/forms/field").select(ba_form, :state, Spree::State.all.map { |s| [s.name, s.id] }) %>

<%= render component("ui/forms/field").select(
ba_form,
:country_id,
Spree::Country.all.map { |c| [c.name, c.id] },
value: @order.ship_address.try(:country_id),
"data-#{stimulus_id}-target": "country",
"data-action": "change->#{stimulus_id}#loadStates"
) %>

<%= render component("ui/forms/field").select(
ba_form,
:state_id,
[],
value: @order.ship_address.try(:state_id),
"data-#{stimulus_id}-target": "state"
) %>

<%= render component("ui/forms/field").text_field(ba_form, :phone) %>
<% end %>
</div>
Expand All @@ -52,8 +69,24 @@
<%= render component("ui/forms/field").text_field(ba_form, :city, class: "flex-1") %>
<%= render component("ui/forms/field").text_field(ba_form, :zipcode, class: "flex-1") %>
</div>
<%= render component("ui/forms/field").select(ba_form, :country, Spree::Country.all.map { |c| [c.name, c.id] }) %>
<%= render component("ui/forms/field").select(ba_form, :state, Spree::State.all.map { |s| [s.name, s.id] }) %>

<%= render component("ui/forms/field").select(
ba_form,
:country_id,
Spree::Country.all.map { |c| [c.name, c.id] },
value: @order.bill_address.try(:country_id),
"data-#{stimulus_id}-target": "country",
"data-action": "change->#{stimulus_id}#loadStates"
) %>

<%= render component("ui/forms/field").select(
ba_form,
:state_id,
[],
value: @order.bill_address.try(:state_id),
"data-#{stimulus_id}-target": "state"
) %>

<%= render component("ui/forms/field").text_field(ba_form, :phone) %>
<% end %>
</div>
Expand All @@ -62,7 +95,7 @@
<% end %>

<%= page_with_sidebar_aside do %>
<%= render component("orders/customer").new(order: @order) %>
<%= render component("orders/customer").new(order: @order) unless @order.user.nil? %>
<% end %>
<% end %>

Expand Down
28 changes: 28 additions & 0 deletions admin/app/components/solidus_admin/orders/show/component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
static targets = ["country", "state"]

connect() {
this.loadStates()
}

loadStates() {
const countryId = this.countryTarget.value

fetch(`/admin/countries/${countryId}/states`)
.then(response => response.json())
.then(data => {
this.updateStateOptions(data)
})
}

updateStateOptions(data) {
const stateSelect = this.stateTarget

stateSelect.innerHTML = ""

data.forEach(state => {
const option = document.createElement("option")

option.value = state.id
option.innerText = state.name
stateSelect.appendChild(option)
})
}
}
3 changes: 2 additions & 1 deletion admin/app/components/solidus_admin/orders/show/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class SolidusAdmin::Orders::Show::Component < SolidusAdmin::BaseComponent
include SolidusAdmin::Layout::PageHelpers

def initialize(order:)
@order = Spree::Order.complete.last #order
#@order = Spree::Order.complete.last
@order = order
end

def form_id
Expand Down
12 changes: 12 additions & 0 deletions admin/app/controllers/solidus_admin/countries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module SolidusAdmin
class CountriesController < SolidusAdmin::BaseController
skip_before_action :authorize_solidus_admin_user!

def states
@states = Spree::State.where(country_id: params[:country_id])
render json: @states.select(:id, :name)
end
end
end
79 changes: 79 additions & 0 deletions admin/app/controllers/solidus_admin/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

module SolidusAdmin
class CustomersController < SolidusAdmin::BaseController
skip_before_action :authorize_solidus_admin_user!

before_action :load_order

def show
edit
end

def edit
country_id = default_country_id

@order.build_bill_address(country_id: country_id) if @order.bill_address.nil?
@order.build_ship_address(country_id: country_id) if @order.ship_address.nil?

@order.bill_address.country_id = country_id if @order.bill_address.country.nil?
@order.ship_address.country_id = country_id if @order.ship_address.country.nil?

respond_to do |format|
format.html { render component('orders/show').new(order: @order) }
end
end

def update
binding.irb
if @order.contents.update_cart(order_params)

if should_associate_user?
requested_user = Spree.user_class.find(params[:user_id])
@order.associate_user!(requested_user, @order.email.blank?)
end

unless @order.completed?
@order.contents.advance
@order.refresh_shipment_rates
end

flash[:success] = t('spree.customer_details_updated')
render action: :edit
end
end

private

def default_country_id
country_id = Spree::Country.default.id

country_id if Spree::Country.available.pluck(:id).include?(country_id)
end

def order_params
address_attributes = [
:id, :name, :address1, :address2, :city, :country_id, :state_id,
:zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company,
country: [:iso, :name, :iso3, :iso_name],
state: [:name, :abbr]
]

params.require(:order).permit(
:email,
:use_billing,
bill_address_attributes: address_attributes,
ship_address_attributes: address_attributes
)
end

def should_associate_user?
params[:guest_checkout] == "false" && params[:user_id] && params[:user_id].to_i != @order.user_id
end

def load_order
@order = Spree::Order.find_by!(number: params[:order_id])
authorize! action_name, @order
end
end
end
6 changes: 6 additions & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
put :activate
end
end

resources :countries, only: [] do
get 'states', to: 'countries#states'
end

resources :orders, only: [:index] do
resources :line_items, only: [:destroy, :create, :update]
resource :customer

member do
get :cart, to: "orders#show"
Expand Down

0 comments on commit 052c89a

Please sign in to comment.