From e85f39ebe24bddc627c706e7bfd394de2050f528 Mon Sep 17 00:00:00 2001 From: kazukinagata Date: Mon, 27 Jul 2020 22:48:19 +0900 Subject: [PATCH] Let developers to specify reducer key name --- README.md | 8 ++++++++ src/generate-require-signin-wrapper.tsx | 12 ++++++------ src/types.ts | 9 +++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b357d54..36f5030 100644 --- a/README.md +++ b/README.md @@ -185,8 +185,16 @@ import SomeProtectedPageComponent from './components/SomeProtectedPageComponent' import SignInPage from './components/SignInPage' import { generateRequireSignInWrapper } from 'redux-token-auth' +// In this example we set `reduxTokenAuth` as reducer key. +// You can set other key name as you like. +const mapStateToProps = (state) => ({ + hasVerificationBeenAttempted: state.reduxTokenAuth.currentUser.hasVerificationBeenAttempted, + isSignedIn: state.reduxTokenAuth.currentUser.isSignedIn +}) + const requireSignIn = generateRequireSignInWrapper({ redirectPathIfNotSignedIn: '/signin', + mapStateToProps, }) const history = createBrowserHistory({}) diff --git a/src/generate-require-signin-wrapper.tsx b/src/generate-require-signin-wrapper.tsx index 8cb802c..f8f1101 100644 --- a/src/generate-require-signin-wrapper.tsx +++ b/src/generate-require-signin-wrapper.tsx @@ -2,18 +2,18 @@ import * as React from 'react' import { connect, ConnectedProps } from 'react-redux' import { GenerateRequireSignInWrapperConfig, - ReduxState, + // ReduxState, } from './types' const generateRequireSignInWrapper = ( - { redirectPathIfNotSignedIn }: GenerateRequireSignInWrapperConfig + { 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 mapStateToProps = (state: ReduxState) => ({ + // hasVerificationBeenAttempted: state.reduxTokenAuth.currentUser.hasVerificationBeenAttempted, + // isSignedIn: state.reduxTokenAuth.currentUser.isSignedIn + // }) const connector = connect(mapStateToProps) type PropsFromRedux = ConnectedProps diff --git a/src/types.ts b/src/types.ts index 1daee96..4d987a6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -19,9 +19,9 @@ export interface ReduxTokenAuthState { readonly currentUser: User } -export interface ReduxState { - readonly reduxTokenAuth: ReduxTokenAuthState -} +// export interface ReduxState { +// readonly reduxTokenAuth: ReduxTokenAuthState +// } export interface AuthHeaders { readonly 'access-token': string @@ -182,7 +182,7 @@ export type ReduxAction = RegistrationRequestSentAction export type AppThunk> = ThunkAction< ReturnType, - ReduxState, + object, unknown, Action > @@ -207,6 +207,7 @@ export interface SingleLayerStringMap { export interface GenerateRequireSignInWrapperConfig { readonly redirectPathIfNotSignedIn: string + readonly mapStateToProps: (state: object) => {hasVerificationBeenAttempted: boolean, isSignedIn: boolean} } // export type RequireSignInWrapper = (PageComponent: ComponentClass) => ComponentClass