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.
|
|
|
|
*/
|
2023-11-02 13:01:42 +00:00
|
|
|
onRetry?: ( () => void ) | undefined;
|
2021-12-02 16:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ErrorPlaceholder = ( {
|
2023-11-02 13:01:42 +00:00
|
|
|
className = '',
|
2021-12-02 16:56:03 +00:00
|
|
|
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 } /> }
|
2023-12-12 23:05:20 +00:00
|
|
|
label={ __( 'Sorry, an error occurred', 'woocommerce' ) }
|
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 />
|
|
|
|
) : (
|
2023-12-08 23:30:33 +00:00
|
|
|
<Button variant="secondary" onClick={ onRetry }>
|
2023-12-12 22:12:36 +00:00
|
|
|
{ __( 'Retry', 'woocommerce' ) }
|
2019-08-02 11:56:53 +00:00
|
|
|
</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;
|