import * as React from 'react' ; import { ComponentClass } from 'react' ; import { connect } from 'react-redux' ; import { GenerateRequireSignInWrapperConfig , ReduxState , RequireSignInWrapper , } from './types' ; const generateRequireSignInWrapper = ( { redirectPathIfNotSignedIn } ) : RequireSignInWrapper => { const requireSignInWrapper = ( PageComponent ) : ComponentClass => { interface WrapperProps { readonly hasVerificationBeenAttempted : boolean ; readonly isSignedIn : boolean ; readonly history : { readonly replace : ( path : string ) => void ; } ; } class GatedPage extends React . Component < WrapperProps > { public componentWillReceiveProps ( nextProps ) { const { history , hasVerificationBeenAttempted , isSignedIn , } = nextProps ; if ( hasVerificationBeenAttempted && ! isSignedIn ) { history . replace ( redirectPathIfNotSignedIn ) ; } } public render ( ) { const { hasVerificationBeenAttempted , isSignedIn , } = this . props ; return ( hasVerificationBeenAttempted && isSignedIn ) ? < PageComponent { ... this . props } / > : < div > < /div>; } } const mapStateToProps = ( state ) => ( { hasVerificationBeenAttempted : state . reduxTokenAuth . currentUser . hasVerificationBeenAttempted , isSignedIn : state . reduxTokenAuth . currentUser . isSignedIn } ) ; return connect ( mapStateToProps ) ( GatedPage ) ; } ; return requireSignInWrapper ; } ; export default generateRequireSignInWrapper ;