import React, { Component } from "react";

import { connect } from "react-redux";

import { clearLaunchParams } from "../utils/helperMethods";

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

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

  componentDidCatch(error, errorInfo) {
    console.error("Uncaught error:", error, errorInfo);
  }
  componentDidUpdate(prevProps, prevState) {
    if (this.state.hasError) {
      clearLaunchParams();
      if (typeof window === "object") {
        window.location.reload();
      }
    }
  }

  render() {
    if (this.state.hasError) {
      return <div></div>;
    }

    return this.props.children;
  }
}

export default connect()(ErrorBoundary);
