2020-03-03 10:26:02 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import PropTypes from 'prop-types';
|
2020-05-14 23:55:22 +00:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContext,
|
|
|
|
useCallback,
|
|
|
|
useState,
|
|
|
|
} from '@wordpress/element';
|
2020-03-03 10:26:02 +00:00
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
2020-03-16 20:57:12 +00:00
|
|
|
import {
|
|
|
|
StoreNoticesContainer,
|
|
|
|
SnackbarNoticesContainer,
|
|
|
|
} from '@woocommerce/base-components/store-notices-container';
|
2020-03-03 10:26:02 +00:00
|
|
|
|
2020-05-10 23:41:10 +00:00
|
|
|
/**
|
|
|
|
* @typedef {import('@woocommerce/type-defs/contexts').NoticeContext} NoticeContext
|
|
|
|
*/
|
|
|
|
|
2020-03-03 10:26:02 +00:00
|
|
|
const StoreNoticesContext = createContext( {
|
2020-03-10 13:39:21 +00:00
|
|
|
notices: [],
|
2020-04-06 20:36:19 +00:00
|
|
|
createNotice: ( status, text, props ) => void { status, text, props },
|
2020-05-10 23:41:10 +00:00
|
|
|
createSnackbarNotice: ( content, options ) => void { content, options },
|
2020-04-30 09:43:56 +00:00
|
|
|
removeNotice: ( id, ctxt ) => void { id, ctxt },
|
2020-05-15 11:09:36 +00:00
|
|
|
setIsSuppressed: ( val ) => void { val },
|
2020-03-10 13:39:21 +00:00
|
|
|
context: 'wc/core',
|
2020-03-03 10:26:02 +00:00
|
|
|
} );
|
|
|
|
|
2020-05-10 23:41:10 +00:00
|
|
|
/**
|
|
|
|
* Returns the notices context values.
|
|
|
|
*
|
|
|
|
* @return {NoticeContext} The notice context value from the notice context.
|
|
|
|
*/
|
2020-03-03 10:26:02 +00:00
|
|
|
export const useStoreNoticesContext = () => {
|
|
|
|
return useContext( StoreNoticesContext );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides an interface for blocks to add notices to the frontend UI.
|
|
|
|
*
|
|
|
|
* Statuses map to https://github.com/WordPress/gutenberg/tree/master/packages/components/src/notice
|
|
|
|
* - Default (no status)
|
|
|
|
* - Error
|
|
|
|
* - Warning
|
|
|
|
* - Info
|
|
|
|
* - Success
|
|
|
|
*/
|
2020-03-10 13:39:21 +00:00
|
|
|
export const StoreNoticesProvider = ( {
|
2020-03-03 10:26:02 +00:00
|
|
|
children,
|
|
|
|
className = '',
|
|
|
|
createNoticeContainer = true,
|
|
|
|
context = 'wc/core',
|
|
|
|
} ) => {
|
|
|
|
const { createNotice, removeNotice } = useDispatch( 'core/notices' );
|
2020-05-14 23:55:22 +00:00
|
|
|
const [ isSuppressed, setIsSuppressed ] = useState( false );
|
2020-03-03 10:26:02 +00:00
|
|
|
|
|
|
|
const createNoticeWithContext = useCallback(
|
|
|
|
( status = 'default', content = '', options = {} ) => {
|
|
|
|
createNotice( status, content, {
|
|
|
|
...options,
|
|
|
|
context: options.context || context,
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
[ createNotice, context ]
|
|
|
|
);
|
|
|
|
|
|
|
|
const removeNoticeWithContext = useCallback(
|
2020-04-30 09:43:56 +00:00
|
|
|
( id, ctxt = context ) => {
|
|
|
|
removeNotice( id, ctxt );
|
2020-03-03 10:26:02 +00:00
|
|
|
},
|
2020-05-10 23:41:10 +00:00
|
|
|
[ removeNotice, context ]
|
2020-03-03 10:26:02 +00:00
|
|
|
);
|
|
|
|
|
2020-03-16 20:57:12 +00:00
|
|
|
const createSnackbarNotice = useCallback(
|
|
|
|
( content = '', options = {} ) => {
|
|
|
|
createNoticeWithContext( 'default', content, {
|
|
|
|
...options,
|
|
|
|
type: 'snackbar',
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
[ createNoticeWithContext ]
|
|
|
|
);
|
|
|
|
|
2020-03-03 10:26:02 +00:00
|
|
|
const { notices } = useSelect(
|
|
|
|
( select ) => {
|
|
|
|
return {
|
|
|
|
notices: select( 'core/notices' ).getNotices( context ),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
[ context ]
|
|
|
|
);
|
|
|
|
|
|
|
|
const contextValue = {
|
|
|
|
notices,
|
|
|
|
createNotice: createNoticeWithContext,
|
2020-03-16 20:57:12 +00:00
|
|
|
createSnackbarNotice,
|
2020-03-03 10:26:02 +00:00
|
|
|
removeNotice: removeNoticeWithContext,
|
|
|
|
context,
|
2020-05-14 23:55:22 +00:00
|
|
|
setIsSuppressed,
|
2020-03-03 10:26:02 +00:00
|
|
|
};
|
|
|
|
|
2020-05-14 23:55:22 +00:00
|
|
|
const noticeOutput = isSuppressed ? null : (
|
|
|
|
<StoreNoticesContainer
|
|
|
|
className={ className }
|
|
|
|
notices={ contextValue.notices }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const snackbarNoticeOutput = isSuppressed ? null : (
|
|
|
|
<SnackbarNoticesContainer />
|
|
|
|
);
|
|
|
|
|
2020-03-03 10:26:02 +00:00
|
|
|
return (
|
|
|
|
<StoreNoticesContext.Provider value={ contextValue }>
|
2020-05-14 23:55:22 +00:00
|
|
|
{ createNoticeContainer && noticeOutput }
|
2020-03-03 10:26:02 +00:00
|
|
|
{ children }
|
2020-05-14 23:55:22 +00:00
|
|
|
{ snackbarNoticeOutput }
|
2020-03-03 10:26:02 +00:00
|
|
|
</StoreNoticesContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
StoreNoticesProvider.propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
createNoticeContainer: PropTypes.bool,
|
|
|
|
children: PropTypes.node,
|
|
|
|
context: PropTypes.string,
|
|
|
|
};
|