Replace Angular directive and use stimulus controller

+ Update specs as well
This commit is contained in:
Jean-Baptiste Bellet
2022-05-02 13:37:12 +02:00
parent ce058c6e36
commit 2c29b1f60f
5 changed files with 13 additions and 84 deletions

View File

@@ -1,62 +0,0 @@
angular.module("admin.orders").directive 'customerSearchOverride', ->
restrict: 'C'
scope:
distributorId: '@'
link: (scope, element, attr) ->
if $('#customer_autocomplete_template').length > 0
customerTemplate = Handlebars.compile($('#customer_autocomplete_template').text())
formatCustomerResult = (customer) ->
customerTemplate
customer: customer
bill_address: customer.bill_address
ship_address: customer.ship_address
element.select2
placeholder: Spree.translations.choose_a_customer
minimumInputLength: 3
ajax:
url: '/admin/search/customers.json'
datatype: 'json'
data: (term, page) ->
{
q: term
distributor_id: scope.distributorId # modified
}
results: (data, page) ->
{ results: data }
dropdownCssClass: 'customer_search'
formatResult: formatCustomerResult
formatSelection: (customer) ->
_.each [
'bill_address'
'ship_address'
], (address) ->
data = customer[address]
address_parts = [
'firstname'
'lastname'
'company'
'address1'
'address2'
'city'
'zipcode'
'phone'
]
attribute_wrapper = '#order_' + address + '_attributes_'
if data # modified
_.each address_parts, (part) ->
$(attribute_wrapper + part).val data[part]
return
$(attribute_wrapper + 'state_id').select2 'val', data['state_id']
$(attribute_wrapper + 'country_id').select2 'val', data['country_id']
else
_.each address_parts, (part) ->
$(attribute_wrapper + part).val ''
return
$(attribute_wrapper + 'state_id').select2 'val', ''
$(attribute_wrapper + 'country_id').select2 'val', ''
return
$('#order_email').val customer.email
$('#user_id').val customer.user_id # modified
customer.email

View File

@@ -1,19 +0,0 @@
<script type='text/template' id='customer_autocomplete_template'>
<div class='customer-autocomplete-item'>
<div class='customer-details'>
<h5>{{customer.email}}</h5>
{{#if bill_address.firstname }}
<strong>{{t 'bill_address' }}</strong>
{{bill_address.firstname}} {{bill_address.lastname}}<br>
{{bill_address.address1}}, {{bill_address.address2}}<br>
{{bill_address.city}}<br>
{{#if bill_address.state_id }}
{{bill_address.state.name}}
{{else}}
{{bill_address.state_name}}
{{/if}}
{{bill_address.country.name}}
{{/if}}
</div>
</div>
</script>

View File

@@ -17,8 +17,8 @@
%legend{:align => "center"}= Spree.t(:customer_search)
- content_for :main_ng_app_name do
= "admin.orders"
= hidden_field_tag :customer_search_override, nil, distributor_id: @order.distributor_id, :class => 'fullwidth title customer-search-override'
= render :partial => "spree/admin/orders/customer_details/autocomplete", :formats => :js
%label{for: "customer_search_override"}= Spree.t(:choose_a_customer)
%select{name: "customer_search_override", "data-controller": "select-customer", "data-select-customer-distributor-value": @order.distributor_id, class: "primary", placeholder: Spree.t(:choose_a_customer) }
= render :partial => 'spree/shared/error_messages', :locals => { :target => @order }

View File

@@ -127,6 +127,12 @@ module WebHelper
page.find(:css, 'body').click
end
def tomselect_search_and_select(value, options)
page.find("[name='#{options[:from]}']").sibling(".ts-wrapper").click
page.find(:css, '.ts-dropdown input.dropdown-input').set(value)
page.find(:css, '.ts-dropdown .ts-dropdown-content .option', text: value).click
end
def accept_js_alert
page.driver.browser.switch_to.alert.accept
end

View File

@@ -383,7 +383,7 @@ describe '
expect(page).to have_selector '#select-customer'
# And I select that customer's email address and save the order
select2_select customer.email, from: 'customer_search_override', search: true
tomselect_search_and_select customer.email, from: 'customer_search_override'
click_button 'Update'
expect(page).to have_selector "h1.js-admin-page-title", text: "Customer Details"
@@ -391,6 +391,10 @@ describe '
order = Spree::Order.last
expect(order.ship_address.lastname).to eq customer.ship_address.lastname
expect(order.bill_address.lastname).to eq customer.bill_address.lastname
expect(order.ship_address.zipcode).to eq customer.ship_address.zipcode
expect(order.bill_address.zipcode).to eq customer.bill_address.zipcode
expect(order.ship_address.city).to eq customer.ship_address.city
expect(order.bill_address.city).to eq customer.bill_address.city
end
context "as an enterprise manager" do