route-middle-page.js 1.23 KB
Newer Older
zhanghaozhe committed
1
import React, { useEffect, useState } from 'react';
zhanghaozhe committed
2
import { withRouter } from "react-router-dom";
zhanghaozhe committed
3 4
import { compose } from "redux";
import { connect } from "react-redux";
zhanghaozhe committed
5
import LoadingComponent from '@/common/Loading'
zhanghaozhe committed
6

zhanghaozhe committed
7

zhanghaozhe committed
8
function RouteMiddlePage(props) {
zhanghaozhe committed
9

zhanghaozhe committed
10 11
    const [isLoading, setLoadingState] = useState(true)

zhanghaozhe committed
12 13
    useEffect(() => {
        let {user, location, history} = props
14

zhanghaozhe committed
15 16
        if (!user.isFetching) {
            if (user.hasError) {
zhanghaozhe committed
17
                history.push('/passport', {from: location})
zhanghaozhe committed
18
                setLoadingState(false)
19
            } else {
20 21
                let {data} = user || {data: {}}
                if (data && Object.values(data).every(item => !!item)) {
zhanghaozhe committed
22
                    history.push(location.pathname)
zhanghaozhe committed
23
                    setLoadingState(false)
zhanghaozhe committed
24
                } else {
zhanghaozhe committed
25
                    history.push('/passport', {from: location})
zhanghaozhe committed
26
                }
27 28
            }
        }
zhanghaozhe committed
29
    }, [props.user.isFetching])
30

zhanghaozhe committed
31
    return (
zhanghaozhe committed
32
        <div className={'loading-route'}>
zhanghaozhe committed
33
            <LoadingComponent isLoading={isLoading}>
zhanghaozhe committed
34
                <div/>
zhanghaozhe committed
35
            </LoadingComponent>
zhanghaozhe committed
36 37
        </div>
    );
38 39
}

zhanghaozhe committed
40 41 42 43 44 45
export default compose(
    connect(
        state => ({user: state.user}),
        null
    ),
    withRouter
zhanghaozhe committed
46
)(RouteMiddlePage)