// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. // See License.txt for license information. var UserStore = require('../stores/user_store.jsx'); var ConfigStore = require('../stores/config_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'); module.exports = React.createClass({ displayName: 'GeneralTab', submitActive: false, submitUsername: function(e) { e.preventDefault(); var user = this.props.user; var username = this.state.username.trim(); 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); }, submitNickname: function(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); }, submitName: function(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); }, submitEmail: function(e) { e.preventDefault(); var user = UserStore.getCurrentUser(); var email = this.state.email.trim().toLowerCase(); if (user.email === email) { return; } if (email === '' || !utils.isEmail(email)) { this.setState({emailError: 'Please enter a valid email address'}); return; } user.email = email; this.submitUser(user); }, submitUser: function(user) { client.updateUser(user, function() { this.updateSection(''); AsyncClient.getMe(); }.bind(this), function(err) { var state = this.getInitialState(); if (err.message) { state.serverError = err.message; } else { state.serverError = err; } this.setState(state); }.bind(this) ); }, submitPicture: function(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() { this.submitActive = false; AsyncClient.getMe(); window.location.reload(); }.bind(this), function(err) { var state = this.getInitialState(); state.serverError = err; this.setState(state); }.bind(this) ); }, updateUsername: function(e) { this.setState({username: e.target.value}); }, updateFirstName: function(e) { this.setState({firstName: e.target.value}); }, updateLastName: function(e) { this.setState({lastName: e.target.value}); }, updateNickname: function(e) { this.setState({nickname: e.target.value}); }, updateEmail: function(e) { this.setState({email: e.target.value}); }, updatePicture: function(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: function(section) { this.setState(assign({}, this.getInitialState(), {clientError: ''})); this.submitActive = false; this.props.updateSection(section); }, handleClose: function() { $(this.getDOMNode()).find('.form-control').each(function() { this.value = ''; }); this.setState(assign({}, this.getInitialState(), {clientError: null, serverError: null, emailError: null})); this.props.updateSection(''); }, componentDidMount: function() { $('#user_settings').on('hidden.bs.modal', this.handleClose); }, componentWillUnmount: function() { $('#user_settings').off('hidden.bs.modal', this.handleClose); }, getInitialState: function() { var user = this.props.user; var emailEnabled = !ConfigStore.getSettingAsBoolean('ByPassEmail', false); return {username: user.username, firstName: user.first_name, lastName: user.last_name, nickname: user.nickname, email: user.email, picture: null, loadingPicture: false, emailEnabled: emailEnabled}; }, render: function() { 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 self = this; var inputs = []; if (this.props.activeSection === 'name') { inputs.push(