2023-08-03 11:06:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2023-12-08 16:28:55 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2023-08-03 11:06:52 +00:00
|
|
|
import { CURRENT_USER_IS_ADMIN } from '@woocommerce/settings';
|
2023-11-14 16:32:53 +00:00
|
|
|
import { StoreNoticesContainer } from '@woocommerce/blocks-components';
|
2023-08-03 11:06:52 +00:00
|
|
|
import { noticeContexts } from '@woocommerce/base-context';
|
|
|
|
import { NoticeType } from '@woocommerce/types';
|
2023-12-08 16:28:55 +00:00
|
|
|
import {
|
|
|
|
DerivedStateReturn,
|
|
|
|
ReactError,
|
|
|
|
} from '@woocommerce/base-components/block-error-boundary/types';
|
|
|
|
|
2023-08-03 11:06:52 +00:00
|
|
|
interface PaymentMethodErrorBoundaryProps {
|
|
|
|
isEditor: boolean;
|
|
|
|
children: React.ReactNode;
|
|
|
|
}
|
2023-12-08 16:28:55 +00:00
|
|
|
|
|
|
|
class PaymentMethodErrorBoundary extends Component< PaymentMethodErrorBoundaryProps > {
|
|
|
|
state = { errorMessage: '', hasError: false };
|
|
|
|
static getDerivedStateFromError( error: ReactError ): DerivedStateReturn {
|
|
|
|
return {
|
|
|
|
errorMessage: error.message,
|
|
|
|
hasError: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { hasError, errorMessage } = this.state;
|
|
|
|
const { isEditor } = this.props;
|
|
|
|
|
|
|
|
if ( hasError ) {
|
|
|
|
let errorText = __(
|
|
|
|
'We are experiencing difficulties with this payment method. Please contact us for assistance.',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2023-12-08 16:28:55 +00:00
|
|
|
);
|
|
|
|
if ( isEditor || CURRENT_USER_IS_ADMIN ) {
|
|
|
|
if ( errorMessage ) {
|
|
|
|
errorText = errorMessage;
|
|
|
|
} else {
|
|
|
|
errorText = __(
|
|
|
|
"There was an error with this payment method. Please verify it's configured correctly.",
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2023-12-08 16:28:55 +00:00
|
|
|
);
|
|
|
|
}
|
2023-08-03 11:06:52 +00:00
|
|
|
}
|
2023-12-08 16:28:55 +00:00
|
|
|
const notices: NoticeType[] = [
|
|
|
|
{
|
|
|
|
id: '0',
|
|
|
|
content: errorText,
|
|
|
|
isDismissible: false,
|
|
|
|
status: 'error',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
return (
|
|
|
|
<StoreNoticesContainer
|
|
|
|
additionalNotices={ notices }
|
|
|
|
context={ noticeContexts.PAYMENTS }
|
|
|
|
/>
|
|
|
|
);
|
2023-08-03 11:06:52 +00:00
|
|
|
}
|
2023-12-08 16:28:55 +00:00
|
|
|
|
|
|
|
return this.props.children;
|
2023-08-03 11:06:52 +00:00
|
|
|
}
|
2023-12-08 16:28:55 +00:00
|
|
|
}
|
2023-08-03 11:06:52 +00:00
|
|
|
export default PaymentMethodErrorBoundary;
|