woocommerce/plugins/woocommerce-blocks/assets/js/base/utils/create-notice.ts

95 lines
2.7 KiB
TypeScript
Raw Normal View History

New contexts for `StoreNoticesContainer` and notice grouping (https://github.com/woocommerce/woocommerce-blocks/pull/7711) * Refactor Store Notices Move snackbar hiding filter before notice creation Implements showApplyCouponNotice Refactor context providers Use STORE_NOTICE_CONTEXTS use refs to track notice containers Refactor ref usage Use existing noticeContexts * Move new notice code to checkout package * Combine store and snackbars * Update noticeContexts imports * Remove context provider * Update data store * Fix 502 * Add new error contexts * Force types * Unnecessary reorder of imports * Fix global handling * Document forceType * Optional props are undefined * Remove function name * Missing condition * Remove context prop * Define ACTION_TYPES * Remove controls * Update assets/js/base/context/event-emit/utils.ts Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * CONTACT_INFORMATION * Remove ref from registerContainer * Abstract container locating methods * pass context correctly when displaying notices * Remove debugging buttons * Update filter usage - remove useMemo so filter can work inline * Refactor existing error notices from the API (https://github.com/woocommerce/woocommerce-blocks/pull/7728) * Update API type defs * Move create notice utils * Replace useCheckoutNotices with new contexts * processCheckoutResponseHeaders should check headers are defined * Scroll to error notices only if we're not editing a field * Error handling utils * processErrorResponse when pushing changes * processErrorResponse when processing checkout * remove formatStoreApiErrorMessage * Add todo for cart errors * Remove unused deps * unused imports * Fix linting warnings * Unused dep * Update assets/js/types/type-defs/api-response.ts Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Add todo * Use generic * remove const * Update array types * Phone should be in address blocks Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update store name to wc/store/store-notices * Fix assertResponseIsValid * Funnel woocommerce_rest_invalid_email_address to the correct place * woocommerce_rest_missing_email_address * Move comments around * Move data back into const * Spacing * Remove spacing * Remove forced snack bar and styling * Move notices within wrapper * Remove type * hasStoreNoticesContainer rename * Group by status/context * Remove global context * Remove white space * remove changes to simplify diff * white space * Move comment to typescript * List style * showApplyCouponNotice docs * See if scrollIntoView exists * fix notice tests Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com>
2022-12-19 15:30:13 +00:00
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import type { Options as NoticeOptions } from '@wordpress/notices';
import { select, dispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { noticeContexts } from '../context/event-emit/utils';
export const DEFAULT_ERROR_MESSAGE = __(
'Something went wrong. Please contact us to get assistance.',
'woo-gutenberg-products-block'
);
export const hasStoreNoticesContainer = ( container: string ): boolean => {
const containers = select( 'wc/store/store-notices' ).getContainers();
return containers.includes( container );
};
const findParentContainer = ( container: string ): string => {
if ( container.includes( noticeContexts.CHECKOUT + '/' ) ) {
return noticeContexts.CHECKOUT;
}
if ( container.includes( noticeContexts.CART + '/' ) ) {
return hasStoreNoticesContainer( noticeContexts.CART )
? noticeContexts.CART
: noticeContexts.CHECKOUT;
}
return container;
};
/**
* Wrapper for @wordpress/notices createNotice.
*
* This is used to create the correct type of notice based on the provided context, and to ensure the notice container
* exists first, otherwise it uses the default context instead.
*/
export const createNotice = (
status: 'error' | 'warning' | 'info' | 'success',
message: string,
options: Partial< NoticeOptions >
) => {
const noticeContext = options?.context;
const suppressNotices =
select( 'wc/store/payment' ).isExpressPaymentMethodActive();
if ( suppressNotices || noticeContext === undefined ) {
return;
}
const { createNotice: dispatchCreateNotice } = dispatch( 'core/notices' );
dispatchCreateNotice( status, message, {
isDismissible: true,
...options,
context: hasStoreNoticesContainer( noticeContext )
? noticeContext
: findParentContainer( noticeContext ),
} );
};
/**
* Creates a notice only if the Store Notice Container is visible.
*/
export const createNoticeIfVisible = (
status: 'error' | 'warning' | 'info' | 'success',
message: string,
options: Partial< NoticeOptions >
) => {
if ( options?.context && hasStoreNoticesContainer( options.context ) ) {
createNotice( status, message, options );
}
};
/**
* Remove notices from all contexts.
*
* @todo Remove this when supported in Gutenberg.
* @see https://github.com/WordPress/gutenberg/pull/44059
*/
export const removeAllNotices = () => {
const containers = select( 'wc/store/store-notices' ).getContainers();
const { removeNotice } = dispatch( 'core/notices' );
const { getNotices } = select( 'core/notices' );
containers.forEach( ( container ) => {
getNotices( container ).forEach( ( notice ) => {
removeNotice( notice.id, container );
} );
} );
};