Skip to content

Commit

Permalink
CET-463/feat: Support address search in NL
Browse files Browse the repository at this point in the history
  • Loading branch information
brtkwr committed Oct 3, 2024
1 parent 5b89c8d commit bfb4627
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions view/frontend/web/js/view/address-autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,44 @@ define([
$(this.companyNameSelector).val(companyName);
$(this.companyIdSelector).val(companyId);
},
setAddressData: function (address) {
$('input[name="city"]').val(response.address.city);
$('input[name="postcode"]').val(address.postal_code);
$('input[name="street[0]"]').val(address.street_ddress);
$('input[name="city"], input[name="postcode"], input[name="street[0]"]').trigger(
'change'
);
},
addressLookup: function (selectedCompany, countryCode) {
if (_.indexOf(self.supportedCountryCodes, countryCode) != -1) {
// Use legacy address search for supported country codes
const addressResponse = $.ajax({
dataType: 'json',
url: `${config.checkoutApiUrl}/v1/${countryCode}/company/${selectedCompany.companyId}/address`
});
addressResponse.done(function (response) {
if (response.address) {
self.setAddressData({
city: response.address.city,
postal_code: response.address.postalCode,
street_ddress: response.address.streetAddress
});
}
});
} else {
// Use new address lookup for unsupported country codes
const addressResponse = $.ajax({
dataType: 'json',
url: `${config.companySearchConfig.searchHost}/companies/v1/company/${selectedCompany.lookupId}`
});
addressResponse.done(function (response) {
// Use new address lookup by default
if (response.address) {
self.setAddressData(response.address);
}
});
}
},
enableCompanySearch: function () {
var self = this;
require(['Two_Gateway/select2-4.1.0/js/select2.min'], function () {
Expand Down Expand Up @@ -101,7 +139,8 @@ define([
id: item.name,
text: item.name,
html: `${item.highlight} (${item.national_identifier.id})`,
companyId: item.national_identifier.id
companyId: item.national_identifier.id,
lookupId: item.lookup_id
});
}
return {
Expand Down Expand Up @@ -140,28 +179,8 @@ define([
$('.select2-selection__rendered').text(selectedItem.id);
self.setCompanyData(selectedItem.companyId, selectedItem.text);
if (self.isAddressSearchEnabled) {
const companyId = selectedItem.companyId;
const countryCode = $(self.countrySelector).val().toLowerCase();
if (_.indexOf(self.supportedCountryCodes, countryCode) != -1) {
const addressResponse = $.ajax({
dataType: 'json',
url: `${config.checkoutApiUrl}/v1/${countryCode}/company/${companyId}/address`
});
addressResponse.done(function (response) {
if (response.address) {
$('input[name="city"]').val(response.address.city);
$('input[name="postcode"]').val(
response.address.postalCode
);
$('input[name="street[0]"]').val(
response.address.streetAddress
);
$(
'input[name="city"], input[name="postcode"], input[name="street[0]"]'
).trigger('change');
}
});
}
const countryCode = $(self.countrySelector).val().toUpperCase();
self.addressLookup(selectedItem, countryCode);
}
});
if ($(self.companyNameSelector).val()) {
Expand Down

0 comments on commit bfb4627

Please sign in to comment.