summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/user/containers/user.edit.js
blob: 5ae1fe50a7f526e37e92c0f26b972ead1a5dcc9e (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
import React, { Component } from 'react'
import { connect } from 'react-redux'

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

import { Loader } from 'app/common'

import UserForm from '../components/user.form'
import UserMenu from '../components/user.menu'

class UserEdit extends Component {
  componentDidMount() {
    console.log(this.props.match.params.id)
    actions.user.show(this.props.match.params.id)
  }

  handleSubmit(data) {
    actions.user.update(data)
      .then(response => {
        // response
        console.log(response)
        history.push('/users/')
      })
      .catch(err => {
        console.log(err)
        if (!err.errors) {
          if (err.error) {
            alert(err.error)
            return
          }
          alert("There was an error saving this user.")
          return
        }
        const errorStr = Object.keys(err.errors)
          .map(key => (
            err.errors[key].map(error => 
              key + ": " + error
            ).join("\n")
          )).join("\n")
        alert("There was an error updating this user.\n" + errorStr)
      })
  }

  render() {
    const { show } = this.props.user
    if (show.loading || !show.res) {
      return (
        <div className='form'>
          <Loader />
        </div>
      )
    }
    return (
      <div className='row formContainer'>
        <UserMenu userActions={this.props.userActions} />
        <UserForm
          data={show.res}
          currentUser={this.props.currentUser}
          onSubmit={this.handleSubmit.bind(this)}
        />
      </div>
    )
  }
}

const mapStateToProps = state => ({
  user: state.user,
  currentUser: state.auth.user,
})

export default connect(mapStateToProps)(UserEdit)