Created
April 14, 2016 22:11
-
-
Save iammerrick/192efabfa50ed826907bd6da1133f712 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Lock extends React.Component { | |
componentDidMount() { | |
this.props.load(); | |
} | |
render() { | |
if (this.props.isLocked) return this.props.lockedView; | |
return React.Children.only(this.props.children); | |
} | |
} | |
connect((state, props) => ({ | |
isLocked: isUserOneOfRoles(state, props.forbid) | |
}), { load }); | |
// Usage | |
<Lock forbid={[ROLES.SOCIAL]} lockedView={<SomeLockedView />}> | |
<TheSecretPage /> | |
</Lock> | |
// If you don't want to repeat the lock you can configure it at a higher level | |
class ConfiguredLock extend React.Component { | |
render() { | |
return ( | |
<Lock forbid={[ROLES.SOCIAL} lockedView={<SomeLockedView />}> | |
{this.props.children} | |
</Lock> | |
); | |
} | |
} | |
// Now if you want to lock on a specific role and fall to a specific view you can accordingly without repeatign yourself | |
<ConfiguredLock> | |
<SomePage /> | |
</ConfiguredLock> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment