Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1202 | Rev 1562 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React from 'react'
import { connect } from 'react-redux'
import { Container } from '@mui/material'

import { addNotification } from '../../redux/notification/notification.actions'
import { ButtonPrimary } from '@buttons'

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props)
    this.state = { hasError: false }
  }

  static getDerivedStateFromError() {
    return { hasError: true }
  }

  componentDidCatch(error, errorInfo) {
    console.error('ErrorBoundary caught an error: ', error, errorInfo)
    this.props.addNotification({ style: 'danger', msg: error.message })
  }

  render() {
    if (this.state.hasError) {
      return (
        <Container>
          <h1>Something went wrong, please reload the page</h1>

          <ButtonPrimary
            className='mt-3'
            onClick={() => window.location.reload(true)}
            label='Reload'
          />
        </Container>
      )
    }

    return this.props.children
  }
}

const mapDispatchToProps = {
  addNotification: (notification) => addNotification(notification)
}

export default connect(null, mapDispatchToProps)(ErrorBoundary)