import * as React from 'react';
import { useAccess, Access, Redirect } from 'umi';
// @ts-ignore
import { stringify } from 'qs';

export default (props:any) => {
  const access = useAccess();
  const { location = {} } = props;
  const { pathname, query } = location;
  let redirect = '/passport/sign-in';
  if (pathname) {
    redirect += `?back_url=${pathname}`;
  }
  if (query) {
    redirect += `&${stringify(query)}`;
  }


  return (
    <Access
      accessible={access.canUsePortal}
      // @ts-ignore
      fallback={<Redirect to={redirect} />}
    >
      {props.children}
    </Access>
  );
};
