2022-12-19 15:30:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import type { Options as NoticeOptions } from '@wordpress/notices';
|
|
|
|
import { select, dispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { noticeContexts } from '../context/event-emit/utils';
|
|
|
|
|
|
|
|
export const DEFAULT_ERROR_MESSAGE = __(
|
|
|
|
'Something went wrong. Please contact us to get assistance.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
);
|
|
|
|
|
2023-01-19 16:40:52 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of all notice contexts defined by Blocks.
|
|
|
|
*
|
|
|
|
* Contexts are defined in enum format, but this returns an array of strings instead.
|
|
|
|
*/
|
|
|
|
export const getNoticeContexts = () => {
|
|
|
|
return Object.values( noticeContexts );
|
2022-12-19 15:30:13 +00:00
|
|
|
};
|
|
|
|
|
2023-01-19 16:40:52 +00:00
|
|
|
const hasStoreNoticesContainer = ( container: string ): boolean => {
|
|
|
|
const containers = select(
|
|
|
|
'wc/store/store-notices'
|
|
|
|
).getRegisteredContainers();
|
|
|
|
return containers.includes( container );
|
2022-12-19 15:30:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for @wordpress/notices createNotice.
|
|
|
|
*/
|
|
|
|
export const createNotice = (
|
|
|
|
status: 'error' | 'warning' | 'info' | 'success',
|
|
|
|
message: string,
|
|
|
|
options: Partial< NoticeOptions >
|
|
|
|
) => {
|
|
|
|
const noticeContext = options?.context;
|
|
|
|
const suppressNotices =
|
|
|
|
select( 'wc/store/payment' ).isExpressPaymentMethodActive();
|
|
|
|
|
|
|
|
if ( suppressNotices || noticeContext === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-19 16:40:52 +00:00
|
|
|
dispatch( 'core/notices' ).createNotice( status, message, {
|
2022-12-19 15:30:13 +00:00
|
|
|
isDismissible: true,
|
|
|
|
...options,
|
2023-01-19 16:40:52 +00:00
|
|
|
context: noticeContext,
|
2022-12-19 15:30:13 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a notice only if the Store Notice Container is visible.
|
|
|
|
*/
|
|
|
|
export const createNoticeIfVisible = (
|
|
|
|
status: 'error' | 'warning' | 'info' | 'success',
|
|
|
|
message: string,
|
|
|
|
options: Partial< NoticeOptions >
|
|
|
|
) => {
|
|
|
|
if ( options?.context && hasStoreNoticesContainer( options.context ) ) {
|
|
|
|
createNotice( status, message, options );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove notices from all contexts.
|
|
|
|
*
|
|
|
|
* @todo Remove this when supported in Gutenberg.
|
|
|
|
* @see https://github.com/WordPress/gutenberg/pull/44059
|
|
|
|
*/
|
|
|
|
export const removeAllNotices = () => {
|
2023-01-19 16:40:52 +00:00
|
|
|
const containers = select(
|
|
|
|
'wc/store/store-notices'
|
|
|
|
).getRegisteredContainers();
|
2022-12-19 15:30:13 +00:00
|
|
|
const { removeNotice } = dispatch( 'core/notices' );
|
|
|
|
const { getNotices } = select( 'core/notices' );
|
|
|
|
|
|
|
|
containers.forEach( ( container ) => {
|
|
|
|
getNotices( container ).forEach( ( notice ) => {
|
|
|
|
removeNotice( notice.id, container );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
};
|
2023-01-18 17:02:38 +00:00
|
|
|
|
|
|
|
export const removeNoticesWithContext = ( context: string ) => {
|
|
|
|
const { removeNotice } = dispatch( 'core/notices' );
|
|
|
|
const { getNotices } = select( 'core/notices' );
|
|
|
|
|
|
|
|
getNotices( context ).forEach( ( notice ) => {
|
|
|
|
removeNotice( notice.id, context );
|
|
|
|
} );
|
|
|
|
};
|