AuthorizedRoute.tsx 814 Bytes
Newer Older
1 2
import { Redirect, Route } from 'umi';

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
3 4 5 6
import React from 'react';
import Authorized from './Authorized';
import { IAuthorityType } from './CheckPermissions';

7
interface AuthorizedRoutePops {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
8 9
  currentAuthority: string;
  component: React.ComponentClass<any, any>;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10
  render: (props: any) => React.ReactNode;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
11 12 13 14
  redirectPath: string;
  authority: IAuthorityType;
}

15
const AuthorizedRoute: React.SFC<AuthorizedRoutePops> = ({
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  component: Component,
  render,
  authority,
  redirectPath,
  ...rest
}) => (
  <Authorized
    authority={authority}
    noMatch={<Route {...rest} render={() => <Redirect to={{ pathname: redirectPath }} />} />}
  >
    <Route
      {...rest}
      render={(props: any) => (Component ? <Component {...props} /> : render(props))}
    />
  </Authorized>
);

export default AuthorizedRoute;