var React = require('react'); var ReactBootstrap = require('react-bootstrap'); var Modal = ReactBootstrap.Modal; var Button = ReactBootstrap.Button; var ButtonGroup = ReactBootstrap.ButtonGroup; var Input = ReactBootstrap.Input; var models = require('./models.js'); var User = models.User; var Error = models.Error; module.exports = React.createClass({ displayName: "AccountSettingsModal", _getInitialState: function(props) { return {error: "", name: props.user.Name, username: props.user.Username, email: props.user.Email, password: models.BogusPassword, confirm_password: models.BogusPassword, passwordChanged: false, initial_password: models.BogusPassword}; }, getInitialState: function() { return this._getInitialState(this.props); }, componentWillReceiveProps: function(nextProps) { if (nextProps.show && !this.props.show) { this.setState(this._getInitialState(nextProps)); } }, passwordValidationState: function() { if (this.state.passwordChanged) { if (this.state.password.length >= 10) return "success"; else if (this.state.password.length >= 6) return "warning"; else return "error"; } }, confirmPasswordValidationState: function() { if (this.state.confirm_password.length > 0) { if (this.state.confirm_password == this.state.password) return "success"; else return "error"; } }, handleCancel: function() { if (this.props.onCancel != null) this.props.onCancel(); }, handleChange: function() { if (this.refs.password.getValue() != this.state.initial_password) this.setState({passwordChanged: true}); this.setState({ name: this.refs.name.getValue(), username: this.refs.username.getValue(), email: this.refs.email.getValue(), password: this.refs.password.getValue(), confirm_password: this.refs.confirm_password.getValue() }); }, handleSubmit: function(e) { var u = new User(); var error = ""; e.preventDefault(); u.UserId = this.props.user.UserId; u.Name = this.state.name; u.Username = this.state.username; u.Email = this.state.email; if (this.state.passwordChanged) { u.Password = this.state.password; if (u.Password != this.state.confirm_password) { this.setState({error: "Error: password do not match"}); return; } } else { u.Password = models.BogusPassword; } this.handleSaveSettings(u); }, handleSaveSettings: function(user) { $.ajax({ type: "PUT", dataType: "json", url: "user/"+user.UserId+"/", data: {user: user.toJSON()}, success: function(data, status, jqXHR) { var e = new Error(); e.fromJSON(data); if (e.isError()) { this.setState({error: e}); } else { user.Password = ""; this.props.onSubmit(user); } }.bind(this), error: function(jqXHR, status, error) { var e = new Error(); e.ErrorId = 5; e.ErrorString = "Request Failed: " + status + error; this.setState({error: e}); }.bind(this), }); }, render: function() { return ( Edit Account Settings {this.state.error}
); } });