Commit a7981496 authored by jim's avatar jim

bug fix#862 determine if the target is instantiated

parent 5c08dabd
...@@ -5,11 +5,7 @@ class Authorized extends React.Component { ...@@ -5,11 +5,7 @@ class Authorized extends React.Component {
render() { render() {
const { children, authority, noMatch = null } = this.props; const { children, authority, noMatch = null } = this.props;
const childrenRender = typeof children === 'undefined' ? null : children; const childrenRender = typeof children === 'undefined' ? null : children;
return CheckPermissions( return CheckPermissions(authority, childrenRender, noMatch);
authority,
childrenRender,
noMatch
);
} }
} }
......
...@@ -4,16 +4,28 @@ import Authorized from './Authorized'; ...@@ -4,16 +4,28 @@ import Authorized from './Authorized';
class AuthorizedRoute extends React.Component { class AuthorizedRoute extends React.Component {
render() { render() {
const { component: Component, render, authority, const {
redirectPath, ...rest } = this.props; component: Component,
render,
authority,
redirectPath,
...rest
} = this.props;
return ( return (
<Authorized <Authorized
authority={authority} authority={authority}
noMatch={<Route {...rest} render={() => <Redirect to={{ pathname: redirectPath }} />} />} noMatch={
<Route
{...rest}
render={() => <Redirect to={{ pathname: redirectPath }} />}
/>
}
> >
<Route <Route
{...rest} {...rest}
render={props => (Component ? <Component {...props} /> : render(props))} render={props =>
(Component ? <Component {...props} /> : render(props))
}
/> />
</Authorized> </Authorized>
); );
......
...@@ -3,7 +3,11 @@ import PromiseRender from './PromiseRender'; ...@@ -3,7 +3,11 @@ import PromiseRender from './PromiseRender';
import { CURRENT } from './index'; import { CURRENT } from './index';
function isPromise(obj) { function isPromise(obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'; return (
!!obj &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function'
);
} }
/** /**
......
...@@ -6,22 +6,34 @@ export default class PromiseRender extends React.PureComponent { ...@@ -6,22 +6,34 @@ export default class PromiseRender extends React.PureComponent {
component: null, component: null,
}; };
componentDidMount() { componentDidMount() {
const ok = this.checkIsInstantiation(this.props.ok);
const error = this.checkIsInstantiation(this.props.error);
this.props.promise this.props.promise
.then(() => { .then(() => {
this.setState({ this.setState({
component: this.props.ok, component: ok,
}); });
}) })
.catch(() => { .catch(() => {
this.setState({ this.setState({
component: () => this.props.error, component: error,
}); });
}); });
} }
// Determine whether the incoming component has been instantiated
// AuthorizedRoute is already instantiated
// Authorized render is already instantiated, children is no instantiated
// Secured is not instantiated
checkIsInstantiation = (target) => {
if (!React.isValidElement(target)) {
return target;
}
return () => target;
};
render() { render() {
const C = this.state.component; const Component = this.state.component;
return C ? ( return Component ? (
<C {...this.props} /> <Component {...this.props} />
) : ( ) : (
<div <div
style={{ style={{
......
...@@ -38,11 +38,7 @@ const authorize = (authority, error) => { ...@@ -38,11 +38,7 @@ const authorize = (authority, error) => {
throw new Error('authority is required'); throw new Error('authority is required');
} }
return function decideAuthority(targer) { return function decideAuthority(targer) {
return () => CheckPermissions( return () => CheckPermissions(authority, targer, classError || Exception403);
authority,
targer,
classError || Exception403
);
}; };
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment