2019-08-30 09:36:06 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { escapeHTML } from '@wordpress/escape-html';
|
|
|
|
|
2019-09-04 16:07:00 +00:00
|
|
|
const getErrorMessage = ( { message, type } ) => {
|
|
|
|
if ( ! message ) {
|
2019-09-05 15:09:31 +00:00
|
|
|
return __(
|
|
|
|
'An unknown error occurred which prevented the block from being updated.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
);
|
2019-08-30 09:36:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 16:07:00 +00:00
|
|
|
if ( type === 'general' ) {
|
|
|
|
return (
|
|
|
|
<span>
|
2019-09-05 15:09:31 +00:00
|
|
|
{ __(
|
|
|
|
'The following error was returned',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-09-04 16:07:00 +00:00
|
|
|
<br />
|
|
|
|
<code>{ escapeHTML( message ) }</code>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( type === 'api' ) {
|
2019-08-30 09:36:06 +00:00
|
|
|
return (
|
|
|
|
<span>
|
2019-09-05 15:09:31 +00:00
|
|
|
{ __(
|
|
|
|
'The following error was returned from the API',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-08-30 09:36:06 +00:00
|
|
|
<br />
|
2019-09-04 16:07:00 +00:00
|
|
|
<code>{ escapeHTML( message ) }</code>
|
2019-08-30 09:36:06 +00:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-04 16:07:00 +00:00
|
|
|
return message;
|
2019-08-30 09:36:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const ErrorMessage = ( { error } ) => (
|
2019-09-05 15:09:31 +00:00
|
|
|
<div className="wc-block-error-message">{ getErrorMessage( error ) }</div>
|
2019-08-30 09:36:06 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
ErrorMessage.propTypes = {
|
|
|
|
/**
|
|
|
|
* The error object.
|
|
|
|
*/
|
|
|
|
error: PropTypes.shape( {
|
|
|
|
/**
|
2019-09-04 16:07:00 +00:00
|
|
|
* Human-readable error message to display.
|
2019-08-30 09:36:06 +00:00
|
|
|
*/
|
2019-09-04 16:07:00 +00:00
|
|
|
message: PropTypes.node,
|
2019-08-30 09:36:06 +00:00
|
|
|
/**
|
2019-09-04 16:07:00 +00:00
|
|
|
* Context in which the error was triggered. That will determine how the error is displayed to the user.
|
2019-08-30 09:36:06 +00:00
|
|
|
*/
|
2019-09-04 16:07:00 +00:00
|
|
|
type: PropTypes.oneOf( [ 'api', 'general' ] ),
|
2019-08-30 09:36:06 +00:00
|
|
|
} ),
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ErrorMessage;
|