Skip to content

Instantly share code, notes, and snippets.

@madeinfree
Created September 15, 2017 13:23
Show Gist options
  • Save madeinfree/270b838df80989ddd175b4fdde81cf3a to your computer and use it in GitHub Desktop.
Save madeinfree/270b838df80989ddd175b4fdde81cf3a to your computer and use it in GitHub Desktop.
Let you easy to manage your component with auth
import * as React from 'react';
import {
Redirect
} from 'react-router-dom';
import {
connect
} from 'react-redux';
import {
compose,
branch,
renderComponent
} from 'recompose';
interface Props {
authorizationField: boolean
}
interface unAuthorizationComponentType {
unAuthorizationComponent: React.Factory<{}>
}
interface redirectComponentType {
ignorePathnane: string,
redirectTo: string
}
const IsAuthorization: React.SFC<Props> = (props) => null
export {
IsAuthorization
}
export default compose(
branch(
(props: Props) => props.authorizationField,
renderComponent((props: redirectComponentType) => {
if (location.pathname !== props.ignorePathnane) {
return <Redirect to={props.redirectTo} />
} else {
return null
}
}),
renderComponent((props: unAuthorizationComponentType) => props.unAuthorizationComponent())
),
)(IsAuthorization)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment