2020-03-16 20:57:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { Notice } from 'wordpress-components';
|
2022-10-05 09:59:38 +00:00
|
|
|
import { sanitizeHTML } from '@woocommerce/utils';
|
2022-08-22 14:07:13 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2022-10-06 12:46:46 +00:00
|
|
|
import { PAYMENT_STORE_KEY } from '@woocommerce/block-data';
|
2022-11-17 13:33:58 +00:00
|
|
|
import type { Notice as NoticeType } from '@wordpress/notices';
|
2022-08-22 14:07:13 +00:00
|
|
|
|
2020-03-16 20:57:12 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
const getWooClassName = ( { status = 'default' } ) => {
|
|
|
|
switch ( status ) {
|
|
|
|
case 'error':
|
|
|
|
return 'woocommerce-error';
|
|
|
|
case 'success':
|
2021-08-12 12:43:35 +00:00
|
|
|
return 'woocommerce-message';
|
2020-03-16 20:57:12 +00:00
|
|
|
case 'info':
|
|
|
|
case 'warning':
|
|
|
|
return 'woocommerce-info';
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
2022-11-17 13:33:58 +00:00
|
|
|
interface StoreNoticesContainerProps {
|
|
|
|
className?: string;
|
|
|
|
context?: string;
|
|
|
|
additionalNotices?: NoticeType[];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component that displays notices from the core/notices data store. See
|
|
|
|
* https://developer.wordpress.org/block-editor/reference-guides/data/data-core-notices/ for more information on this
|
|
|
|
* data store.
|
|
|
|
*
|
|
|
|
* @param props
|
|
|
|
* @param props.className Class name to add to the container.
|
|
|
|
* @param props.context Context to show notices from.
|
|
|
|
* @param props.additionalNotices Additional notices to display.
|
|
|
|
* @function Object() { [native code] }
|
|
|
|
*/
|
2022-04-08 12:11:50 +00:00
|
|
|
export const StoreNoticesContainer = ( {
|
|
|
|
className,
|
|
|
|
context = 'default',
|
|
|
|
additionalNotices = [],
|
2022-11-17 13:33:58 +00:00
|
|
|
}: StoreNoticesContainerProps ): JSX.Element | null => {
|
2022-08-22 14:07:13 +00:00
|
|
|
const isExpressPaymentMethodActive = useSelect( ( select ) =>
|
2022-10-06 12:46:46 +00:00
|
|
|
select( PAYMENT_STORE_KEY ).isExpressPaymentMethodActive()
|
2022-08-22 14:07:13 +00:00
|
|
|
);
|
2022-04-08 12:11:50 +00:00
|
|
|
|
|
|
|
const { notices } = useSelect( ( select ) => {
|
|
|
|
const store = select( 'core/notices' );
|
|
|
|
return {
|
|
|
|
notices: store.getNotices( context ),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
const { removeNotice } = useDispatch( 'core/notices' );
|
|
|
|
const regularNotices = notices
|
|
|
|
.filter( ( notice ) => notice.type !== 'snackbar' )
|
|
|
|
.concat( additionalNotices );
|
2020-03-16 20:57:12 +00:00
|
|
|
|
|
|
|
if ( ! regularNotices.length ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-26 21:00:54 +00:00
|
|
|
const wrapperClass = classnames( className, 'wc-block-components-notices' );
|
|
|
|
|
2022-08-22 14:07:13 +00:00
|
|
|
// We suppress the notices when the express payment method is active
|
|
|
|
return isExpressPaymentMethodActive ? null : (
|
2020-03-16 20:57:12 +00:00
|
|
|
<div className={ wrapperClass }>
|
|
|
|
{ regularNotices.map( ( props ) => (
|
|
|
|
<Notice
|
2022-09-23 18:48:28 +00:00
|
|
|
key={ `store-notice-${ props.id }` }
|
2020-03-16 20:57:12 +00:00
|
|
|
{ ...props }
|
|
|
|
className={ classnames(
|
|
|
|
'wc-block-components-notices__notice',
|
|
|
|
getWooClassName( props )
|
|
|
|
) }
|
|
|
|
onRemove={ () => {
|
|
|
|
if ( props.isDismissible ) {
|
2022-04-08 12:11:50 +00:00
|
|
|
removeNotice( props.id, context );
|
2020-03-16 20:57:12 +00:00
|
|
|
}
|
|
|
|
} }
|
|
|
|
>
|
2022-10-05 09:59:38 +00:00
|
|
|
{ sanitizeHTML( props.content ) }
|
2020-03-16 20:57:12 +00:00
|
|
|
</Notice>
|
|
|
|
) ) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-23 18:48:28 +00:00
|
|
|
export default StoreNoticesContainer;
|