/* global localStorage */ var React = require('react/addons') var request = require('superagent') var Catalyst = require('../Mixins/Catalyst') var AuthActions = require('../Actions/AuthActions') var AuthStore = require('../Stores/AuthStore') var apiUrl = require('../../../config').apiUrl module.exports = React.createClass({ mixins: [Catalyst.LinkedStateMixin], propTypes: { close: React.PropTypes.func, currentUser: React.PropTypes.object }, getInitialState: function () { return { currentTab: 'profile', profileName: this.props.currentUser.profileName, userName: this.props.currentUser.name, email: this.props.currentUser.email, currentPassword: '', newPassword: '', confirmation: '', contactTitle: '', contactContent: '' } }, componentDidMount: function () { this.unsubscribe = AuthStore.listen(this.onListen) }, componentWillUnmount: function () { this.unsubscribe() }, onListen: function (res) { console.log(res) if (res.status === 'userProfileUpdated') { this.setState({ isUpdatingProfile: false, isUpdatingProfileDone: true, isUpdatingProfileFailed: false }) return } if (res.status === 'userProfileUpdatingFailed') { this.setState({ isUpdatingProfile: false, isUpdatingProfileDone: false, isUpdatingProfileFailed: true }) return } }, activeProfile: function () { this.setState({currentTab: 'profile'}) }, activeContact: function () { this.setState({currentTab: 'contact'}) }, activeInfo: function () { this.setState({currentTab: 'info'}) }, activeLogout: function () { this.setState({currentTab: 'logout'}) }, saveProfile: function () { this.setState({ isUpdatingProfile: true, isUpdatingProfileDone: false, isUpdatingProfileFailed: false }, function () { AuthActions.updateProfile({ profileName: this.state.profileName, name: this.state.userName, email: this.state.email }) }) }, savePassword: function () { this.setState({ isChangingPassword: true, isChangingPasswordDone: false, isChangingPasswordFailed: false }) if (this.state.newPassword === this.state.confirmation) { request .put(apiUrl + 'auth/password') .set({ Authorization: 'Bearer ' + localStorage.getItem('token') }) .send({ currentPassword: this.state.currentPassword, newPassword: this.state.newPassword }) .end(function (err, res) { if (err) { console.error(err) this.setState({ currentPassword: '', newPassword: '', confirmation: '', isChangingPassword: false, isChangingPasswordDone: false, isChangingPasswordFailed: true }) return } this.setState({ currentPassword: '', newPassword: '', confirmation: '', isChangingPassword: false, isChangingPasswordDone: true, isChangingPasswordFailed: false }) }.bind(this)) } }, sendEmail: function () { this.setState({ isSending: true, isSendingDone: false, isSendingFailed: false }, function () { request .post(apiUrl + 'mail') .set({ Authorization: 'Bearer ' + localStorage.getItem('token') }) .send({ title: this.state.contactTitle, content: this.state.contactContent }) .end(function (err, res) { if (err) { console.error(err) this.setState({ isSending: false, isSendingDone: false, isSendingFailed: true }) return } this.setState({ isSending: false, isSendingDone: true, isSendingFailed: false, contactTitle: '', contactContent: '' }) }.bind(this)) }) }, logOut: function () { AuthActions.logout() }, interceptClick: function (e) { e.stopPropagation() }, render: function () { var content if (this.state.currentTab === 'profile') { content = (

Updating profile...

Successfully updated

An Error occurred

Changing password...

Successfully changed

An Error occurred

) } else if (this.state.currentTab === 'contact') { content = (

Let us know your opinion about CodeXen.
Your feedback might be used to improvement of CodeXen.