// 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 asyncClient = require('../utils/async_client.jsx'); var UserStore = require('../stores/user_store.jsx'); export default class NewChannelModal extends React.Component { constructor() { super(); this.handleSubmit = this.handleSubmit.bind(this); this.displayNameKeyUp = this.displayNameKeyUp.bind(this); this.handleClose = this.handleClose.bind(this); this.state = {channelType: ''}; } handleSubmit(e) { e.preventDefault(); var channel = {}; var state = {serverError: ''}; channel.display_name = React.findDOMNode(this.refs.display_name).value.trim(); if (!channel.display_name) { state.displayNameError = 'This field is required'; state.inValid = true; } else if (channel.display_name.length > 22) { state.displayNameError = 'This field must be less than 22 characters'; state.inValid = true; } else { state.displayNameError = ''; } channel.name = React.findDOMNode(this.refs.channel_name).value.trim(); if (!channel.name) { state.nameError = 'This field is required'; state.inValid = true; } else if (channel.name.length > 22) { state.nameError = 'This field must be less than 22 characters'; state.inValid = true; } else { var cleanedName = utils.cleanUpUrlable(channel.name); if (cleanedName !== channel.name) { state.nameError = "Must be lowercase alphanumeric characters, allowing '-' but not starting or ending with '-'"; state.inValid = true; } else { state.nameError = ''; } } this.setState(state); if (state.inValid) { return; } var cu = UserStore.getCurrentUser(); channel.team_id = cu.team_id; channel.description = React.findDOMNode(this.refs.channel_desc).value.trim(); channel.type = this.state.channelType; client.createChannel(channel, function success(data) { $(React.findDOMNode(this.refs.modal)).modal('hide'); asyncClient.getChannel(data.id); utils.switchChannel(data); React.findDOMNode(this.refs.display_name).value = ''; React.findDOMNode(this.refs.channel_name).value = ''; React.findDOMNode(this.refs.channel_desc).value = ''; }.bind(this), function error(err) { state.serverError = err.message; state.inValid = true; this.setState(state); }.bind(this) ); } displayNameKeyUp() { var displayName = React.findDOMNode(this.refs.display_name).value.trim(); var channelName = utils.cleanUpUrlable(displayName); React.findDOMNode(this.refs.channel_name).value = channelName; } componentDidMount() { var self = this; $(React.findDOMNode(this.refs.modal)).on('show.bs.modal', function onModalShow(e) { var button = e.relatedTarget; self.setState({channelType: $(button).attr('data-channeltype')}); }); $(React.findDOMNode(this.refs.modal)).on('hidden.bs.modal', this.handleClose); } componentWillUnmount() { $(React.findDOMNode(this.refs.modal)).off('hidden.bs.modal', this.handleClose); } handleClose() { $(React.findDOMNode(this)).find('.form-control').each(function clearForms() { this.value = ''; }); this.setState({channelType: '', displayNameError: '', nameError: '', serverError: '', inValid: false}); } render() { var displayNameError = null; var nameError = null; var serverError = null; var displayNameClass = 'form-group'; var nameClass = 'form-group'; if (this.state.displayNameError) { displayNameError = ; displayNameClass += ' has-error'; } if (this.state.nameError) { nameError = ; nameClass += ' has-error'; } if (this.state.serverError) { serverError =
; } var channelTerm = 'Channel'; if (this.state.channelType === 'P') { channelTerm = 'Group'; } return ( ); } }