summaryrefslogtreecommitdiff
path: root/frontend/app/views/graph/graph.container.js
blob: 57ea205dfcccb03adcd81dd75ee071a2f98eb15e (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import './graph.css'

import actions from 'app/actions'
import { Loader } from 'app/common'

// import * as uploadActions from './upload.actions'

import PageNew from './components/page.new'
import PageEdit from './components/page.edit'

import GraphHeader from './components/graph.header'
import GraphEditor from './components/graph.editor'
import AudioList from './components/audio.list'
import CursorList from './components/cursor.list'

class GraphContainer extends Component {
  componentDidMount() {
    if (this.shouldShowGraph()) this.load()
  }
  componentDidUpdate(prevProps) {
    if (this.shouldLoadGraph(prevProps)) this.load()
  }
  shouldShowGraph() {
    const { graph_name, page_name } = this.props.match.params
    return (graph_name && !page_name && graph_name !== 'index')
  }
  shouldLoadGraph(prevProps) {
    const { graph, location } = this.props
    const { key } = location
    if (key === prevProps.location.key) return false
    if (!this.shouldShowGraph()) return false
    return (graph.show.name === prevProps.graph.show.name)
  }
  load() {
    actions.site.setSiteTitle("loading " + this.props.match.params.graph_name + "...")
    actions.graph.show('name/' + this.props.match.params.graph_name)
    .then(data => {
      actions.site.setSiteTitle(data.res.title)
    })
  }
  render() {
    if (!this.shouldShowGraph()) return <div />
    if (!this.props.graph.show.res || this.props.graph.show.loading) {
      return (
        <div>
          <GraphHeader />
          <div className='body'>
            <div className='graph loading'>
              <Loader />
            </div>
          </div>
        </div>
      )
    }
    return (
      <div>
        <GraphHeader />
        <div className='body'>
          <GraphEditor />
          <div className='sidebar'>
            {this.props.graph.editor.addingPage && <PageNew />}
            {this.props.graph.editor.editingPage && <PageEdit />}
            {this.props.graph.editor.showingAudio && <AudioList />}
            {this.props.graph.editor.showingCursors && <CursorList />}
          </div>
        </div>
      </div>
    )
  }
}

// <Route exact path='/:graph_name' component={GraphView} />
const mapStateToProps = state => ({
  graph: state.graph,
})

const mapDispatchToProps = dispatch => ({
  // uploadActions: bindActionCreators({ ...uploadActions }, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(GraphContainer)