2019-11-07 00:17:46 +00:00
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
|
import domReady from '@wordpress/dom-ready';
|
2022-01-06 12:53:30 +00:00
|
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
2019-11-07 00:17:46 +00:00
|
|
|
|
|
2019-11-12 01:17:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a promise and resolves when the loader overlay no longer exists.
|
|
|
|
|
*
|
2021-02-18 04:27:32 +00:00
|
|
|
|
* @param {HTMLElement} saveButton Save button DOM element.
|
|
|
|
|
* @return {Promise} Promise for save status.
|
2019-11-12 01:17:36 +00:00
|
|
|
|
*/
|
2021-02-18 04:27:32 +00:00
|
|
|
|
const saveCompleted = ( saveButton ) => {
|
|
|
|
|
if ( saveButton && ! saveButton.disabled ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
|
const promise = new Promise( ( resolve ) => {
|
|
|
|
|
window.requestAnimationFrame( resolve );
|
2019-11-12 01:17:36 +00:00
|
|
|
|
} );
|
2021-02-18 04:27:32 +00:00
|
|
|
|
return promise.then( () => saveCompleted( saveButton ) );
|
2019-11-12 01:17:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.resolve( true );
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-07 00:17:46 +00:00
|
|
|
|
/**
|
|
|
|
|
* Displays a notice on tax settings save.
|
|
|
|
|
*/
|
|
|
|
|
const showTaxCompletionNotice = () => {
|
2019-11-12 01:17:36 +00:00
|
|
|
|
const saveButton = document.querySelector( '.woocommerce-save-button' );
|
|
|
|
|
|
2021-02-18 04:27:32 +00:00
|
|
|
|
if ( saveButton.classList.contains( 'has-tax' ) ) {
|
2019-11-12 01:17:36 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-18 04:27:32 +00:00
|
|
|
|
saveCompleted( saveButton ).then( () => {
|
|
|
|
|
// Check if a row was added successfully after WooCommerce removes invalid rows.
|
|
|
|
|
if ( ! document.querySelector( '.wc_tax_rates .tips' ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveButton.classList.add( 'has-tax' );
|
2019-11-12 01:17:36 +00:00
|
|
|
|
dispatch( 'core/notices' ).createSuccessNotice(
|
2024-08-20 09:13:17 +00:00
|
|
|
|
__( 'You’ve added your first tax rate!', 'woocommerce' ),
|
2019-11-12 01:17:36 +00:00
|
|
|
|
{
|
|
|
|
|
id: 'WOOCOMMERCE_ONBOARDING_TAX_NOTICE',
|
|
|
|
|
actions: [
|
|
|
|
|
{
|
|
|
|
|
url: getAdminLink( 'admin.php?page=wc-admin' ),
|
2022-03-30 09:00:04 +00:00
|
|
|
|
label: __( 'Continue setup.', 'woocommerce' ),
|
2019-11-12 01:17:36 +00:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
2021-02-18 04:27:32 +00:00
|
|
|
|
);
|
|
|
|
|
} );
|
2019-11-07 00:17:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
domReady( () => {
|
|
|
|
|
const saveButton = document.querySelector( '.woocommerce-save-button' );
|
2021-02-18 04:27:32 +00:00
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
window.htmlSettingsTaxLocalizeScript &&
|
|
|
|
|
window.htmlSettingsTaxLocalizeScript.rates &&
|
|
|
|
|
! window.htmlSettingsTaxLocalizeScript.rates.length &&
|
|
|
|
|
saveButton
|
|
|
|
|
) {
|
2019-11-07 00:17:46 +00:00
|
|
|
|
saveButton.addEventListener( 'click', showTaxCompletionNotice );
|
|
|
|
|
}
|
|
|
|
|
} );
|