import React from "react";
import { authenticationService } from "../_services";
import { MathJax } from "better-react-mathjax";

export default class ErrorBoundaries extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            hasError: false,
            errorMessage: ""
        }
    }
    componentDidCatch(error, errorInfo) {
        this.setState({ hasError: true });
        this.setState({ errorMessage: error.message });
        //Do something with err and errorInfo
        console.log(error);
        authenticationService.refreshFireToken().then(() => {
            this.forceUpdate();
        });
    }
    render() {
        if (this.state?.hasError) {
            return (
                <div className="divClass">
                    {this.state.errorMessage}
                    <br/>
                    <p>Something went wrong, please try again.</p>
                </div>
            )
        }
        return (<MathJax>{this.props.children}</MathJax>);
    }
}