summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/episode/containers/episode.new.js
blob: 2a6beaf7f71737bdc08384a7fcfa3f1e7738a17b (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
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { connect } from 'react-redux'

import { history } from 'app/store'
import actions from 'app/actions'

import EpisodeForm from '../components/episode.form'
import EpisodeMenu from '../components/episode.menu'

class EpisodeNew extends Component {
  state = {
    loading: true,
    initialData: {},
    project: {},
  }
  componentDidMount() {
    this.ready()
  }
  componentDidUpdate(prevProps) {
    if (this.props.project.lookup !== prevProps.project.lookup) {
      this.ready()
    }
  }
  ready() {
    if (!this.props.project.lookup) return
    const { project_id } = this.props.match.params
    const project = this.props.project.lookup[project_id]
    this.setState({
      loading: false,
      initialData: { project_id },
      project,
    })
    console.log(this.props.match.params.project_id)
  }

  handleSubmit(data) {
    console.log(data)
    actions.episode.create(data)
      .then(res => {
        console.log(res)
        if (res.res && res.res.id) {
          history.push('/')
        }
      })
      .catch(err => {
        console.error('error')
      })
  }

  render() {
    if (this.state.loading) {
      return (
        <div className='row formContainer' />
      )      
    }
    return (
      <div className='row formContainer'>
        <EpisodeMenu />
        <EpisodeForm
          isNew
          data={this.state.initialData}
          project={this.state.project}
          onSubmit={this.handleSubmit.bind(this)}
        />
      </div>
    )
  }
}

const mapStateToProps = state => ({
  project: state.project.index,
})

export default connect(mapStateToProps)(EpisodeNew)