import React from 'react'; import { makeGetRequest, makePostRequest } from '../utils/xhr.js'; class ChannelCreateForm extends React.Component { constructor (props) { super(props); this.state = { error : null, channel : null, password: null, status : null, }; this.handleChannelInput = this.handleChannelInput.bind(this); this.handleInput = this.handleInput.bind(this); this.cleanseInput = this.cleanseInput.bind(this); this.checkChannelIsAvailable = this.checkChannelIsAvailable.bind(this); this.createChannel = this.createChannel.bind(this); } handleChannelInput (event) { event.preventDefault(); const name = event.target.name; let value = event.target.value; value = this.cleanseInput(value); this.setState({[name]: value}); this.checkChannelIsAvailable(value); } cleanseInput (input) { input = input.replace(/\s+/g, '-'); // replace spaces with dashes input = input.replace(/[^A-Za-z0-9-]/g, ''); // remove all characters that are not A-Z, a-z, 0-9, or '-' return input; } handleInput (event) { event.preventDefault(); const name = event.target.name; const value = event.target.value; this.setState({[name]: value}); } checkChannelIsAvailable (channel) { const that = this; makeGetRequest(`/api/channel-is-available/${channel}`) .then(() => { that.setState({urlError: null}); }) .catch((error) => { that.setState({error: error.message}); }); } validatePassword (password) { if (!password || password.length < 1) { throw new Error('Please provide a password'); } } createChannel (event) { event.preventDefault(); const params = `username=${this.state.channel}&password=${this.state.password}`; const url = '/signup'; // validate submission data try { this.validatePassword(this.state.password); } catch (error) { return this.setState({error: error.message}); } // publish the channel const that = this; this.setState({status: 'We are publishing your new channel. Sit tight...'}); makePostRequest(url, params) .then(result => { that.props.updateLoggedInChannelOutsideReact(result.channelName, result.channelClaimId, result.shortChannelId); that.props.updateUploaderState('loggedInChannelName', result.channelName); that.props.updateUploaderState('loggedInChannelShortId', result.shortChannelId); that.props.selectOption(result.channelName); }) .catch(error => { console.log('create channel failure:', error); if (error.message) { this.setState({'error': error.message}); } else { this.setState({'error': 'Unfortunately, we encountered an error while creating your channel. Please let us know in Discord!'}); } }); } render () { return (
{ !this.state.status ? (

{this.state.error}

@ {'\u2713'}
) : (

{this.state.status}

)}
); } } module.exports = ChannelCreateForm;