import React from 'react';

import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom';

import { getBaseName } from '../utils/utils';

class SecurityLayout extends React.Component {
  state = {
    isReady: false,
  };

  componentDidMount() {
    this.setState({
      isReady: true,
    });
  }

  render() {
    const { children, global } = this.props;
    const isLogin = global.token !== null;
    if (
      !isLogin &&
      window.location.pathname !== `/${getBaseName()}/user/login`
    ) {
      return (
        <Redirect to={`/${getBaseName()}/user/login?client=${global.client}`} />
      );
    }
    return children;
  }
}

const mapStateToProps = state => {
  return {
    global: state.getIn(['global', 'globalConfig']),
  };
};
export default connect(
  mapStateToProps,
  null,
)(SecurityLayout);
