2020-03-16 20:57:12 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-02-09 15:04:02 +00:00
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
2023-01-19 16:40:52 +00:00
|
|
|
import {
|
|
|
|
PAYMENT_STORE_KEY,
|
|
|
|
STORE_NOTICES_STORE_KEY,
|
|
|
|
} from '@woocommerce/block-data';
|
|
|
|
import { getNoticeContexts } from '@woocommerce/base-utils';
|
|
|
|
import type { Notice } from '@wordpress/notices';
|
2023-02-09 15:04:02 +00:00
|
|
|
import { useMemo, useEffect } from '@wordpress/element';
|
2023-04-05 12:43:03 +00:00
|
|
|
import type { NoticeType } from '@woocommerce/types';
|
2022-08-22 14:07:13 +00:00
|
|
|
|
2020-03-16 20:57:12 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2022-12-19 15:30:13 +00:00
|
|
|
import StoreNotices from './store-notices';
|
|
|
|
import SnackbarNotices from './snackbar-notices';
|
2023-04-05 12:43:03 +00:00
|
|
|
import type { StoreNoticesContainerProps } from './types';
|
2020-03-16 20:57:12 +00:00
|
|
|
|
2023-04-05 12:43:03 +00:00
|
|
|
const formatNotices = ( notices: Notice[], context: string ): NoticeType[] => {
|
2022-12-19 15:30:13 +00:00
|
|
|
return notices.map( ( notice ) => ( {
|
|
|
|
...notice,
|
|
|
|
context,
|
2023-04-05 12:43:03 +00:00
|
|
|
} ) ) as NoticeType[];
|
2020-03-16 20:57:12 +00:00
|
|
|
};
|
|
|
|
|
2022-12-19 15:30:13 +00:00
|
|
|
const StoreNoticesContainer = ( {
|
|
|
|
className = '',
|
2023-02-09 15:04:02 +00:00
|
|
|
context = '',
|
2022-04-08 12:11:50 +00:00
|
|
|
additionalNotices = [],
|
2022-11-17 13:33:58 +00:00
|
|
|
}: StoreNoticesContainerProps ): JSX.Element | null => {
|
2023-02-09 15:04:02 +00:00
|
|
|
const { registerContainer, unregisterContainer } = useDispatch(
|
|
|
|
STORE_NOTICES_STORE_KEY
|
|
|
|
);
|
2023-01-19 16:40:52 +00:00
|
|
|
const { suppressNotices, registeredContainers } = useSelect(
|
|
|
|
( select ) => ( {
|
|
|
|
suppressNotices:
|
|
|
|
select( PAYMENT_STORE_KEY ).isExpressPaymentMethodActive(),
|
|
|
|
registeredContainers: select(
|
|
|
|
STORE_NOTICES_STORE_KEY
|
|
|
|
).getRegisteredContainers(),
|
|
|
|
} )
|
2022-08-22 14:07:13 +00:00
|
|
|
);
|
2023-02-09 15:04:02 +00:00
|
|
|
const contexts = useMemo< string[] >(
|
|
|
|
() => ( Array.isArray( context ) ? context : [ context ] ),
|
|
|
|
[ context ]
|
|
|
|
);
|
2023-01-19 16:40:52 +00:00
|
|
|
// Find sub-contexts that have not been registered. We will show notices from those contexts here too.
|
|
|
|
const allContexts = getNoticeContexts();
|
|
|
|
const unregisteredSubContexts = allContexts.filter(
|
|
|
|
( subContext: string ) =>
|
2023-02-09 13:41:18 +00:00
|
|
|
contexts.some( ( _context: string ) =>
|
|
|
|
subContext.includes( _context + '/' )
|
|
|
|
) && ! registeredContainers.includes( subContext )
|
2023-01-19 16:40:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Get notices from the current context and any sub-contexts and append the name of the context to the notice
|
|
|
|
// objects for later reference.
|
2023-04-05 12:43:03 +00:00
|
|
|
const notices = useSelect< NoticeType[] >( ( select ) => {
|
2022-12-19 15:30:13 +00:00
|
|
|
const { getNotices } = select( 'core/notices' );
|
|
|
|
|
2023-01-19 16:40:52 +00:00
|
|
|
return [
|
|
|
|
...unregisteredSubContexts.flatMap( ( subContext: string ) =>
|
|
|
|
formatNotices( getNotices( subContext ), subContext )
|
|
|
|
),
|
2023-02-09 13:41:18 +00:00
|
|
|
...contexts.flatMap( ( subContext: string ) =>
|
|
|
|
formatNotices(
|
|
|
|
getNotices( subContext ).concat( additionalNotices ),
|
|
|
|
subContext
|
|
|
|
)
|
2022-12-19 15:30:13 +00:00
|
|
|
),
|
2023-04-05 12:43:03 +00:00
|
|
|
].filter( Boolean ) as NoticeType[];
|
2022-04-08 12:11:50 +00:00
|
|
|
} );
|
2023-02-09 15:04:02 +00:00
|
|
|
|
|
|
|
// Register the container context with the parent.
|
|
|
|
useEffect( () => {
|
|
|
|
contexts.map( ( _context ) => registerContainer( _context ) );
|
|
|
|
return () => {
|
|
|
|
contexts.map( ( _context ) => unregisterContainer( _context ) );
|
|
|
|
};
|
|
|
|
}, [ contexts, registerContainer, unregisterContainer ] );
|
|
|
|
|
2023-04-05 12:43:03 +00:00
|
|
|
if ( suppressNotices ) {
|
2020-03-16 20:57:12 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:30:13 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<StoreNotices
|
|
|
|
className={ className }
|
|
|
|
notices={ notices.filter(
|
|
|
|
( notice ) => notice.type === 'default'
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
<SnackbarNotices
|
|
|
|
className={ className }
|
|
|
|
notices={ notices.filter(
|
|
|
|
( notice ) => notice.type === 'snackbar'
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
</>
|
2020-03-16 20:57:12 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-23 18:48:28 +00:00
|
|
|
export default StoreNoticesContainer;
|