route-middle-page.js 1.18 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
                    setLoadingState(false)
zhanghaozhe committed
23
                } else {
zhanghaozhe committed
24
                    history.push('/passport', {from: location})
zhanghaozhe committed
25
                }
26 27
            }
        }
zhanghaozhe committed
28
    }, [props.user.isFetching])
29

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

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