2019-02-13 11:44:58 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-05-13 13:08:31 +00:00
|
|
|
import { applyFilters } from '@wordpress/hooks';
|
2019-02-13 11:44:58 +00:00
|
|
|
import classnames from 'classnames';
|
2023-06-14 20:32:28 +00:00
|
|
|
import { WooFooterItem } from '@woocommerce/admin-layout';
|
2021-05-13 13:08:31 +00:00
|
|
|
import { OPTIONS_STORE_NAME, USER_STORE_NAME } from '@woocommerce/data';
|
2019-02-13 11:44:58 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2021-02-09 15:19:43 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2021-05-13 13:08:31 +00:00
|
|
|
import { useEffect } from '@wordpress/element';
|
2019-02-13 11:44:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-11-09 07:17:08 +00:00
|
|
|
import SnackbarList from './snackbar/list';
|
2019-02-13 11:44:58 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2021-05-13 13:08:31 +00:00
|
|
|
const QUEUE_OPTION = 'woocommerce_admin_transient_notices_queue';
|
|
|
|
const QUEUED_NOTICE_FILTER = 'woocommerce_admin_queued_notice_filter';
|
|
|
|
|
2023-06-14 20:32:28 +00:00
|
|
|
export function TransientNotices( props ) {
|
2021-06-02 17:30:32 +00:00
|
|
|
const { removeNotice: onRemove } = useDispatch( 'core/notices' );
|
2022-06-21 08:37:34 +00:00
|
|
|
const { createNotice: createNotice2, removeNotice: onRemove2 } =
|
|
|
|
useDispatch( 'core/notices2' );
|
2021-05-13 13:08:31 +00:00
|
|
|
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
|
|
|
|
const {
|
|
|
|
currentUser = {},
|
|
|
|
notices = [],
|
|
|
|
notices2 = [],
|
|
|
|
noticesQueue = {},
|
|
|
|
} = useSelect( ( select ) => {
|
2021-02-09 15:19:43 +00:00
|
|
|
// NOTE: This uses core/notices2, if this file is copied back upstream
|
|
|
|
// to Gutenberg this needs to be changed back to just core/notices.
|
2021-05-13 13:08:31 +00:00
|
|
|
return {
|
|
|
|
currentUser: select( USER_STORE_NAME ).getCurrentUser(),
|
|
|
|
notices: select( 'core/notices' ).getNotices(),
|
|
|
|
notices2: select( 'core/notices2' ).getNotices(),
|
2022-06-21 08:37:34 +00:00
|
|
|
noticesQueue:
|
|
|
|
select( OPTIONS_STORE_NAME ).getOption( QUEUE_OPTION ),
|
2021-05-13 13:08:31 +00:00
|
|
|
};
|
2021-02-09 15:19:43 +00:00
|
|
|
} );
|
|
|
|
|
2021-05-13 13:08:31 +00:00
|
|
|
const getCurrentUserNotices = () => {
|
|
|
|
return Object.values( noticesQueue ).filter(
|
|
|
|
( notice ) => notice.user_id === currentUser.id || ! notice.user_id
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const dequeueNotice = ( id ) => {
|
|
|
|
const remainingNotices = { ...noticesQueue };
|
|
|
|
delete remainingNotices[ id ];
|
|
|
|
updateOptions( {
|
|
|
|
[ QUEUE_OPTION ]: remainingNotices,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
getCurrentUserNotices().forEach( ( queuedNotice ) => {
|
2022-02-11 14:38:38 +00:00
|
|
|
/**
|
|
|
|
* Filter each transient notice.
|
|
|
|
*
|
|
|
|
* @filter woocommerce_admin_queued_notice_filter
|
|
|
|
* @param {Object} notice A transient notice.
|
|
|
|
*/
|
2021-05-13 13:08:31 +00:00
|
|
|
const notice = applyFilters( QUEUED_NOTICE_FILTER, queuedNotice );
|
|
|
|
|
2021-06-02 17:30:32 +00:00
|
|
|
createNotice2( notice.status, notice.content, {
|
2021-05-13 13:08:31 +00:00
|
|
|
onDismiss: () => {
|
|
|
|
dequeueNotice( notice.id );
|
|
|
|
},
|
|
|
|
...( notice.options || {} ),
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}, [] );
|
|
|
|
|
2021-02-09 15:19:43 +00:00
|
|
|
/**
|
|
|
|
* Combines the two notices in the component vs in the useSelect, as we don't want to
|
|
|
|
* create new object references on each useSelect call.
|
|
|
|
*/
|
|
|
|
const getNotices = () => {
|
|
|
|
return notices.concat( notices2 );
|
|
|
|
};
|
|
|
|
|
|
|
|
const { className } = props;
|
|
|
|
const classes = classnames(
|
|
|
|
'woocommerce-transient-notices',
|
|
|
|
'components-notices__snackbar',
|
|
|
|
className
|
|
|
|
);
|
2021-05-13 13:08:31 +00:00
|
|
|
const combinedNotices = getNotices();
|
2021-02-09 15:19:43 +00:00
|
|
|
|
|
|
|
return (
|
2023-06-14 20:32:28 +00:00
|
|
|
<WooFooterItem>
|
|
|
|
<SnackbarList
|
|
|
|
notices={ combinedNotices }
|
|
|
|
className={ classes }
|
|
|
|
onRemove={ onRemove }
|
|
|
|
onRemove2={ onRemove2 }
|
|
|
|
/>
|
|
|
|
</WooFooterItem>
|
2021-02-09 15:19:43 +00:00
|
|
|
);
|
2019-02-13 11:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TransientNotices.propTypes = {
|
|
|
|
/**
|
|
|
|
* Additional class name to style the component.
|
|
|
|
*/
|
|
|
|
className: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* Array of notices to be displayed.
|
|
|
|
*/
|
|
|
|
notices: PropTypes.array,
|
|
|
|
};
|