Improve countries and states loading

This commit is contained in:
Matt-Yorkley
2022-01-30 09:31:38 +00:00
parent bf4428b688
commit 370ec17818
3 changed files with 28 additions and 15 deletions

View File

@@ -14,7 +14,7 @@ module CheckoutCallbacks
prepend_before_action :require_distributor_chosen
before_action :load_order, :associate_user, :load_saved_addresses, :load_saved_credit_cards
before_action :load_shipping_methods, :load_countries, if: -> { params[:step] == "details" }
before_action :load_shipping_methods, if: -> { params[:step] == "details" }
before_action :ensure_order_not_completed
before_action :ensure_checkout_allowed
@@ -49,15 +49,6 @@ module CheckoutCallbacks
@shipping_methods = Spree::ShippingMethod.for_distributor(@order.distributor).order(:name)
end
def load_countries
@countries = available_countries.map { |c| [c.name, c.id] }
@countries_with_states = available_countries.map { |c|
[c.id, c.states.map { |s|
[s.name, s.id]
}]
}
end
def redirect_to_shop?
!@order ||
!@order.checkout_allowed? ||

View File

@@ -17,6 +17,24 @@ module Spree
end.sort { |a, b| a.name <=> b.name }
end
def countries
available_countries.map { |c| [c.name, c.id] }
end
def states_for_country(country)
country.states.map do |state|
[state.name, state.id]
end
end
def countries_with_states
available_countries.map { |c|
[c.id, c.states.map { |s|
[s.name, s.id]
}]
}
end
def pretty_time(time)
[I18n.l(time.to_date, format: :long),
time.strftime("%l:%M %p")].join(" ")

View File

@@ -25,7 +25,7 @@
= bill_address.text_field :phone, { placeholder: t("split_checkout.step1.your_details.phone.placeholder") }
= f.error_message_on "bill_address.phone"
%div.checkout-substep{ "data-controller": "dependant-select", "data-dependant-select-options-value": @countries_with_states }
%div.checkout-substep{ "data-controller": "dependant-select", "data-dependant-select-options-value": countries_with_states }
-# BILLING ADDRESS
%div.checkout-title
= t("split_checkout.step1.billing_address.title")
@@ -45,9 +45,11 @@
= bill_address.text_field :city, { placeholder: t("split_checkout.step1.address.city.placeholder") }
= f.error_message_on "bill_address.city"
- bill_address_country = @order.bill_address.country || DefaultCountry.country
%div.checkout-input
= bill_address.label :state_id, t("split_checkout.step1.address.state_id.label")
= bill_address.select :state_id, @countries_with_states, { }, { "data-dependant-select-target": "select" }
= bill_address.select :state_id, states_for_country(bill_address_country), { selected: @order.bill_address&.state_id }, { "data-dependant-select-target": "select" }
%div.checkout-input
= bill_address.label :zipcode, t("split_checkout.step1.address.zipcode.label")
@@ -56,7 +58,7 @@
%div.checkout-input
= bill_address.label :country_id, t("split_checkout.step1.address.country_id.label")
= bill_address.select :country_id, @countries, { selected: @order.bill_address.country_id || DefaultCountry.id }, {"data-dependant-select-target": "source", "data-action": "dependant-select#handleSelectChange"}
= bill_address.select :country_id, countries, { selected: bill_address_country.id }, { "data-dependant-select-target": "source", "data-action": "dependant-select#handleSelectChange" }
- if spree_current_user||true
%div.checkout-input
@@ -121,9 +123,11 @@
= ship_address.text_field :city, { placeholder: t("split_checkout.step1.address.city.placeholder") }
= f.error_message_on "ship_address.city"
- ship_address_country = @order.ship_address.country || DefaultCountry.country
%div.checkout-input
= ship_address.label :state_id, t("split_checkout.step1.address.state_id.label")
= ship_address.select :state_id, @countries_with_states, { }, { "data-dependant-select-target": "select" }
= ship_address.select :state_id, states_for_country(ship_address_country), { selected: @order.ship_address&.state_id }, { "data-dependant-select-target": "select" }
%div.checkout-input
= ship_address.label :zipcode, t("split_checkout.step1.address.zipcode.label")
@@ -132,7 +136,7 @@
%div.checkout-input
= ship_address.label :country_id, t("split_checkout.step1.address.country_id.label")
= ship_address.select :country_id, @countries, { selected: @order.ship_address.country_id || DefaultCountry.id }, {"data-dependant-select-target": "source", "data-action": "dependant-select#handleSelectChange"}
= ship_address.select :country_id, countries, { selected: ship_address_country.id }, { "data-dependant-select-target": "source", "data-action": "dependant-select#handleSelectChange" }
- if spree_current_user
%div.checkout-input{ "data-toggle-target": "content", style: "display: none" }