summaryrefslogtreecommitdiff
path: root/frontend/app/views/graph/components/page.edit.js
blob: 40257268108c925bbdf616dcbee425fe0b231dc9 (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 { Link } from 'react-router-dom'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

import { history } from 'app/store'
import actions from 'app/actions'
import * as siteActions from 'app/views/site/site.actions'
import * as graphActions from 'app/views/graph/graph.actions'

import { Loader } from 'app/common'

import PageForm from '../components/page.form'

class PageEdit extends Component {
  componentDidMount() {
    // actions.page.show(this.props.match.params.id)
  }

  handleSubmit(data) {
    const { path: graphPath } = this.props.graph.show.res
    const { path: oldPagePath } = this.props.page.show.res
    const { path: newPagePath } = data
    actions.page.update(data)
      .then(response => {
        // console.log(response)
        actions.site.setSiteTitle(response.res.title)
        this.props.graphActions.hideEditPageForm()
        if (oldPagePath !== newPagePath) {
          const newPath = '/' + graphPath + '/' + newPagePath
          history.push(newPath)
        }
      })
  }

  render() {
    const { show } = this.props.page
    if (show.loading || !show.res) {
      return (
        <div className='form'>
          <Loader />
        </div>
      )
    }
    return (
      <PageForm
        data={show.res}
        graph={this.props.graph.show.res}
        onSubmit={this.handleSubmit.bind(this)}
      />
    )
  }
}

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

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

export default connect(mapStateToProps, mapDispatchToProps)(PageEdit)