summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/media/containers/media.new.js
blob: c193c2f5e0ef4bf3c6e441ff7b8253b0b1287726 (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
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 MediaForm from '../components/media.form'
import MediaMenu from '../components/media.menu'

class MediaNew extends Component {
  state = {
    loading: true,
    initialData: {},
  }

  componentDidMount() {
    // console.log(this.props.match.params.id)
    if (this.props.match.params && this.props.match.params.id) {
      actions.media.show(this.props.match.params.id)
      .then(data => {
        const { id, ...initialData } = data.res
        delete initialData.settings.video
        delete initialData.settings.crop
        delete initialData.settings.display
        delete initialData.settings.fullsize
        delete initialData.settings.thumbnail
        delete initialData.settings.bibliography
        console.log("copying", id)
        this.setState({
          loading: false, 
          initialData,
        })
      })
    } else {
      this.setState({ loading: false })
    }
  }

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

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

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

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

export default connect(mapStateToProps, mapDispatchToProps)(MediaNew)