diff --git a/browser/main/Components/PersonalSettingModal.jsx b/browser/main/Components/PersonalSettingModal.jsx index af73063a..09206ec7 100644 --- a/browser/main/Components/PersonalSettingModal.jsx +++ b/browser/main/Components/PersonalSettingModal.jsx @@ -6,6 +6,8 @@ var Catalyst = require('../Mixins/Catalyst') var AuthActions = require('../Actions/AuthActions') +var AuthStore = require('../Stores/AuthStore') + var apiUrl = 'http://localhost:8000/' module.exports = React.createClass({ @@ -17,6 +19,7 @@ module.exports = React.createClass({ getInitialState: function () { return { currentTab: 'profile', + profileName: this.props.currentUser.profileName, userName: this.props.currentUser.name, email: this.props.currentUser.email, currentPassword: '', @@ -26,6 +29,32 @@ module.exports = React.createClass({ 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'}) }, @@ -39,12 +68,24 @@ module.exports = React.createClass({ this.setState({currentTab: 'logout'}) }, saveProfile: function () { - AuthActions.updateProfile({ - name: this.state.userName, - email: this.state.email + 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') @@ -56,21 +97,65 @@ module.exports = React.createClass({ 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: '' + confirmation: '', + isChangingPassword: false, + isChangingPasswordDone: true, + isChangingPasswordFailed: false }) - if (err) { - console.error(err) - return - } }.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() @@ -84,6 +169,10 @@ module.exports = React.createClass({ content = (
+
+ + +
@@ -94,6 +183,9 @@ module.exports = React.createClass({
+

Updating profile...

+

Successfully updated

+

An Error occurred

@@ -112,6 +204,9 @@ module.exports = React.createClass({
+

Changing password...

+

Successfully changed

+

An Error occurred

@@ -126,7 +221,10 @@ module.exports = React.createClass({