2019-10-21 18:13:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-11-12 15:31:44 +00:00
|
|
|
import { dispatch } from '@wordpress/data';
|
2019-10-21 18:13:25 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-11-12 15:31:44 +00:00
|
|
|
import domReady from '@wordpress/dom-ready';
|
2019-11-22 17:07:26 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/wc-admin-settings';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { queueRecordEvent } from '@woocommerce/tracks';
|
2019-10-21 18:13:25 +00:00
|
|
|
|
2019-11-15 13:32:02 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
|
2019-10-21 18:13:25 +00:00
|
|
|
/**
|
2019-11-12 15:31:44 +00:00
|
|
|
* Returns a promise and resolves when the post begins to publish.
|
2019-10-21 18:13:25 +00:00
|
|
|
*
|
2019-11-12 15:31:44 +00:00
|
|
|
* @return {Promise} Promise for overlay existence.
|
2019-10-21 18:13:25 +00:00
|
|
|
*/
|
2019-11-12 15:31:44 +00:00
|
|
|
const saveStarted = () => {
|
2020-03-02 22:22:32 +00:00
|
|
|
if (
|
|
|
|
! document
|
|
|
|
.querySelector( '.editor-post-publish-button' )
|
|
|
|
.classList.contains( 'is-busy' )
|
|
|
|
) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const promise = new Promise( ( resolve ) => {
|
|
|
|
window.requestAnimationFrame( resolve );
|
2019-11-12 15:31:44 +00:00
|
|
|
} );
|
|
|
|
return promise.then( () => saveStarted() );
|
|
|
|
}
|
2019-10-21 18:13:25 +00:00
|
|
|
|
2019-11-12 15:31:44 +00:00
|
|
|
return Promise.resolve( true );
|
|
|
|
};
|
2019-10-21 18:13:25 +00:00
|
|
|
|
2019-11-12 15:31:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a promise and resolves when the post has been saved and notices have shown.
|
|
|
|
*
|
|
|
|
* @return {Promise} Promise for overlay existence.
|
|
|
|
*/
|
|
|
|
const saveCompleted = () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
if (
|
2020-03-02 22:22:32 +00:00
|
|
|
document
|
|
|
|
.querySelector( '.editor-post-publish-button' )
|
|
|
|
.classList.contains( 'is-busy' )
|
2020-02-14 02:23:21 +00:00
|
|
|
) {
|
|
|
|
const promise = new Promise( ( resolve ) => {
|
|
|
|
window.requestAnimationFrame( resolve );
|
2019-11-12 15:31:44 +00:00
|
|
|
} );
|
|
|
|
return promise.then( () => saveCompleted() );
|
|
|
|
}
|
2019-10-21 18:13:25 +00:00
|
|
|
|
2019-11-12 15:31:44 +00:00
|
|
|
return Promise.resolve( true );
|
|
|
|
};
|
2019-10-21 18:13:25 +00:00
|
|
|
|
2019-11-12 15:31:44 +00:00
|
|
|
/**
|
|
|
|
* Displays a notice on page save and updates the hompage options.
|
|
|
|
*/
|
|
|
|
const onboardingHomepageNotice = () => {
|
|
|
|
const saveButton = document.querySelector( '.editor-post-publish-button' );
|
|
|
|
if ( saveButton.classList.contains( 'is-clicked' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-21 18:13:25 +00:00
|
|
|
|
2019-11-12 15:31:44 +00:00
|
|
|
saveButton.classList.add( 'is-clicked' );
|
2019-10-21 18:13:25 +00:00
|
|
|
|
2019-11-12 15:31:44 +00:00
|
|
|
saveCompleted().then( () => {
|
|
|
|
const notificationType =
|
2020-02-14 02:23:21 +00:00
|
|
|
document.querySelector( '.components-snackbar__content' ) !== null
|
|
|
|
? 'snackbar'
|
|
|
|
: 'default';
|
2019-10-21 18:13:25 +00:00
|
|
|
|
2019-11-12 15:31:44 +00:00
|
|
|
dispatch( 'core/notices' ).removeNotice( 'SAVE_POST_NOTICE_ID' );
|
|
|
|
dispatch( 'core/notices' ).createSuccessNotice(
|
2020-02-14 02:23:21 +00:00
|
|
|
__(
|
|
|
|
"🏠 Nice work creating your store's homepage!",
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
2019-11-12 15:31:44 +00:00
|
|
|
{
|
|
|
|
id: 'WOOCOMMERCE_ONBOARDING_HOME_PAGE_NOTICE',
|
|
|
|
type: notificationType,
|
|
|
|
actions: [
|
2019-10-21 18:13:25 +00:00
|
|
|
{
|
2019-11-12 15:31:44 +00:00
|
|
|
label: __( 'Continue setup.', 'woocommerce-admin' ),
|
2019-11-15 13:32:02 +00:00
|
|
|
onClick: () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
queueRecordEvent(
|
|
|
|
'tasklist_appearance_continue_setup',
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
window.location = getAdminLink(
|
|
|
|
'admin.php?page=wc-admin&task=appearance'
|
|
|
|
);
|
2019-11-15 13:32:02 +00:00
|
|
|
},
|
2019-11-12 15:31:44 +00:00
|
|
|
},
|
|
|
|
],
|
2019-10-21 18:13:25 +00:00
|
|
|
}
|
2019-11-12 15:31:44 +00:00
|
|
|
);
|
2019-10-21 18:13:25 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2019-11-12 15:31:44 +00:00
|
|
|
domReady( () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
const publishButton = document.querySelector(
|
2020-03-02 22:22:32 +00:00
|
|
|
'.editor-post-publish-button'
|
2020-02-14 02:23:21 +00:00
|
|
|
);
|
2019-11-12 15:31:44 +00:00
|
|
|
if ( publishButton ) {
|
2020-03-02 22:22:32 +00:00
|
|
|
publishButton.addEventListener(
|
|
|
|
'click',
|
|
|
|
saveStarted().then( () => onboardingHomepageNotice() )
|
|
|
|
);
|
2019-10-21 18:13:25 +00:00
|
|
|
}
|
|
|
|
} );
|