import * as React from 'react' import { connect, ConnectedProps } from 'react-redux' import { GenerateRequireSignInWrapperConfig, // ReduxState, } from './types' const generateRequireSignInWrapper = ( { redirectPathIfNotSignedIn, mapStateToProps }: GenerateRequireSignInWrapperConfig ) => { const requireSignInWrapper = (PageComponent: React.ComponentClass | React.FunctionComponent) => { // const mapStateToProps = (state: ReduxState) => ({ // hasVerificationBeenAttempted: state.reduxTokenAuth.currentUser.hasVerificationBeenAttempted, // isSignedIn: state.reduxTokenAuth.currentUser.isSignedIn // }) const connector = connect(mapStateToProps) type PropsFromRedux = ConnectedProps type Props = PropsFromRedux & { readonly history: { readonly replace: (path: string) => void } } class GatedPage extends React.Component { public componentDidUpdate() { const { history, hasVerificationBeenAttempted, isSignedIn, } = this.props if (hasVerificationBeenAttempted && !isSignedIn) { history.replace(redirectPathIfNotSignedIn) } } public render (): JSX.Element { const { hasVerificationBeenAttempted, isSignedIn, } = this.props return (hasVerificationBeenAttempted && isSignedIn) ? :
; } } return connect( mapStateToProps, )(GatedPage) } return requireSignInWrapper } export default generateRequireSignInWrapper export type GenerateRequireSignInWrapper = typeof generateRequireSignInWrapper