2019-07-11 10:12:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-01-31 18:20:33 +00:00
|
|
|
import { Icon, notice } from '@woocommerce/icons';
|
2019-07-11 10:12:44 +00:00
|
|
|
import classNames from 'classnames';
|
2019-09-05 15:09:31 +00:00
|
|
|
import { Button, Placeholder, Spinner } from '@wordpress/components';
|
2019-07-11 10:12:44 +00:00
|
|
|
|
2019-08-30 09:36:06 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-12-02 16:56:03 +00:00
|
|
|
import ErrorMessage from './error-message';
|
2019-12-12 19:16:11 +00:00
|
|
|
import './editor.scss';
|
2019-08-02 11:56:53 +00:00
|
|
|
|
2021-12-02 16:56:03 +00:00
|
|
|
export interface ErrorObject {
|
|
|
|
/**
|
|
|
|
* Human-readable error message to display.
|
|
|
|
*/
|
|
|
|
message: string;
|
|
|
|
/**
|
|
|
|
* Context in which the error was triggered. That will determine how the error is displayed to the user.
|
|
|
|
*/
|
|
|
|
type: 'api' | 'general';
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ErrorPlaceholderProps {
|
|
|
|
/**
|
|
|
|
* Classname to add to placeholder in addition to the defaults.
|
|
|
|
*/
|
|
|
|
className?: string;
|
|
|
|
/**
|
|
|
|
* The error object.
|
|
|
|
*/
|
|
|
|
error: ErrorObject;
|
|
|
|
/**
|
|
|
|
* Whether there is a request running, so the 'Retry' button is hidden and
|
|
|
|
* a spinner is shown instead.
|
|
|
|
*/
|
|
|
|
isLoading: boolean;
|
|
|
|
/**
|
|
|
|
* Callback to retry an action.
|
|
|
|
*/
|
|
|
|
onRetry?: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ErrorPlaceholder = ( {
|
|
|
|
className,
|
|
|
|
error,
|
|
|
|
isLoading = false,
|
|
|
|
onRetry,
|
|
|
|
}: ErrorPlaceholderProps ): JSX.Element => (
|
2019-08-02 11:56:53 +00:00
|
|
|
<Placeholder
|
2020-01-31 18:20:33 +00:00
|
|
|
icon={ <Icon srcElement={ notice } /> }
|
2019-09-09 10:52:48 +00:00
|
|
|
label={ __(
|
|
|
|
'Sorry, an error occurred',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-08-02 11:56:53 +00:00
|
|
|
className={ classNames( 'wc-block-api-error', className ) }
|
|
|
|
>
|
2019-08-30 09:36:06 +00:00
|
|
|
<ErrorMessage error={ error } />
|
2019-08-02 11:56:53 +00:00
|
|
|
{ onRetry && (
|
2020-12-14 11:54:34 +00:00
|
|
|
<>
|
2019-08-02 11:56:53 +00:00
|
|
|
{ isLoading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
2020-12-21 16:03:54 +00:00
|
|
|
<Button isSecondary onClick={ onRetry }>
|
2019-08-02 11:56:53 +00:00
|
|
|
{ __( 'Retry', 'woo-gutenberg-products-block' ) }
|
|
|
|
</Button>
|
|
|
|
) }
|
2020-12-14 11:54:34 +00:00
|
|
|
</>
|
2019-08-02 11:56:53 +00:00
|
|
|
) }
|
|
|
|
</Placeholder>
|
|
|
|
);
|
2019-07-11 10:12:44 +00:00
|
|
|
|
2019-09-09 12:04:54 +00:00
|
|
|
export default ErrorPlaceholder;
|