route-middle-page.js 1.34 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

15

zhanghaozhe committed
16
        if (history.action === 'POP' && history.length > 2) {
17 18 19 20
            history.goBack();
            return
        }

zhanghaozhe committed
21 22
        if (!user.isFetching) {
            if (user.hasError) {
zhanghaozhe committed
23
                history.replace('/passport', {from: location})
zhanghaozhe committed
24
                setLoadingState(false)
25
            } else {
26 27
                let {data} = user || {data: {}}
                if (data && Object.values(data).every(item => !!item)) {
zhanghaozhe committed
28
                    history.replace(location.pathname)
zhanghaozhe committed
29
                    setLoadingState(false)
zhanghaozhe committed
30
                } else {
31
                    history.replace('/passport', {from: location})
zhanghaozhe committed
32
                }
33 34
            }
        }
zhanghaozhe committed
35
    })
36

zhanghaozhe committed
37
    return (
zhanghaozhe committed
38
        <div className={'loading-route'}>
zhanghaozhe committed
39
            <LoadingComponent isLoading={isLoading}>
zhanghaozhe committed
40
                <div/>
zhanghaozhe committed
41
            </LoadingComponent>
zhanghaozhe committed
42 43
        </div>
    );
44 45
}

zhanghaozhe committed
46 47 48 49 50 51
export default compose(
    connect(
        state => ({user: state.user}),
        null
    ),
    withRouter
zhanghaozhe committed
52
)(RouteMiddlePage)