diff --git a/public/api/User.php b/public/api/User.php index c14a9a2..0c1208f 100644 --- a/public/api/User.php +++ b/public/api/User.php @@ -81,12 +81,12 @@ VALUES (?, ?, ?, ?, ?, ?)'; public function setUserData ($token, $user_data) { $token_data = $this->token->decode($token); if ($token_data !== false) { - $query = 'UPDATE users SET email=?, public_name=?, username=?, allow_email=? WHERE id=?'; + $user_id = $token_data->id; + $query = 'UPDATE users SET email=?, public_name=?, allow_email=? WHERE id=?'; $properties = array( $user_data['email'], $user_data['publicName'], - $user_data['username'], - $user_data['allowEmail'], + $user_data['allowEmails'], $user_id, ); $update_success = $this->db->execute($query, $properties); diff --git a/src/components/management/AccountManager/MyAccount.jsx b/src/components/management/AccountManager/MyAccount.jsx index 9b39576..4fca010 100644 --- a/src/components/management/AccountManager/MyAccount.jsx +++ b/src/components/management/AccountManager/MyAccount.jsx @@ -2,27 +2,69 @@ import Inferno from 'inferno'; import { Component } from 'inferno'; import PropTypes from 'prop-types'; +import { request } from '../../../Helpers'; + export class MyAccount extends Component { constructor(props) { super(props); PropTypes.checkPropTypes({ email: PropTypes.string.isRequired, - // username: PropTypes.string.isRequired, publicName: PropTypes.string.isRequired, allowEmails: PropTypes.bool.isRequired, userDictionaries: PropTypes.array.isRequired, - updateUserData: PropTypes.func, + sendUserData: PropTypes.func, changeDictionary: PropTypes.func, }, props, 'prop', 'MyAccount'); this.state = { email: this.props.email, - username: this.props.username, + emailError: '', publicName: this.props.publicName, allowEmails: this.props.allowEmails, userDictionaries: this.props.userDictionaries, + hasChanged: false, + canSend: false, }; + + this.originalState = Object.assign({}, this.state); + } + + get hasChanged () { + for (const property in this.state) { + if (['email', 'publicName', 'allowEmails'].includes(property)) { + if (this.state[property] !== this.originalState[property]) { + return true; + } + } + } + return false; + } + + checkFields () { + const emailError = /^.+@.+$/.test(this.state.email) ? '' : 'This doesn\'t look like an email address. Please make sure it\'s correct.'; + this.setState({ + emailError, + hasChanged: this.hasChanged, + canSend: emailError === '' && this.hasChanged, + }); + } + + saveChanges () { + if (this.state.canSend) { + this.props.sendUserData({ + email: this.state.email, + publicName: this.state.publicName, + allowEmails: this.state.allowEmails, + }, () => { + this.setState({ + hasChanged: false, + canSend: false, + }, () => { + this.originalState = Object.assign({}, this.state); + }); + }); + } } render() { @@ -38,7 +80,8 @@ export class MyAccount extends Component {