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

import { Loader } from 'app/common'

import actions from 'app/actions'

class EditorGate extends Component {
  constructor(props) {
    super(props)
    this.load()
  }

  componentDidMount() {
    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
    const episode = this.props.episode.lookup[this.props.episode_id]
    const project = this.props.project.lookup[episode.project_id]
    actions.site.loadEpisode(project, episode)
  }

  render() {
    if (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.id,
})

export default connect(mapStateToProps)(EditorGate)