2020-03-03 10:26:02 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-10 13:39:21 +00:00
|
|
|
import { useStoreNoticesContext } from '@woocommerce/base-context';
|
2020-03-03 10:26:02 +00:00
|
|
|
|
|
|
|
export const useStoreNotices = () => {
|
2020-03-16 20:57:12 +00:00
|
|
|
const {
|
|
|
|
notices,
|
|
|
|
createNotice,
|
|
|
|
removeNotice,
|
|
|
|
createSnackbarNotice,
|
|
|
|
} = useStoreNoticesContext();
|
2020-03-03 10:26:02 +00:00
|
|
|
|
2020-03-16 20:57:12 +00:00
|
|
|
const noticesApi = {
|
|
|
|
addDefaultNotice: ( text, noticeProps = {} ) =>
|
|
|
|
void createNotice( 'default', text, {
|
|
|
|
...noticeProps,
|
|
|
|
} ),
|
|
|
|
addErrorNotice: ( text, noticeProps = {} ) =>
|
|
|
|
void createNotice( 'error', text, {
|
|
|
|
...noticeProps,
|
|
|
|
} ),
|
|
|
|
addWarningNotice: ( text, noticeProps = {} ) =>
|
|
|
|
void createNotice( 'warning', text, {
|
|
|
|
...noticeProps,
|
|
|
|
} ),
|
|
|
|
addInfoNotice: ( text, noticeProps = {} ) =>
|
|
|
|
void createNotice( 'info', text, {
|
|
|
|
...noticeProps,
|
|
|
|
} ),
|
|
|
|
addSuccessNotice: ( text, noticeProps = {} ) =>
|
|
|
|
void createNotice( 'success', text, {
|
|
|
|
...noticeProps,
|
|
|
|
} ),
|
|
|
|
removeNotices: ( type = null ) => {
|
|
|
|
notices.map( ( notice ) => {
|
|
|
|
if ( type === null || notice.status === type ) {
|
|
|
|
removeNotice( notice.id );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
removeNotice,
|
|
|
|
addSnackbarNotice: ( text, noticeProps = {} ) => {
|
|
|
|
createSnackbarNotice( text, noticeProps );
|
|
|
|
},
|
|
|
|
};
|
2020-03-03 10:26:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
notices,
|
|
|
|
...noticesApi,
|
|
|
|
};
|
|
|
|
};
|