2019-07-11 10:12:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-08-02 11:56:53 +00:00
|
|
|
import { Fragment } from '@wordpress/element';
|
2019-07-11 10:12:44 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Gridicon from 'gridicons';
|
|
|
|
import classNames from 'classnames';
|
2019-08-02 11:56:53 +00:00
|
|
|
import { escapeHTML } from '@wordpress/escape-html';
|
2019-07-11 10:12:44 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Placeholder,
|
|
|
|
Spinner,
|
|
|
|
} from '@wordpress/components';
|
|
|
|
|
2019-08-02 11:56:53 +00:00
|
|
|
const getErrorMessage = ( { apiMessage, message } ) => {
|
|
|
|
if ( message ) {
|
|
|
|
return message;
|
2019-07-11 10:12:44 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 11:56:53 +00:00
|
|
|
if ( apiMessage ) {
|
2019-07-11 10:12:44 +00:00
|
|
|
return (
|
2019-08-02 11:56:53 +00:00
|
|
|
<span>
|
|
|
|
{ __( 'The following error was returned from the API', 'woo-gutenberg-products-block' ) }
|
|
|
|
<br />
|
|
|
|
<code>{ escapeHTML( apiMessage ) }</code>
|
|
|
|
</span>
|
2019-07-11 10:12:44 +00:00
|
|
|
);
|
|
|
|
}
|
2019-08-02 11:56:53 +00:00
|
|
|
|
|
|
|
return __( 'An unknown error occurred which prevented the block from being updated.', 'woo-gutenberg-products-block' );
|
|
|
|
};
|
|
|
|
|
|
|
|
const ApiErrorPlaceholder = ( { className, error, isLoading, onRetry } ) => (
|
|
|
|
<Placeholder
|
|
|
|
icon={ <Gridicon icon="notice" /> }
|
|
|
|
label={ __( 'Sorry, an error occurred', 'woo-gutenberg-products-block' ) }
|
|
|
|
className={ classNames( 'wc-block-api-error', className ) }
|
|
|
|
>
|
|
|
|
<div className="wc-block-error__message">
|
|
|
|
{ getErrorMessage( error ) }
|
|
|
|
</div>
|
|
|
|
{ onRetry && (
|
|
|
|
<Fragment>
|
|
|
|
{ isLoading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
<Button isDefault onClick={ onRetry }>
|
|
|
|
{ __( 'Retry', 'woo-gutenberg-products-block' ) }
|
|
|
|
</Button>
|
|
|
|
) }
|
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
</Placeholder>
|
|
|
|
);
|
2019-07-11 10:12:44 +00:00
|
|
|
|
|
|
|
ApiErrorPlaceholder.propTypes = {
|
|
|
|
/**
|
|
|
|
* Callback to retry an action.
|
|
|
|
*/
|
|
|
|
onRetry: PropTypes.func.isRequired,
|
|
|
|
/**
|
|
|
|
* Classname to add to placeholder in addition to the defaults.
|
|
|
|
*/
|
|
|
|
className: PropTypes.string,
|
2019-08-02 11:56:53 +00:00
|
|
|
/**
|
|
|
|
* The error object.
|
|
|
|
*/
|
|
|
|
error: PropTypes.shape( {
|
|
|
|
/**
|
|
|
|
* API error message to display in case of a missing `message`.
|
|
|
|
*/
|
|
|
|
apiMessage: PropTypes.node,
|
|
|
|
/**
|
|
|
|
* Human-readable error message to display.
|
|
|
|
*/
|
|
|
|
message: PropTypes.string,
|
|
|
|
} ),
|
|
|
|
/**
|
|
|
|
* Whether there is a request running, so the 'Retry' button is hidden and
|
|
|
|
* a spinner is shown instead.
|
|
|
|
*/
|
|
|
|
isLoading: PropTypes.bool,
|
2019-07-11 10:12:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ApiErrorPlaceholder;
|