2023-01-19 16:40:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { ApiErrorResponse, isApiErrorResponse } from '@woocommerce/types';
|
2023-01-30 15:12:17 +00:00
|
|
|
import { createNotice } from '@woocommerce/base-utils';
|
2023-01-19 16:40:52 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used to notify the user of cart item errors/conflicts
|
|
|
|
*/
|
|
|
|
export const notifyCartErrors = (
|
|
|
|
errors: ApiErrorResponse[] | null = null,
|
|
|
|
oldErrors: ApiErrorResponse[] | null = null
|
|
|
|
) => {
|
|
|
|
if ( oldErrors ) {
|
|
|
|
oldErrors.forEach( ( error ) => {
|
|
|
|
dispatch( 'core/notices' ).removeNotice( error.code, 'wc/cart' );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( errors !== null ) {
|
|
|
|
errors.forEach( ( error ) => {
|
|
|
|
if ( isApiErrorResponse( error ) ) {
|
|
|
|
createNotice( 'error', decodeEntities( error.message ), {
|
|
|
|
id: error.code,
|
|
|
|
context: 'wc/cart',
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|