woocommerce/plugins/woocommerce-admin/client/wp-admin-scripts/onboarding-tax-notice/index.js

70 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { dispatch } from '@wordpress/data';
import domReady from '@wordpress/dom-ready';
import { getAdminLink } from '@woocommerce/settings';
/**
* Returns a promise and resolves when the loader overlay no longer exists.
*
* @param {HTMLElement} saveButton Save button DOM element.
* @return {Promise} Promise for save status.
*/
const saveCompleted = ( saveButton ) => {
if ( saveButton && ! saveButton.disabled ) {
const promise = new Promise( ( resolve ) => {
window.requestAnimationFrame( resolve );
} );
return promise.then( () => saveCompleted( saveButton ) );
}
return Promise.resolve( true );
};
/**
* Displays a notice on tax settings save.
*/
const showTaxCompletionNotice = () => {
const saveButton = document.querySelector( '.woocommerce-save-button' );
if ( saveButton.classList.contains( 'has-tax' ) ) {
return;
}
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' );
dispatch( 'core/notices' ).createSuccessNotice(
__( 'Youve added your first tax rate!', 'woocommerce' ),
{
id: 'WOOCOMMERCE_ONBOARDING_TAX_NOTICE',
actions: [
{
url: getAdminLink( 'admin.php?page=wc-admin' ),
label: __( 'Continue setup.', 'woocommerce' ),
},
],
}
);
} );
};
domReady( () => {
const saveButton = document.querySelector( '.woocommerce-save-button' );
if (
window.htmlSettingsTaxLocalizeScript &&
window.htmlSettingsTaxLocalizeScript.rates &&
! window.htmlSettingsTaxLocalizeScript.rates.length &&
saveButton
) {
saveButton.addEventListener( 'click', showTaxCompletionNotice );
}
} );