Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Improve handling of editing Addresses in the Admin Panel

Damian Legawiecbfc6c17491a7
diff --git a/.eslintrc b/.eslintrc
index 4f2027af5d..c6b5e5c2ce 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -14,6 +14,9 @@ globals:
CouponManager: true
ThumbnailsCarousel: true
Turbolinks: true
+ ADDRESS_FIELDS: true
+ AUTH_TOKEN: true
+ update_state: true
env:
browser: true
diff --git a/backend/app/assets/javascripts/spree/backend/address_states.js b/backend/app/assets/javascripts/spree/backend/address_states.js
index 2afa8efbef..60f7009c20 100644
--- a/backend/app/assets/javascripts/spree/backend/address_states.js
+++ b/backend/app/assets/javascripts/spree/backend/address_states.js
@@ -1,20 +1,22 @@
// eslint-disable-next-line camelcase, no-unused-vars
-function update_state (region, done) {
+function update_state(region, done) {
'use strict'
- var country = $('span#' + region + 'country .select2').val()
- var stateSelect = $('span#' + region + 'state select.select2')
- var stateInput = $('span#' + region + 'state input.state_name')
+ var countryId = $('#' + region + 'country select').val()
+ var stateContainer = $('#' + region + 'state').parent()
+ var stateSelect = $('#' + region + 'state select')
+ var stateInput = $('#' + region + 'state input.state_name')
- $.get(Spree.routes.states_search + '?country_id=' + country, function (data) {
+ $.get(Spree.routes.states_search + '?country_id=' + countryId, function (data) {
var states = data.states
+ var statesRequired = data.states_required
if (states.length > 0) {
stateSelect.html('')
var statesWithBlank = [{
name: '',
id: ''
}].concat(states)
- $.each(statesWithBlank, function (pos, state) {
+ $.each(statesWithBlank, function (_pos, state) {
var opt = $(document.createElement('option'))
.prop('value', state.id)
.html(state.name)
@@ -23,10 +25,18 @@ function update_state (region, done) {
stateSelect.prop('disabled', false).show()
stateSelect.select2()
stateInput.hide().prop('disabled', true)
+ stateContainer.show()
} else {
- stateInput.prop('disabled', false).show()
- var select = stateSelect.select2()
- select.select2('destroy').hide()
+ stateSelect.val(null).trigger('change')
+ if (stateSelect.data('select2')) {
+ stateSelect.select2('destroy')
+ }
+ stateSelect.hide()
+ if (statesRequired) {
+ stateInput.prop('disabled', false).show()
+ } else {
+ stateContainer.hide()
+ }
}
if (done) done()
diff --git a/backend/app/assets/javascripts/spree/backend/checkouts/edit.js b/backend/app/assets/javascripts/spree/backend/checkouts/edit.js
index 88a4411347..403ddfabf3 100644
--- a/backend/app/assets/javascripts/spree/backend/checkouts/edit.js
+++ b/backend/app/assets/javascripts/spree/backend/checkouts/edit.js
@@ -1,30 +1,13 @@
-//= require_self
-/* global customerTemplate, update_state */
-// eslint-disable-next-line camelcase
-
-function clear_billing_address_fields () {
- var fields = ['firstname', 'lastname', 'company', 'address1', 'address2',
- 'city', 'zipcode', 'state_id', 'country_id', 'phone']
- $.each(fields, function (i, field) {
- $('#order_bill_address_attributes_' + field).val('')
- })
-}
-
-function clear_shipping_address_fields () {
- var fields = ['firstname', 'lastname', 'company', 'address1', 'address2',
- 'city', 'zipcode', 'state_id', 'country_id', 'phone']
- $.each(fields, function (i, field) {
- $('#order_ship_address_attributes_' + field).val('')
+function clearAddressFields(kinds = ['ship', 'bill']) {
+ kinds.forEach(function(kind) {
+ ADDRESS_FIELDS.forEach(function(field) {
+ $('#order_' + kind + '_address_attributes_' + field).val('')
+ })
})
}
-function clear_address_fields () {
- clear_billing_address_fields ()
- clear_shipping_address_fields ()
-}
-
-function formatCustomerResult (customer) {
- var escapedResult = customerTemplate({
+function formatCustomerResult(customer) {
+ var escapedResult = window.customerTemplate({
customer: customer,
bill_address: customer.bill_address,
ship_address: customer.ship_address
@@ -32,7 +15,27 @@ function formatCustomerResult (customer) {
return $(escapedResult)
}
-function formatCustomerSelection (customer) {
+function formatCustomerAddress(address, kind) {
+ $('#order_' + kind + '_address_attributes_firstname').val(address.firstname)
+ $('#order_' + kind + '_address_attributes_lastname').val(address.lastname)
+ $('#order_' + kind + '_address_attributes_address1').val(address.address1)
+ $('#order_' + kind + '_address_attributes_company').val(address.company)
+ $('#order_' + kind + '_address_attributes_address2').val(address.address2)
+ $('#order_' + kind + '_address_attributes_city').val(address.city)
+ $('#order_' + kind + '_address_attributes_zipcode').val(address.zipcode)
+ $('#order_' + kind + '_address_attributes_phone').val(address.phone)
+ $('#order_' + kind + '_address_attributes_phone').val(address.phone)
+ $('#order_' + kind + '_address_attributes_country_id').val(address.country_id)
+ $('#order_' + kind + '_address_attributes_country_id').trigger('change')
+
+ var stateSelect = $('#order_' + kind + '_address_attributes_state_id')
+
+ update_state(kind.charAt(0), function() {
+ stateSelect.val(address.state_id).trigger('change')
+ })
+}
+
+function formatCustomerSelection(customer) {
$('#order_email').val(customer.email)
$('#order_user_id').val(customer.id)
$('#guest_checkout_true').prop('checked', false)
@@ -43,47 +46,15 @@ function formatCustomerSelection (customer) {
var shipAddress = customer.ship_address
if (billAddress) {
- $('#order_bill_address_attributes_firstname').val(billAddress.firstname)
- $('#order_bill_address_attributes_lastname').val(billAddress.lastname)
- $('#order_bill_address_attributes_address1').val(billAddress.address1)
- $('#order_bill_address_attributes_company').val(billAddress.company)
- $('#order_bill_address_attributes_address2').val(billAddress.address2)
- $('#order_bill_address_attributes_city').val(billAddress.city)
- $('#order_bill_address_attributes_zipcode').val(billAddress.zipcode)
- $('#order_bill_address_attributes_phone').val(billAddress.phone)
- $('#order_bill_address_attributes_phone').val(billAddress.phone)
- $('#order_bill_address_attributes_country_id').select2().val(billAddress.country_id)
- $('#order_bill_address_attributes_country_id').select2().trigger('change.select2').promise().done(function () {
- update_state('b', function () {
- if ($('span#bstate select.select2').find("option[value='" + billAddress.state_id + "']").length) {
- $('span#bstate select.select2').val(billAddress.state_id).trigger('change.select2')
- }
- })
- })
+ formatCustomerAddress(billAddress, 'bill')
} else {
- clear_billing_address_fields ()
+ clearAddressFields(['bill'])
}
if (shipAddress) {
- $('#order_ship_address_attributes_firstname').val(shipAddress.firstname)
- $('#order_ship_address_attributes_lastname').val(shipAddress.lastname)
- $('#order_ship_address_attributes_address1').val(shipAddress.address1)
- $('#order_ship_address_attributes_company').val(shipAddress.company)
- $('#order_ship_address_attributes_address2').val(shipAddress.address2)
- $('#order_ship_address_attributes_city').val(shipAddress.city)
- $('#order_ship_address_attributes_zipcode').val(shipAddress.zipcode)
- $('#order_ship_address_attributes_phone').val(shipAddress.phone)
- $('#order_ship_address_attributes_phone').val(shipAddress.phone)
- $('#order_ship_address_attributes_country_id').select2().val(shipAddress.country_id)
- $('#order_ship_address_attributes_country_id').select2().trigger('change.select2').promise().done(function () {
- update_state('s', function () {
- if ($('span#sstate select.select2').find("option[value='" + shipAddress.state_id + "']").length) {
- $('span#sstate select.select2').val(shipAddress.state_id).trigger('change.select2')
- }
- })
- })
+ formatCustomerAddress(shipAddress, 'ship')
} else {
- clear_shipping_address_fields ()
+ clearAddressFields(['ship'])
}
return customer.email
@@ -112,7 +83,7 @@ function set_customer_search_select (selector) {
token: Spree.api_key
}
},
- processResults: function (data, page) {
+ processResults: function (data, _page) {
return { results: data['users'] }
}
},
@@ -155,6 +126,6 @@ document.addEventListener('DOMContentLoaded', function() {
$('#customer_search').val('')
$('#order_user_id').val('')
$('#order_email').val('')
- clear_address_fields()
+ clearAddressFields()
})
})
diff --git a/backend/app/views/spree/admin/orders/customer_details/_form.html.erb b/backend/app/views/spree/admin/orders/customer_details/_form.html.erb
index 3104b94b26..2ecc7d4673 100644
--- a/backend/app/views/spree/admin/orders/customer_details/_form.html.erb
+++ b/backend/app/views/spree/admin/orders/customer_details/_form.html.erb
@@ -83,8 +83,8 @@
<%= f.fields_for :ship_address do |sa_form| %>
<div class="form-group">
<span data-hook="use_billing">
- <%= check_box_tag 'order[use_billing]', '1', @order.shipping_eq_billing_address? %>
- <%= label_tag 'order[use_billing]', Spree.t(:use_billing_address) %>
+ <%= check_box_tag 'order[use_billing]', '1', @order.shipping_eq_billing_address?, class: 'checkbox' %>
+ <%= label_tag 'order[use_billing]', Spree.t(:use_billing_address), class: 'form-check-label' %>
</span>
</div>
diff --git a/backend/app/views/spree/admin/shared/_address_form.html.erb b/backend/app/views/spree/admin/shared/_address_form.html.erb
index eb27dfd4de..983aa46358 100644
--- a/backend/app/views/spree/admin/shared/_address_form.html.erb
+++ b/backend/app/views/spree/admin/shared/_address_form.html.erb
@@ -4,25 +4,25 @@
<% if field == "country" %>
<%= field_container (s_or_b + '_' + field), nil, class: ["form-group", "#{type}-row"] do %>
<%= f.label :country_id, Spree.t(:country) %>
- <span id="<%= s_or_b %>country">
- <%= f.collection_select :country_id, available_countries, :id, :name, {}, { class: 'select2' } %>
- </span>
+ <div id="<%= s_or_b %>country">
+ <%= f.collection_select :country_id, available_countries, :id, :name, {}, { class: 'select2 w-100' } %>
+ </div>
<% end %>
<% elsif field == "state" %>
<%= field_container (s_or_b + '_' + field), :state, class: ["form-group", "#{type}-row"] do %>
<%= f.label :state_id, Spree.t(:state) %>
- <span id="<%= s_or_b %>state">
+ <div id="<%= s_or_b %>state">
<% if f.object.country.try(:states) %>
<%= f.text_field :state_name,
style: "display: #{f.object.country.try(:states) && f.object.country.states.empty? ? 'block' : 'none' };",
disabled: !(f.object.country.try(:states) && f.object.country.states.empty?), class: 'form-control state_name' %>
- <%= f.collection_select :state_id, f.object.country.states.sort, :id, :name, { include_blank: true }, { class: 'select2', disabled: f.object.country.states.empty? } %>
+ <%= f.collection_select :state_id, f.object.country.states.sort, :id, :name, { include_blank: true }, { class: 'select2 w-100', disabled: f.object.country.states.empty? } %>
<% else %>
<div>
<em><%= Spree.t(:no_country) %></em>
</div>
<% end %>
- </span>
+ </div>
<%= error_message_on f.object, :state_id %>
<% end %>
<% else %>
@@ -38,7 +38,7 @@
<% content_for :head do %>
<%= javascript_tag do %>
$(document).ready(function(){
- $('span#<%= s_or_b %>country .select2').on('change', function() { update_state('<%= s_or_b %>'); });
+ $('#<%= s_or_b %>country select').on('change', function() { update_state('<%= s_or_b %>'); });
});
<% end %>
<% end %>
diff --git a/backend/app/views/spree/admin/shared/_head.html.erb b/backend/app/views/spree/admin/shared/_head.html.erb
index 02b9de23a4..8da3d49297 100644
--- a/backend/app/views/spree/admin/shared/_head.html.erb
+++ b/backend/app/views/spree/admin/shared/_head.html.erb
@@ -22,6 +22,7 @@
<%= javascript_tag do %>
<%== "var AUTH_TOKEN = #{form_authenticity_token.inspect};" %>
<%== "Spree.api_key = '#{try_spree_current_user.spree_api_key}';" if try_spree_current_user %>
+ <%== "var ADDRESS_FIELDS = #{Spree::Address::ADDRESS_FIELDS}" %>
<% end %>
<%= yield :head %>
diff --git a/backend/app/views/spree/admin/users/_addresses_form.html.erb b/backend/app/views/spree/admin/users/_addresses_form.html.erb
index 9bfbdbbe94..427fbc91fc 100644
--- a/backend/app/views/spree/admin/users/_addresses_form.html.erb
+++ b/backend/app/views/spree/admin/users/_addresses_form.html.erb
@@ -25,12 +25,10 @@
</div>
<div class="card-body">
- <p class="field checkbox" data-hook="use_billing">
- <%= label_tag :use_billing, id: 'use_billing' do %>
- <% checked = @user.bill_address.nil? ? false : @user.bill_address == @user.ship_address %>
- <%= check_box_tag 'user[use_billing]', '1', checked %>
- <%= Spree.t(:use_billing_address) %>
- <% end %>
+ <p class="form-group" data-hook="use_billing">
+ <% checked = @user.bill_address.nil? ? false : @user.bill_address == @user.ship_address %>
+ <%= check_box_tag 'user[use_billing]', '1', checked, class: 'checkbox' %>
+ <%= label_tag :use_billing, Spree.t(:use_billing_address), for: 'user_use_billing', id: 'use_billing', class: 'form-check-label' %>
</p>
<%= f.fields_for :ship_address, (@user.ship_address || Spree::Address.default(@user, "ship")) do |sa_form| %>
<% sa_form.object ||= Spree::Address.new(country: Spree::Country.new) %>
diff --git a/backend/spec/features/admin/orders/customer_details_spec.rb b/backend/spec/features/admin/orders/customer_details_spec.rb
index cf4dfe2fde..d2a1a13570 100644
--- a/backend/spec/features/admin/orders/customer_details_spec.rb
+++ b/backend/spec/features/admin/orders/customer_details_spec.rb
@@ -71,7 +71,7 @@ describe 'Customer Details', type: :feature, js: true do
end
context 'selected country has no state' do
- before { create(:country, iso: 'BRA', name: 'Brazil') }
+ before { create(:country, iso: 'BRA', name: 'Brazil', states_required: true) }
it 'changes state field to text input' do
click_link 'Customer'