// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. // See License.txt for license information. var utils = require('../utils/utils.jsx'); var Client =require('../utils/client.jsx'); var UserStore = require('../stores/user_store.jsx'); module.exports = React.createClass({ handleSubmit: function(e) { var invite_ids = this.state.invite_ids; var count = invite_ids.length; var invites = []; var email_errors = this.state.email_errors; var first_name_errors = this.state.first_name_errors; var last_name_errors = this.state.last_name_errors; var valid = true; for (var i = 0; i < count; i++) { var index = invite_ids[i]; var invite = {}; invite.email = this.refs["email"+index].getDOMNode().value.trim(); if (!invite.email || !utils.isEmail(invite.email)) { email_errors[index] = "Please enter a valid email address"; valid = false; } else { email_errors[index] = ""; } if (config.AllowInviteNames) { invite.first_name = this.refs["first_name"+index].getDOMNode().value.trim(); if (!invite.first_name ) { first_name_errors[index] = "This is a required field"; valid = false; } else { first_name_errors[index] = ""; } invite.last_name = this.refs["last_name"+index].getDOMNode().value.trim(); if (!invite.last_name ) { last_name_errors[index] = "This is a required field"; valid = false; } else { last_name_errors[index] = ""; } } invites.push(invite); } this.setState({ email_errors: email_errors, first_name_errors: first_name_errors, last_name_errors: last_name_errors }); if (!valid || invites.length === 0) return; var data = {} data["invites"] = invites; Client.inviteMembers(data, function() { $(this.refs.modal.getDOMNode()).modal('hide'); for (var i = 0; i < invite_ids.length; i++) { var index = invite_ids[i]; this.refs["email"+index].getDOMNode().value = ""; if (config.AllowInviteNames) { this.refs["first_name"+index].getDOMNode().value = ""; this.refs["last_name"+index].getDOMNode().value = ""; } } this.setState({ invite_ids: [0], id_count: 0, email_errors: {}, first_name_errors: {}, last_name_errors: {} }); }.bind(this), function(err) { this.setState({ server_error: err }); }.bind(this) ); }, componentDidUpdate: function() { $(this.refs.modalBody.getDOMNode()).css('max-height', $(window).height() - 200); $(this.refs.modalBody.getDOMNode()).css('overflow-y', 'scroll'); }, addInviteFields: function() { var count = this.state.id_count + 1; var invite_ids = this.state.invite_ids; invite_ids.push(count); this.setState({ invite_ids: invite_ids, id_count: count }); }, removeInviteFields: function(index) { var invite_ids = this.state.invite_ids; var i = invite_ids.indexOf(index); if (index > -1) invite_ids.splice(i, 1); this.setState({ invite_ids: invite_ids }); }, getInitialState: function() { return { invite_ids: [0], id_count: 0, email_errors: {}, first_name_errors: {}, last_name_errors: {} }; }, render: function() { var currentUser = UserStore.getCurrentUser() if (currentUser != null) { var invite_sections = []; var invite_ids = this.state.invite_ids; var self = this; for (var i = 0; i < invite_ids.length; i++) { var index = invite_ids[i]; var email_error = this.state.email_errors[index] ? : null; var first_name_error = this.state.first_name_errors[index] ? : null; var last_name_error = this.state.last_name_errors[index] ? : null; invite_sections[index] = (