From 898d54cd0034bbb2727bc5b5eaf9d5a4f2a852de Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Sun, 9 Dec 2018 13:24:17 +0100 Subject: Add React 16 didCatch/ErrorBoundary component --- src/components/util/ErrorBoundary/index.js | 54 +++++++++++++++++++++++++++++ src/components/util/ErrorBoundary/styles.js | 13 +++++++ 2 files changed, 67 insertions(+) create mode 100644 src/components/util/ErrorBoundary/index.js create mode 100644 src/components/util/ErrorBoundary/styles.js (limited to 'src/components/util/ErrorBoundary') diff --git a/src/components/util/ErrorBoundary/index.js b/src/components/util/ErrorBoundary/index.js new file mode 100644 index 000000000..def01c74f --- /dev/null +++ b/src/components/util/ErrorBoundary/index.js @@ -0,0 +1,54 @@ +import React, { Component } from 'react'; +import injectSheet from 'react-jss'; +import { defineMessages, intlShape } from 'react-intl'; + +import Button from '../../ui/Button'; + +import styles from './styles'; + +const messages = defineMessages({ + headline: { + id: 'app.errorHandler.headline', + defaultMessage: '!!!Something went wrong.', + }, + action: { + id: 'app.errorHandler.action', + defaultMessage: '!!!Reload', + }, +}); + +export default @injectSheet(styles) class ErrorBoundary extends Component { + state = { + hasError: false, + } + + static contextTypes = { + intl: intlShape, + }; + + componentDidCatch(error, info) { + this.setState({ hasError: true }); + } + + render() { + const { classes } = this.props; + const { intl } = this.context; + + if (this.state.hasError) { + return ( +
+

+ {intl.formatMessage(messages.headline)} +

+
+ ); + } + + return this.props.children; + } +} \ No newline at end of file diff --git a/src/components/util/ErrorBoundary/styles.js b/src/components/util/ErrorBoundary/styles.js new file mode 100644 index 000000000..8d62767f6 --- /dev/null +++ b/src/components/util/ErrorBoundary/styles.js @@ -0,0 +1,13 @@ +export default (theme) => ({ + component: { + display: 'flex', + width: '100%', + alignItems: 'center', + justifyContent: 'center', + flexDirection: 'column', + }, + title: { + fontSize: 20, + color: theme.colorText, + } +}); -- cgit v1.2.3-70-g09d2