spee.ch/react/containers/ChannelCreateForm/view.jsx

165 lines
6.1 KiB
React
Raw Normal View History

2018-01-05 22:59:25 +01:00
import React from 'react';
2018-01-20 01:40:47 +01:00
import ProgressBar from 'components/ProgressBar';
import request from 'utils/request';
2018-01-05 22:59:25 +01:00
class ChannelCreateForm extends React.Component {
constructor (props) {
super(props);
this.state = {
error : null,
channel : '',
password: '',
2018-01-05 22:59:25 +01:00
status : null,
};
this.handleChannelInput = this.handleChannelInput.bind(this);
this.handleInput = this.handleInput.bind(this);
this.createChannel = this.createChannel.bind(this);
}
2018-01-10 20:26:01 +01:00
cleanseChannelInput (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;
}
2018-01-05 22:59:25 +01:00
handleChannelInput (event) {
let value = event.target.value;
2018-01-10 20:26:01 +01:00
value = this.cleanseChannelInput(value);
this.setState({channel: value});
if (value) {
this.updateIsChannelAvailable(value);
} else {
this.setState({error: 'Please enter a channel name'});
}
2018-01-05 22:59:25 +01:00
}
handleInput (event) {
const name = event.target.name;
const value = event.target.value;
this.setState({[name]: value});
}
updateIsChannelAvailable (channel) {
2018-01-24 18:27:36 +01:00
const channelWithAtSymbol = `@${channel}`;
2018-02-07 06:55:04 +01:00
request(`/api/channel/availability/${channelWithAtSymbol}`)
.then(isAvailable => {
if (isAvailable) {
2018-02-07 00:05:31 +01:00
this.setState({'error': null});
} else {
2018-02-07 00:05:31 +01:00
this.setState({'error': 'That channel has already been claimed'});
}
2018-01-05 22:59:25 +01:00
})
.catch((error) => {
2018-02-07 00:05:31 +01:00
this.setState({'error': error.message});
2018-01-05 22:59:25 +01:00
});
}
checkIsChannelAvailable (channel) {
const channelWithAtSymbol = `@${channel}`;
return new Promise((resolve, reject) => {
2018-02-07 06:55:04 +01:00
request(`/api/channel/availability/${channelWithAtSymbol}`)
.then(isAvailable => {
2018-01-25 03:08:21 +01:00
console.log('checkIsChannelAvailable result:', isAvailable);
if (!isAvailable) {
return reject(new Error('That channel has already been claimed'));
}
resolve();
})
.catch((error) => {
reject(error);
});
});
}
checkIsPasswordProvided () {
const password = this.state.password;
return new Promise((resolve, reject) => {
if (!password || password.length < 1) {
console.log('password not provided');
return reject(new Error('Please provide a password'));
}
console.log('password provided');
resolve();
});
}
makePublishChannelRequest (username, password) {
const params = {
method : 'POST',
body : JSON.stringify({username, password}),
headers: new Headers({
'Content-Type': 'application/json',
}),
credentials: 'include',
};
return new Promise((resolve, reject) => {
request('/signup', params)
.then(result => {
2018-01-25 03:08:21 +01:00
console.log('makePublishChannelRequest result:', result);
return resolve(result);
})
.catch(error => {
console.log('create channel request failed:', error);
reject(new Error('Unfortunately, we encountered an error while creating your channel. Please let us know in Discord!'));
});
});
2018-01-06 01:47:55 +01:00
}
2018-01-05 22:59:25 +01:00
createChannel (event) {
event.preventDefault();
this.checkIsPasswordProvided()
.then(() => {
2018-02-07 00:05:31 +01:00
return this.checkIsChannelAvailable(this.state.channel, this.state.password);
})
.then(() => {
2018-02-07 00:05:31 +01:00
this.setState({status: 'We are publishing your new channel. Sit tight...'});
return this.makePublishChannelRequest(this.state.channel, this.state.password);
})
2018-01-06 01:47:55 +01:00
.then(result => {
2018-02-07 00:05:31 +01:00
this.setState({status: null});
this.props.onChannelLogin(result.channelName, result.shortChannelId, result.channelClaimId);
2018-01-06 01:47:55 +01:00
})
.catch((error) => {
2018-02-07 00:05:31 +01:00
this.setState({'error': error.message, status: null});
2018-01-06 01:47:55 +01:00
});
2018-01-05 22:59:25 +01:00
}
render () {
return (
2018-01-06 01:47:55 +01:00
<div>
{ !this.state.status ? (
2018-03-06 07:45:39 +01:00
<form id='publish-channel-form'>
<div className='row row--wide row--short'>
<div className='column column--3 column--sml-10'>
<label className='label' htmlFor='new-channel-name'>Name:</label>
</div><div className='column column--6 column--sml-10'>
<div className='input-text--primary flex-container--row flex-container--left-bottom span--relative'>
<span>@</span>
2018-03-06 07:45:39 +01:00
<input type='text' name='channel' id='new-channel-name' className='input-text' placeholder='exampleChannelName' value={this.state.channel} onChange={this.handleChannelInput} />
{ (this.state.channel && !this.state.error) && <span id='input-success-channel-name' className='info-message--success span--absolute'>{'\u2713'}</span> }
{ this.state.error && <span id='input-success-channel-name' className='info-message--failure span--absolute'>{'\u2716'}</span> }
</div>
2018-01-06 01:47:55 +01:00
</div>
</div>
2018-03-06 07:45:39 +01:00
<div className='row row--wide row--short'>
<div className='column column--3 column--sml-10'>
<label className='label' htmlFor='new-channel-password'>Password:</label>
</div><div className='column column--6 column--sml-10'>
<div className='input-text--primary'>
<input type='password' name='password' id='new-channel-password' className='input-text' placeholder='' value={this.state.password} onChange={this.handleInput} />
</div>
2018-01-06 01:47:55 +01:00
</div>
2018-01-05 22:59:25 +01:00
</div>
2018-03-06 07:45:39 +01:00
{this.state.error ? (
<p className='info-message--failure'>{this.state.error}</p>
) : (
<p className='info-message'>Choose a name and password for your channel</p>
)}
<div className='row row--wide'>
<button className='button--primary' onClick={this.createChannel}>Create Channel</button>
2018-01-06 01:47:55 +01:00
</div>
</form>
) : (
2018-01-20 01:40:47 +01:00
<div>
2018-03-06 07:45:39 +01:00
<p className='fine-print'>{this.state.status}</p>
<ProgressBar size={12} />
2018-01-20 01:40:47 +01:00
</div>
2018-01-06 01:47:55 +01:00
)}
</div>
2018-01-05 22:59:25 +01:00
);
}
}
2018-01-17 19:49:57 +01:00
export default ChannelCreateForm;