| 1 |
import Dialog from 'elementor-app/ui/dialog/dialog'; |
| 2 |
|
| 3 |
// In the current time there is no solution to use "getDerivedStateFromError" static method with functional component |
| 4 |
// That is why this component is a class component. |
| 5 |
// @link https://reactjs.org/docs/hooks-faq.html#do-hooks-cover-all-use-cases-for-classes |
| 6 |
export default class ErrorBoundary extends React.Component { |
| 7 |
static propTypes = { |
| 8 |
children: PropTypes.any, |
| 9 |
title: PropTypes.string, |
| 10 |
text: PropTypes.string, |
| 11 |
learnMoreUrl: PropTypes.string, |
| 12 |
}; |
| 13 |
|
| 14 |
static defaultProps = { |
| 15 |
title: __( 'App could not be loaded', 'elementor' ), |
| 16 |
text: __( 'We’re sorry, but something went wrong. Click on ‘Learn more’ and follow each of the steps to quickly solve it.', 'elementor' ), |
| 17 |
learnMoreUrl: 'https://go.elementor.com/app-general-load-issue/', |
| 18 |
}; |
| 19 |
|
| 20 |
constructor( props ) { |
| 21 |
super( props ); |
| 22 |
|
| 23 |
this.state = { |
| 24 |
hasError: null, |
| 25 |
}; |
| 26 |
} |
| 27 |
|
| 28 |
static getDerivedStateFromError() { |
| 29 |
return { hasError: true }; |
| 30 |
} |
| 31 |
|
| 32 |
goBack() { |
| 33 |
// If the app was opened inside an iframe, it will close it, |
| 34 |
// if not, it will redirect to the last location. |
| 35 |
if ( window.top !== window.self ) { |
| 36 |
window.top.$e.run( 'app/close' ); |
| 37 |
} |
| 38 |
|
| 39 |
window.location = elementorAppConfig.return_url; |
| 40 |
} |
| 41 |
|
| 42 |
render() { |
| 43 |
if ( this.state.hasError ) { |
| 44 |
return <Dialog |
| 45 |
title={ this.props.title } |
| 46 |
text={ this.props.text } |
| 47 |
approveButtonUrl={ this.props.learnMoreUrl } |
| 48 |
approveButtonColor="link" |
| 49 |
approveButtonTarget="_blank" |
| 50 |
approveButtonText={ __( 'Learn More', 'elementor' ) } |
| 51 |
dismissButtonText={ __( 'Go Back', 'elementor' ) } |
| 52 |
dismissButtonOnClick={ this.goBack } |
| 53 |
/>; |
| 54 |
} |
| 55 |
|
| 56 |
return this.props.children; |
| 57 |
} |
| 58 |
} |
| 59 |
|