1 | import React, {Component, PropTypes} from 'react'
|
2 | import Studio from 'jsreport-studio'
|
3 |
|
4 | export default class ChangePasswordModal extends Component {
|
5 | static propTypes = {
|
6 | close: PropTypes.func.isRequired,
|
7 | options: PropTypes.object.isRequired
|
8 | }
|
9 |
|
10 | constructor () {
|
11 | super()
|
12 | this.state = {}
|
13 | }
|
14 |
|
15 | async changePassword () {
|
16 | const { entity } = this.props.options
|
17 | const { close } = this.props
|
18 |
|
19 | try {
|
20 | let data = {
|
21 | newPassword: this.refs.newPassword1.value
|
22 | }
|
23 |
|
24 | if (!Studio.authentication.user.isAdmin) {
|
25 | data.oldPassword = this.refs.oldPassword.value
|
26 | }
|
27 |
|
28 | await Studio.api.post(`/api/users/${entity.shortid}/password`, { data: data })
|
29 | this.refs.newPassword1.value = ''
|
30 | this.refs.newPassword2.value = ''
|
31 | close()
|
32 | } catch (e) {
|
33 | this.setState({ apiError: e.message })
|
34 | }
|
35 | }
|
36 |
|
37 | validatePassword () {
|
38 | this.setState(
|
39 | {
|
40 | passwordError: this.refs.newPassword2.value && this.refs.newPassword2.value !== this.refs.newPassword1.value,
|
41 | apiError: null
|
42 | })
|
43 | }
|
44 |
|
45 | render () {
|
46 | return <div>
|
47 | {Studio.authentication.user.isAdmin ? '' : <div className='form-group'>
|
48 | <label>old password</label>
|
49 | <input type='password' ref='oldPassword' />
|
50 | </div>
|
51 | }
|
52 | <div className='form-group'>
|
53 | <label>new password</label>
|
54 | <input type='password' ref='newPassword1' />
|
55 | </div>
|
56 | <div className='form-group'>
|
57 | <label>new password verification</label>
|
58 | <input type='password' ref='newPassword2' onChange={() => this.validatePassword()} />
|
59 | </div>
|
60 | <div className='form-group'>
|
61 | <span style={{color: 'red', display: this.state.passwordError ? 'block' : 'none'}}>password doesn't match</span>
|
62 | <span style={{color: 'red', display: this.state.apiError ? 'block' : 'none'}}>{this.state.apiError}</span>
|
63 | </div>
|
64 | <div className='button-bar'>
|
65 | <button className='button confirmation' onClick={() => this.changePassword()}>ok</button>
|
66 | </div>
|
67 | </div>
|
68 | }
|
69 | }
|