|
| 1 | +/* global localStorage */ |
| 2 | +var React = require('react/addons') |
| 3 | +var request = require('superagent') |
| 4 | + |
| 5 | +var Catalyst = require('../Mixins/Catalyst') |
| 6 | + |
| 7 | +var AuthActions = require('../Actions/AuthActions') |
| 8 | + |
| 9 | +var apiUrl = 'http://localhost:8000/' |
| 10 | + |
| 11 | +module.exports = React.createClass({ |
| 12 | + mixins: [Catalyst.LinkedStateMixin], |
| 13 | + propTypes: { |
| 14 | + close: React.PropTypes.func, |
| 15 | + currentUser: React.PropTypes.object |
| 16 | + }, |
| 17 | + getInitialState: function () { |
| 18 | + return { |
| 19 | + currentTab: 'profile', |
| 20 | + userName: this.props.currentUser.name, |
| 21 | + email: this.props.currentUser.email, |
| 22 | + currentPassword: '', |
| 23 | + newPassword: '', |
| 24 | + confirmation: '', |
| 25 | + contactTitle: '', |
| 26 | + contactContent: '' |
| 27 | + } |
| 28 | + }, |
| 29 | + activeProfile: function () { |
| 30 | + this.setState({currentTab: 'profile'}) |
| 31 | + }, |
| 32 | + activeContact: function () { |
| 33 | + this.setState({currentTab: 'contact'}) |
| 34 | + }, |
| 35 | + activeInfo: function () { |
| 36 | + this.setState({currentTab: 'info'}) |
| 37 | + }, |
| 38 | + activeLogout: function () { |
| 39 | + this.setState({currentTab: 'logout'}) |
| 40 | + }, |
| 41 | + saveProfile: function () { |
| 42 | + AuthActions.updateProfile({ |
| 43 | + name: this.state.userName, |
| 44 | + email: this.state.email |
| 45 | + }) |
| 46 | + }, |
| 47 | + savePassword: function () { |
| 48 | + if (this.state.newPassword === this.state.confirmation) { |
| 49 | + request |
| 50 | + .put(apiUrl + 'auth/password') |
| 51 | + .set({ |
| 52 | + Authorization: 'Bearer ' + localStorage.getItem('token') |
| 53 | + }) |
| 54 | + .send({ |
| 55 | + currentPassword: this.state.currentPassword, |
| 56 | + newPassword: this.state.newPassword |
| 57 | + }) |
| 58 | + .end(function (err, res) { |
| 59 | + this.setState({ |
| 60 | + currentPassword: '', |
| 61 | + newPassword: '', |
| 62 | + confirmation: '' |
| 63 | + }) |
| 64 | + if (err) { |
| 65 | + console.error(err) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + }.bind(this)) |
| 70 | + } |
| 71 | + }, |
| 72 | + sendEmail: function () { |
| 73 | + |
| 74 | + }, |
| 75 | + logOut: function () { |
| 76 | + AuthActions.logout() |
| 77 | + }, |
| 78 | + interceptClick: function (e) { |
| 79 | + e.stopPropagation() |
| 80 | + }, |
| 81 | + render: function () { |
| 82 | + var content |
| 83 | + if (this.state.currentTab === 'profile') { |
| 84 | + content = ( |
| 85 | + <div className='profile'> |
| 86 | + <div className='profileTop'> |
| 87 | + <div className='profileFormRow'> |
| 88 | + <label>Name</label> |
| 89 | + <input valueLink={this.linkState('userName')} className='block-input' type='text' placeholder='Name'/> |
| 90 | + </div> |
| 91 | + <div className='profileFormRow'> |
| 92 | + <label>E-mail</label> |
| 93 | + <input valueLink={this.linkState('email')} className='block-input' type='text' placeholder='E-mail'/> |
| 94 | + </div> |
| 95 | + <div className='profileFormRow'> |
| 96 | + <button onClick={this.saveProfile} className='saveButton btn-primary'>Save</button> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + |
| 100 | + <div className='profileBottom'> |
| 101 | + <div className='profileFormRow'> |
| 102 | + <label>Current password</label> |
| 103 | + <input valueLink={this.linkState('currentPassword')} className='block-input' type='password' placeholder='Current password'/> |
| 104 | + </div> |
| 105 | + <div className='profileFormRow'> |
| 106 | + <label>New password</label> |
| 107 | + <input valueLink={this.linkState('newPassword')} className='block-input' type='password' placeholder='New password'/> |
| 108 | + </div> |
| 109 | + <div className='profileFormRow'> |
| 110 | + <label>Confirmation</label> |
| 111 | + <input valueLink={this.linkState('confirmation')} className='block-input' type='password' placeholder='Confirmation'/> |
| 112 | + </div> |
| 113 | + <div className='profileFormRow'> |
| 114 | + <button onClick={this.savePassword} className='saveButton btn-primary'>Save</button> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + ) |
| 119 | + } else if (this.state.currentTab === 'contact') { |
| 120 | + content = ( |
| 121 | + <div className='contact'> |
| 122 | + <p> |
| 123 | + Let us know your opinion about CodeXen.<br/> |
| 124 | + Your feedback might be used to improvement of CodeXen. |
| 125 | + </p> |
| 126 | + <input valueLink={this.linkState('contactTitle')} className='block-input' type='text' placeholder='title'/> |
| 127 | + <textarea valueLink={this.linkState('contactContent')} className='block-input' placeholder='message content'/> |
| 128 | + <div className='contactFormRow'> |
| 129 | + <button onClick={this.sendEmail} className='saveButton btn-primary'>Send</button> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + ) |
| 133 | + } else if (this.state.currentTab === 'info') { |
| 134 | + content = ( |
| 135 | + <div className='info'> |
| 136 | + <h2 className='infoLabel'>External links</h2> |
| 137 | + <ul className='externalList'> |
| 138 | + <li><a>CodeXen Homepage <i className='fa fa-external-link'/></a></li> |
| 139 | + <li><a>Regulation <i className='fa fa-external-link'/></a></li> |
| 140 | + <li><a>Private policy <i className='fa fa-external-link'/></a></li> |
| 141 | + </ul> |
| 142 | + </div> |
| 143 | + ) |
| 144 | + } else { |
| 145 | + content = ( |
| 146 | + <div className='logout'> |
| 147 | + <p className='logoutLabel'>Are you sure to logout?</p> |
| 148 | + <img className='userPhoto' width='150' height='150' src='../vendor/dummy.jpg'/><br/> |
| 149 | + <button onClick={this.logOut} className='logoutButton btn-default'>Logout</button> |
| 150 | + </div> |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | + return ( |
| 155 | + <div onClick={this.interceptClick} className='PersonalSettingModal modal'> |
| 156 | + <div className='settingNav'> |
| 157 | + <h1>Personal setting</h1> |
| 158 | + <nav> |
| 159 | + <button className={this.state.currentTab === 'profile' ? 'active' : ''} onClick={this.activeProfile}><i className='fa fa-user fa-fw'/> Profile</button> |
| 160 | + <button className={this.state.currentTab === 'contact' ? 'active' : ''} onClick={this.activeContact}><i className='fa fa-phone fa-fw'/> Contact</button> |
| 161 | + <button className={this.state.currentTab === 'info' ? 'active' : ''} onClick={this.activeInfo}><i className='fa fa-info-circle fa-fw'/> Info</button> |
| 162 | + <button className={this.state.currentTab === 'logout' ? 'active' : ''} onClick={this.activeLogout}><i className='fa fa-sign-out fa-fw'/> Logout</button> |
| 163 | + </nav> |
| 164 | + </div> |
| 165 | + <div className='settingBody'> |
| 166 | + {content} |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + ) |
| 170 | + } |
| 171 | +}) |
0 commit comments