import React, { useEffect } from 'react';
import { Redirect, RouterTypes } from 'umi'
import { connect } from 'dva';
import { UserModelState } from '@/models/user';

import styles from './index.css';

const unauthorizedPaths = [
  '/check',
  '/register',
];

const Page: React.FC<RouterTypes & UserModelState> = ({
  isLogin,
  children,
  location: { pathname },
}) => {
  if (pathname !== '/' && !isLogin && !unauthorizedPaths.includes(pathname)) {
    return <Redirect to="/check" />
  }
  if (isLogin && unauthorizedPaths.includes(pathname)) {
    return <Redirect to="/home" />;
  }
  return (
    <>{children}</>
  );
};

export default connect(
  ({ user }: any) => ({ ...user }),
)(Page);
