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

zhanghaozhe committed
7
function RouteMiddlePage(props) {
zhanghaozhe committed
8
  const [isLoading, setLoadingState] = useState(true)
zhanghaozhe committed
9

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

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

zhanghaozhe committed
28 29 30 31 32 33 34
  return (
    <div className={"loading-route"}>
      <LoadingComponent isLoading={isLoading}>
        <div />
      </LoadingComponent>
    </div>
  )
35 36
}

zhanghaozhe committed
37
export default compose(
zhanghaozhe committed
38 39 40
  connect((state) => ({ user: state.user }), null),
  withRouter
)(RouteMiddlePage)