Skip to content

Commit

Permalink
Support address validation in new backend customer address modal for …
Browse files Browse the repository at this point in the history
…Magento 2.3, fix JS errors
  • Loading branch information
fastdivision committed Jul 11, 2019
1 parent 5e14152 commit a0c17fb
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 30 deletions.
50 changes: 50 additions & 0 deletions Block/Adminhtml/Customer/Edit/Address/ValidateButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Taxjar_SalesTax
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category Taxjar
* @package Taxjar_SalesTax
* @copyright Copyright (c) 2017 TaxJar. TaxJar is a trademark of TPS Unlimited, Inc. (http://www.taxjar.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

namespace Taxjar\SalesTax\Block\Adminhtml\Customer\Edit\Address;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magento\Customer\Block\Adminhtml\Edit\GenericButton;

class ValidateButton extends GenericButton implements ButtonProviderInterface
{
/**
* @inheritdoc
*
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Validate Address'),
'on_click' => '',
'data_attribute' => [
'mage-init' => [
'Magento_Ui/js/form/button-adapter' => [
'actions' => [
[
'targetName' => 'addressValidation',
'actionName' => 'validateAddress'
],
],
],
],
],
'sort_order' => 30
];
}
}
19 changes: 15 additions & 4 deletions Ui/Component/Customer/ValidateAddressButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Taxjar\SalesTax\Ui\Component\Customer;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
Expand All @@ -31,6 +32,11 @@ class ValidateAddressButton extends Field
*/
protected $scopeConfig;

/**
* @var ProductMetadataInterface
*/
protected $productMetadata;

/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
Expand All @@ -42,10 +48,12 @@ public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
ScopeConfigInterface $scopeConfig,
ProductMetadataInterface $productMetadata,
array $components = [],
array $data = []
) {
$this->scopeConfig = $scopeConfig;
$this->productMetadata = $productMetadata;
parent::__construct($context, $uiComponentFactory, $components, $data);
}

Expand All @@ -58,10 +66,13 @@ public function prepare()

$isEnabled = $this->scopeConfig->getValue(TaxjarConfig::TAXJAR_ADDRESS_VALIDATION);

$newData = array_replace_recursive(
['componentDisabled' => !$isEnabled],
(array) $this->getData('config')
);
// If address validation is disabled or version >= 2.3, hide this button
if (!$isEnabled || floatval($this->productMetadata->getVersion()) >= 2.3) {
$newData = array_replace_recursive(
['componentDisabled' => true],
(array) $this->getData('config')
);
}

$this->setData('config', $newData);
}
Expand Down
3 changes: 1 addition & 2 deletions view/adminhtml/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ var config = {
map: {
'*': {
taxjarClipboard: 'Taxjar_SalesTax/js/clipboard',
taxjarPopup: 'Taxjar_SalesTax/js/popup',
taxjarModal: 'Taxjar_SalesTax/js/modal'
taxjarPopup: 'Taxjar_SalesTax/js/popup'
}
}
};
25 changes: 25 additions & 0 deletions view/adminhtml/ui_component/customer_address_form.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<!--
/**
* Taxjar_SalesTax
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category Taxjar
* @package Taxjar_SalesTax
* @copyright Copyright (c) 2017 TaxJar. TaxJar is a trademark of TPS Unlimited, Inc. (http://www.taxjar.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<settings>
<buttons>
<button name="validate" class="Taxjar\SalesTax\Block\Adminhtml\Customer\Edit\Address\ValidateButton"/>
</buttons>
</settings>
</form>
46 changes: 34 additions & 12 deletions view/adminhtml/web/js/suggested_addresses_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ define([
'uiComponent',
'uiRegistry',
'Magento_Ui/js/modal/alert',
'taxjarModal',
'Taxjar_SalesTax/js/modal',
'Taxjar_SalesTax/js/model/address_validation_core'
], function (ko, $, Component, uiRegistry, alert, taxjarModal, avCore) {
], function (ko, $, Component, uiRegistry, alert, modal, avCore) {
'use strict';

return Component.extend({
Expand All @@ -46,7 +46,7 @@ define([

var self = this;

this.addressModal = taxjarModal({
this.addressModal = $('#tj-suggested-addresses').modal({
buttons: [
{
text: $.mage.__('Edit Address'),
Expand All @@ -63,6 +63,7 @@ define([
var selectedAddressId = uiRegistry.get('addressValidation').suggestedAddressRadio();
var selectedAddress = addrs[selectedAddressId].address;
var forms = [this.data.form];
var formScope = 'customer_address_form.customer_address_form.general';

if (window.order && window.order.shippingAsBilling) {
forms.push($('.order-shipping-address > fieldset'));
Expand All @@ -76,11 +77,19 @@ define([
forms[x].find('select[name*="[country_id]"]').val(selectedAddress.countryId);
}

if (uiRegistry.get(formScope)) {
uiRegistry.get(formScope + '.street.street_0').value(selectedAddress.street[0]);
uiRegistry.get(formScope + '.city').value(selectedAddress.city);
uiRegistry.get(formScope + '.region_id').value(selectedAddress.regionId);
uiRegistry.get(formScope + '.country_id').value(selectedAddress.countryId);
uiRegistry.get(formScope + '.postcode').value(selectedAddress.postcode);
}

this.closeModal();
}
}
]
}, $('#tj-suggested-addresses'));
});

if (config.controller === 'order_create') {
this.appendButtonToOrder();
Expand Down Expand Up @@ -130,22 +139,35 @@ define([
var body = $('body');
button = button || $('[data-index="validateAddressButton"]:visible');
var form = button.closest('fieldset');
var formScope = 'customer_address_form.customer_address_form.general';
var formValues = $(form).serializeArray();
var addr = {
street: [this.getAddressFormValue(formValues, '[street][0]')],
city: this.getAddressFormValue(formValues, '[city]'),
regionId: this.getAddressFormValue(formValues, '[region_id]'),
countryId: this.getAddressFormValue(formValues, '[country_id]'),
postcode: this.getAddressFormValue(formValues, '[postcode]')
};
var addr = {};

if (uiRegistry.get(formScope)) {
addr = {
street: [uiRegistry.get(formScope + '.street.street_0').value()],
city: uiRegistry.get(formScope + '.city').value(),
regionId: uiRegistry.get(formScope + '.region_id').value(),
countryId: uiRegistry.get(formScope + '.country_id').value(),
postcode: uiRegistry.get(formScope + '.postcode').value()
};
} else {
addr = {
street: [this.getAddressFormValue(formValues, '[street][0]')],
city: this.getAddressFormValue(formValues, '[city]'),
regionId: this.getAddressFormValue(formValues, '[region_id]'),
countryId: this.getAddressFormValue(formValues, '[country_id]'),
postcode: this.getAddressFormValue(formValues, '[postcode]')
};
}

button.attr('disabled', true);
body.trigger('processStart');

avCore.getSuggestedAddresses(addr, function (res) {
button.attr('disabled', false);
body.trigger('processStop');
self.addressModal.openModal({'form': form});
self.addressModal.data('mage-modal').openModal({'form': form});
}, function (res) {
button.attr('disabled', false);
body.trigger('processStop');
Expand Down
10 changes: 5 additions & 5 deletions view/base/web/js/address_validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ define([
'ko',
'jquery',
'uiRegistry',
'taxjarModal',
'Taxjar_SalesTax/js/modal',
'Taxjar_SalesTax/js/model/address_validation_core'
], function (ko, $, uiRegistry, taxjarModal, avCore) {
], function (ko, $, uiRegistry, modal, avCore) {
'use strict';

return function (addressValidation) {
Expand All @@ -36,7 +36,7 @@ define([
_create: function () {
var body = $('body');
var button = $(this.options.selectors.button, this.element);
var addressModal = taxjarModal({
var addressModal = $('#tj-suggested-addresses').modal({
buttons: [
{
text: $.mage.__('Edit Address'),
Expand All @@ -62,7 +62,7 @@ define([
}
}
]
}, $('#tj-suggested-addresses'));
});

this.element.validation({
/**
Expand All @@ -84,7 +84,7 @@ define([
avCore.getSuggestedAddresses(addr, function (res) {
button.attr('disabled', false);
body.trigger('processStop');
addressModal.openModal({'form': form});
addressModal.data('mage-modal').openModal({'form': form});
}, function (res) {
button.attr('disabled', false);
body.trigger('processStop');
Expand Down
4 changes: 2 additions & 2 deletions view/base/web/js/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ define([
], function ($) {
'use strict';

$.widget('taxjar.modal', $.mage.modal, {
$.widget('mage.modal', $.mage.modal, {
data: {},
openModal: function (data) {
if (data) {
Expand All @@ -31,5 +31,5 @@ define([
}
});

return $.taxjar.modal;
return $.mage.modal;
});
5 changes: 0 additions & 5 deletions view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
*/

var config = {
map: {
'*': {
taxjarModal: 'Taxjar_SalesTax/js/modal'
}
},
config: {
mixins: {
'Magento_Customer/js/addressValidation': {
Expand Down

0 comments on commit a0c17fb

Please sign in to comment.