All files / lib index.js

5.26% Statements 3/57
0% Branches 0/32
0% Functions 0/16
5.88% Lines 3/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155            1x           1x                                                                                                                                                                                                                                                                   1x                        
import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import reactRouterFetch from 'react-router-fetch'
import { toggleAppFetching, getFetching } from '../redux/is-app-fetching'
import ReactDOM, { unstable_renderSubtreeIntoContainer as renderSubtreeIntoContainer } from 'react-dom'
 
const FetchingIndicatorWrapper = ({ Indicator, shouldShow, ...rest }) => (
  <div>
    {shouldShow && React.cloneElement(Indicator, { shouldShow, ...rest })}
  </div>
)
 
const TransitionManager = class TransitionManager extends Component {
 
  static contextTypes = {
    store: PropTypes.object.isRequired
  }
 
  static propTypes = {
    onFetchStart: PropTypes.func,
    onFetchEnd: PropTypes.func,
    onError: PropTypes.func,
    fetchInitial: PropTypes.bool,
    showIndicatorOnInitial: PropTypes.bool,
    FetchingIndicator: PropTypes.element,
    ErrorIndicator: PropTypes.element,
    SplashScreen: PropTypes.element
  }
 
  state = {
    isAppFetching: false
  }
 
  componentWillMount () {
    const { fetchInitial } = this.props
    if (fetchInitial) this.fetchRoutes(this.props)
  }
 
  componentDidMount () {
    const { fetchInitial, showIndicatorOnInitial } = this.props
    this.node = document.createElement('div')
    document.body.appendChild(this.node)
    if (this.state.isAppFetching && fetchInitial && showIndicatorOnInitial) this.renderLoading(true)
  }
 
  componentWillReceiveProps (nextProps) {
    const current = `${this.props.location.pathname}${this.props.location.search}`
    const next = `${nextProps.location.pathname}${nextProps.location.search}`
    const { isAppFetching, onError } = nextProps
    if (isAppFetching.error && onError) onError(isAppFetching.payload)
    if (current === next) {
      return
    }
    this.fetchRoutes(nextProps)
    this.renderLoading(true)
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !nextState.isAppFetching
  }
 
  componentWillUnmount () {
    if (this.node) {
      ReactDOM.unmountComponentAtNode(this.node)
      document.body.removeChild(this.node)
    }
    this.portal = null
    this.node = null
  }
 
  renderLoading (shoudShow) {
    const { FetchingIndicator } = this.props
    if (shoudShow) {
      document.body.classList.add('TransitionManager-body-is-fetching')
      this.portal = renderSubtreeIntoContainer(this, <FetchingIndicatorWrapper Indicator={FetchingIndicator} shouldShow={shoudShow} {...this.props} />, this.node)
    } else {
      document.body.classList.remove('TransitionManager-body-is-fetching')
      this.portal = renderSubtreeIntoContainer(this, <FetchingIndicatorWrapper Indicator={FetchingIndicator} shouldShow={shoudShow} {...this.props} />, this.node)
    }
  }
 
  fetchRoutes (nextProps) {
    const { dispatch, onFetchStart, onFetchEnd } = this.props
    if (onFetchStart) onFetchStart()
    dispatch(toggleAppFetching())
    this.setState({
      isAppFetching: !this.state.isAppFetching
    }, () => {
      reactRouterFetch({
        components: nextProps.routes.map((route) => route.component),
        params: nextProps.params,
        location: nextProps.location
      }, false, {
        dispatch,
        getState: this.context.store.getState
      })
        .then(() => {
          this.setState({
            isAppFetching: !this.state.isAppFetching
          }, () => {
            this.renderLoading(false)
            if (onFetchEnd) onFetchEnd()
            dispatch(toggleAppFetching())
          })
        },
        (err) => {
          this.setState({
            isAppFetching: !this.state.isAppFetching
          }, () => {
            this.renderLoading(false)
            if (onFetchEnd) onFetchEnd(err)
            dispatch(toggleAppFetching(err))
          })
        })
    })
  }
 
  render () {
    const { ErrorIndicator, isAppFetching, SplashScreen, fetchInitial } = this.props
    if (isAppFetching.error) {
      return (
        <div>
          {React.cloneElement(ErrorIndicator, {...this.props})}
        </div>
      )
    }
    if (fetchInitial && isAppFetching && SplashScreen) {
      return (
        <div>
          {SplashScreen}
        </div>
      )
    }
    return (
      <div>
        {this.props.children}
      </div>
    )
  }
 
}
 
const mapStateToProps = (state, ownProps) => {
  return {
    isAppFetching: getFetching(state),
    location: ownProps.location,
    params: ownProps.params,
    routes: ownProps.routes
  }
}
 
export default connect(
  mapStateToProps
)(TransitionManager)