summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/editor/editor.gate.js
blob: 1524b53b91436caebf76ec7c65e923e71038c437 (plain)
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
import React, { Component } from 'react'
import { connect } from 'react-redux'

import { Loader } from 'app/common'

import actions from 'app/actions'

class EditorGate extends Component {
  state = {
    ready: false,
  }

  constructor(props) {
    super(props)
  }

  componentDidMount() {
    if (parseInt(this.props.episode_id) === parseInt(this.props.current_episode_id)) {
      this.setState({ ready: true })
    } else {
      this.load()
    }
  }

  componentDidUpdate(prevProps) {
    if (
      this.props.episode_id !== prevProps.episode_id ||
      this.props.episode.lookup !== prevProps.episode.lookup ||
      this.props.project.lookup !== prevProps.project.lookup
    ) {
      this.load()
    }
  }

  load() {
    if (!this.props.project.lookup) return
    if (!this.props.episode.lookup) return
    if (!this.props.episode_id) return
    if (parseInt(this.props.episode_id) === parseInt(this.props.current_episode_id)) return
    this.setState({ ready: false })
    const episode = this.props.episode.lookup[this.props.episode_id]
    const project = this.props.project.lookup[episode.project_id]
    actions.site.loadEpisode(project, episode)
    .then(() => {
      this.setState({ ready: true })
    })
  }

  render() {
    if (this.state.ready && parseInt(this.props.episode_id) === parseInt(this.props.current_episode_id)) {
      return this.props.children
    }
    return (
      <div className="overview"><Loader /></div>
    )
  }
}

const mapStateToProps = state => ({
  episode: state.episode.index,
  project: state.project.index,
  current_episode_id: state.site.episode ? state.site.episode.id : 0,
})

export default connect(mapStateToProps)(EditorGate)