aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/util/ErrorBoundary/index.js
blob: def01c74fa5deb2f9c6d411d79c209a28f73eb76 (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
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 (
        <div className={classes.component}>
          <h1 className={classes.title}>
            {intl.formatMessage(messages.headline)}
          </h1>
          <Button 
            label={intl.formatMessage(messages.action)}
            buttonType="inverted"
            onClick={() => location.reload()}
          />
        </div>
      );
    }

    return this.props.children;
  }
}