privateRoute.js 1000 Bytes
Newer Older
zhanghaozhe committed
1
import React, { useState, useEffect } from 'react';
2
import { Route } from "react-router-dom";
3
import { connect } from "react-redux";
zhanghaozhe committed
4
import RouteMiddlePage from './route-middle-page'
zhanghaozhe committed
5
import Loading from '@/common/Loading'
6 7 8


const PrivateRoute = ({component: Component, path, user, ...rest}) => {
9

zhanghaozhe committed
10 11 12 13 14 15
    const [isLoading, setLoadingState] = useState(true)
    const [authenticated, setAuthorization] = useState(false)

    useEffect(() => {
        let _auth = !user.hasError && user.code != 4040
        typeof _auth !== 'undefined' && (setAuthorization(_auth) , setLoadingState(false))
FE committed
16
    }, [user.hasError])
zhanghaozhe committed
17

18

19
    return (
zhanghaozhe committed
20

21
        <Route {...rest} render={props => {
zhanghaozhe committed
22 23 24 25 26
            return <Loading isLoading={isLoading}>
                {authenticated
                    ? <Component {...props}/>
                    : <RouteMiddlePage state={{from: props.location}}/>}
            </Loading>
27
        }}/>
zhanghaozhe committed
28
    )
29 30 31 32 33 34
};

export default connect(
    state => ({user: state.user}),
    null
)(PrivateRoute);