// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. var UserStore = require('../../stores/user_store.jsx'); var ErrorStore = require('../../stores/error_store.jsx'); var SettingItemMin = require('../setting_item_min.jsx'); var SettingItemMax = require('../setting_item_max.jsx'); var SettingPicture = require('../setting_picture.jsx'); var client = require('../../utils/client.jsx'); var AsyncClient = require('../../utils/async_client.jsx'); var utils = require('../../utils/utils.jsx'); var assign = require('object-assign'); export default class UserSettingsGeneralTab extends React.Component { constructor(props) { super(props); this.submitActive = false; this.submitUsername = this.submitUsername.bind(this); this.submitNickname = this.submitNickname.bind(this); this.submitName = this.submitName.bind(this); this.submitEmail = this.submitEmail.bind(this); this.submitUser = this.submitUser.bind(this); this.submitPicture = this.submitPicture.bind(this); this.updateUsername = this.updateUsername.bind(this); this.updateFirstName = this.updateFirstName.bind(this); this.updateLastName = this.updateLastName.bind(this); this.updateNickname = this.updateNickname.bind(this); this.updateEmail = this.updateEmail.bind(this); this.updateConfirmEmail = this.updateConfirmEmail.bind(this); this.updatePicture = this.updatePicture.bind(this); this.updateSection = this.updateSection.bind(this); this.setupInitialState = this.setupInitialState.bind(this); this.state = this.setupInitialState(props); } submitUsername(e) { e.preventDefault(); var user = this.props.user; var username = this.state.username.trim().toLowerCase(); var usernameError = utils.isValidUsername(username); if (usernameError === 'Cannot use a reserved word as a username.') { this.setState({clientError: 'This username is reserved, please choose a new one.'}); return; } else if (usernameError) { this.setState({clientError: "Username must begin with a letter, and contain between 3 to 15 lowercase characters made up of numbers, letters, and the symbols '.', '-' and '_'."}); return; } if (user.username === username) { this.setState({clientError: 'You must submit a new username'}); return; } user.username = username; this.submitUser(user, false); } submitNickname(e) { e.preventDefault(); var user = UserStore.getCurrentUser(); var nickname = this.state.nickname.trim(); if (user.nickname === nickname) { this.setState({clientError: 'You must submit a new nickname'}); return; } user.nickname = nickname; this.submitUser(user, false); } submitName(e) { e.preventDefault(); var user = UserStore.getCurrentUser(); var firstName = this.state.firstName.trim(); var lastName = this.state.lastName.trim(); if (user.first_name === firstName && user.last_name === lastName) { this.setState({clientError: 'You must submit a new first or last name'}); return; } user.first_name = firstName; user.last_name = lastName; this.submitUser(user, false); } submitEmail(e) { e.preventDefault(); var user = UserStore.getCurrentUser(); var email = this.state.email.trim().toLowerCase(); var confirmEmail = this.state.confirmEmail.trim().toLowerCase(); if (user.email === email) { return; } if (email === '' || !utils.isEmail(email)) { this.setState({emailError: 'Please enter a valid email address'}); return; } if (email !== confirmEmail) { this.setState({emailError: 'The new emails you entered do not match'}); return; } user.email = email; this.submitUser(user, true); } submitUser(user, emailUpdated) { client.updateUser(user, () => { this.updateSection(''); AsyncClient.getMe(); const verificationEnabled = global.window.mm_config.SendEmailNotifications === 'true' && global.window.mm_config.RequireEmailVerification === 'true' && emailUpdated; if (verificationEnabled) { ErrorStore.storeLastError({message: 'Check your email at ' + user.email + ' to verify the address.'}); ErrorStore.emitChange(); this.setState({emailChangeInProgress: true}); } }, (err) => { var state = this.setupInitialState(this.props); if (err.message) { state.serverError = err.message; } else { state.serverError = err; } this.setState(state); } ); } submitPicture(e) { e.preventDefault(); if (!this.state.picture) { return; } if (!this.submitActive) { return; } var picture = this.state.picture; if (picture.type !== 'image/jpeg' && picture.type !== 'image/png') { this.setState({clientError: 'Only JPG or PNG images may be used for profile pictures'}); return; } var formData = new FormData(); formData.append('image', picture, picture.name); this.setState({loadingPicture: true}); client.uploadProfileImage(formData, function imageUploadSuccess() { this.submitActive = false; AsyncClient.getMe(); window.location.reload(); }.bind(this), function imageUploadFailure(err) { var state = this.setupInitialState(this.props); state.serverError = err.message; this.setState(state); }.bind(this) ); } updateUsername(e) { this.setState({username: e.target.value}); } updateFirstName(e) { this.setState({firstName: e.target.value}); } updateLastName(e) { this.setState({lastName: e.target.value}); } updateNickname(e) { this.setState({nickname: e.target.value}); } updateEmail(e) { this.setState({email: e.target.value}); } updateConfirmEmail(e) { this.setState({confirmEmail: e.target.value}); } updatePicture(e) { if (e.target.files && e.target.files[0]) { this.setState({picture: e.target.files[0]}); this.submitActive = true; this.setState({clientError: null}); } else { this.setState({picture: null}); } } updateSection(section) { const emailChangeInProgress = this.state.emailChangeInProgress; this.setState(assign({}, this.setupInitialState(this.props), {emailChangeInProgress: emailChangeInProgress, clientError: '', serverError: '', emailError: ''})); this.submitActive = false; this.props.updateSection(section); } setupInitialState(props) { var user = props.user; return {username: user.username, firstName: user.first_name, lastName: user.last_name, nickname: user.nickname, email: user.email, confirmEmail: '', picture: null, loadingPicture: false, emailChangeInProgress: false}; } render() { var user = this.props.user; var clientError = null; if (this.state.clientError) { clientError = this.state.clientError; } var serverError = null; if (this.state.serverError) { serverError = this.state.serverError; } var emailError = null; if (this.state.emailError) { emailError = this.state.emailError; } var nameSection; var inputs = []; if (this.props.activeSection === 'name') { inputs.push(