From 370ec17818ef828895f90e2b5b516d77b94867e4 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sun, 30 Jan 2022 09:31:38 +0000 Subject: [PATCH] Improve countries and states loading --- app/controllers/concerns/checkout_callbacks.rb | 11 +---------- app/helpers/spree/base_helper.rb | 18 ++++++++++++++++++ app/views/split_checkout/_details.html.haml | 14 +++++++++----- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/controllers/concerns/checkout_callbacks.rb b/app/controllers/concerns/checkout_callbacks.rb index 991fd8e394..66ad90f69d 100644 --- a/app/controllers/concerns/checkout_callbacks.rb +++ b/app/controllers/concerns/checkout_callbacks.rb @@ -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? || diff --git a/app/helpers/spree/base_helper.rb b/app/helpers/spree/base_helper.rb index 11320424b4..92e38aa8a1 100644 --- a/app/helpers/spree/base_helper.rb +++ b/app/helpers/spree/base_helper.rb @@ -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(" ") diff --git a/app/views/split_checkout/_details.html.haml b/app/views/split_checkout/_details.html.haml index 6940784684..83762eda6b 100644 --- a/app/views/split_checkout/_details.html.haml +++ b/app/views/split_checkout/_details.html.haml @@ -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" }