aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/util/ErrorBoundary/index.js
blob: 9c789e9816a5d7da9b717eea9fa803d345245b2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import injectSheet from 'react-jss';
import { defineMessages, injectIntl } 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',
  },
});

@injectSheet(styles)
class ErrorBoundary extends Component {
  state = {
    hasError: false,
  };

  static propTypes = {
    classes: PropTypes.object.isRequired,
    children: PropTypes.node.isRequired,
  };

  componentDidCatch() {
    this.setState({ hasError: true });
  }

  render() {
    const { classes } = this.props;
    const { intl } = this.props;

    if (this.state.hasError) {
      return (
        <div className={classes.component}>
          <h1 className={classes.title}>
            {intl.formatMessage(messages.headline)}
          </h1>
          <Button
            label={intl.formatMessage(messages.action)}
            buttonType="inverted"
            onClick={() => window.location.reload()}
          />
        </div>
      );
    }

    return this.props.children;
  }
}

export default injectIntl(ErrorBoundary);