// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. // See License.txt for license information. var SettingItemMin = require('./setting_item_min.jsx'); var SettingItemMax = require('./setting_item_max.jsx'); var Client = require('../utils/client.jsx'); var AsyncClient = require('../utils/async_client.jsx'); var Constants = require('../utils/constants.jsx'); export default class SecurityTab extends React.Component { constructor(props) { super(props); this.submitPassword = this.submitPassword.bind(this); this.updateCurrentPassword = this.updateCurrentPassword.bind(this); this.updateNewPassword = this.updateNewPassword.bind(this); this.updateConfirmPassword = this.updateConfirmPassword.bind(this); this.handleClose = this.handleClose.bind(this); this.state = {currentPassword: '', newPassword: '', confirmPassword: ''}; } submitPassword(e) { e.preventDefault(); var user = this.props.user; var currentPassword = this.state.currentPassword; var newPassword = this.state.newPassword; var confirmPassword = this.state.confirmPassword; if (currentPassword === '') { this.setState({passwordError: 'Please enter your current password', serverError: ''}); return; } if (newPassword.length < 5) { this.setState({passwordError: 'New passwords must be at least 5 characters', serverError: ''}); return; } if (newPassword !== confirmPassword) { this.setState({passwordError: 'The new passwords you entered do not match', serverError: ''}); return; } var data = {}; data.user_id = user.id; data.current_password = currentPassword; data.new_password = newPassword; Client.updatePassword(data, function success() { this.props.updateSection(''); AsyncClient.getMe(); this.setState({currentPassword: '', newPassword: '', confirmPassword: ''}); }.bind(this), function fail(err) { var state = this.getInitialState(); if (err.message) { state.serverError = err.message; } else { state.serverError = err; } state.passwordError = ''; this.setState(state); }.bind(this) ); } updateCurrentPassword(e) { this.setState({currentPassword: e.target.value}); } updateNewPassword(e) { this.setState({newPassword: e.target.value}); } updateConfirmPassword(e) { this.setState({confirmPassword: e.target.value}); } handleHistoryOpen() { $('#user_settings').modal('hide'); } handleDevicesOpen() { $('#user_settings').modal('hide'); } handleClose() { $(React.findDOMNode(this)).find('.form-control').each(function resetValue() { this.value = ''; }); this.setState({currentPassword: '', newPassword: '', confirmPassword: '', serverError: null, passwordError: null}); this.props.updateTab('general'); } componentDidMount() { $('#user_settings').on('hidden.bs.modal', this.handleClose); } componentWillUnmount() { $('#user_settings').off('hidden.bs.modal', this.handleClose); this.props.updateSection(''); } render() { var serverError; if (this.state.serverError) { serverError = this.state.serverError; } var passwordError; if (this.state.passwordError) { passwordError = this.state.passwordError; } var updateSectionStatus; var passwordSection; if (this.props.activeSection === 'password') { var inputs = []; var submit = null; if (this.props.user.auth_service === '') { inputs.push(