Let developers to specify reducer key name

This commit is contained in:
kazukinagata 2020-07-27 22:48:19 +09:00
parent 255ee70ab6
commit e85f39ebe2
3 changed files with 19 additions and 10 deletions

View File

@ -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({})

View File

@ -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<typeof connector>

View File

@ -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<ReturnType = Promise<void>> = ThunkAction<
ReturnType,
ReduxState,
object,
unknown,
Action<string>
>
@ -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