summaryrefslogtreecommitdiff
path: root/frontend/views/page/page.container.js
blob: bbb1f3a6f3f7c7f12f8f3a318c05825850e646b5 (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
87
88
import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import './page.css'

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

import * as graphActions from '../graph/graph.actions'
import * as pageActions from './page.actions'

import PageEdit from '../graph/components/page.edit'
import TileNew from './components/tile.new'
import TileEdit from './components/tile.edit'

import PageHeader from './components/page.header'
import PageEditor from './components/page.editor'

class PageContainer extends Component {
  componentDidMount() {
    if (this.shouldShowPage()) this.load()
  }
  componentDidUpdate(prevProps) {
    if (this.shouldLoadPage(prevProps)) this.load()
  }
  shouldShowPage() {
    const { graph_name, page_name } = this.props.match.params
    // console.log(graph_name, page_name)
    return (graph_name && page_name && graph_name !== 'index')
  }
  shouldLoadPage(prevProps) {
    const { page, location } = this.props
    const { key } = location
    if (key === prevProps.location.key) return false
    if (!this.shouldShowPage()) return false
    return (page.show.name === prevProps.page.show.name)
  }
  load() {
    actions.site.setSiteTitle("loading " + this.props.match.params.page_name + "...")
    this.props.pageActions.showGraphAndPageIfUnloaded(this.props.match.params)
    .then(data => {
      actions.site.setSiteTitle(data.res.title)
      if (!data.res.tiles.length) {
        this.props.pageActions.showAddTileForm()
      }
    })
  }
  render() {
    if (!this.shouldShowPage()) return null
    if (!this.props.page.show.res || this.props.page.show.loading) {
      return (
        <div>
          <PageHeader />
          <div className='body'>
            <div className='page loading'>
              <Loader />
            </div>
          </div>
        </div>
      )
    }
    return (
      <div>
        <PageHeader />
        <div className='body'>
          <PageEditor />
          {this.props.graph.editor.editingPage && <PageEdit />}
          {this.props.page.editor.addingTile && <TileNew />}
          {this.props.page.editor.editingTile && <TileEdit />}
        </div>
      </div>
    )
  }
}

const mapStateToProps = state => ({
  graph: state.graph,
  page: state.page,
})

const mapDispatchToProps = dispatch => ({
  graphActions: bindActionCreators({ ...graphActions }, dispatch),
  pageActions: bindActionCreators({ ...pageActions }, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(PageContainer)