2019-07-11 10:12:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2022-02-01 16:54:38 +00:00
|
|
|
import { Icon, warning } from '@wordpress/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.
|
|
|
|
*/
|
2023-03-08 16:22:51 +00:00
|
|
|
type: 'api' | 'general' | string;
|
2021-12-02 16:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-02-01 16:54:38 +00:00
|
|
|
icon={ <Icon icon={ warning } /> }
|
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;
|