2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
2018-04-18 06:03:01 +02:00
|
|
|
import { isNameValid } from 'lbry-redux';
|
2019-02-13 17:27:20 +01:00
|
|
|
import { FormField } from 'component/common/form';
|
2018-03-26 23:32:43 +02:00
|
|
|
import BusyIndicator from 'component/common/busy-indicator';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import { CHANNEL_NEW, CHANNEL_ANONYMOUS } from 'constants/claim';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
channel: string, // currently selected channel
|
|
|
|
channels: Array<{ name: string }>,
|
|
|
|
balance: number,
|
|
|
|
onChannelChange: string => void,
|
|
|
|
createChannel: (string, number) => Promise<any>,
|
|
|
|
fetchChannelListMine: () => void,
|
|
|
|
fetchingChannels: boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
newChannelName: string,
|
|
|
|
newChannelBid: number,
|
|
|
|
addingChannel: boolean,
|
|
|
|
creatingChannel: boolean,
|
|
|
|
newChannelNameError: string,
|
|
|
|
newChannelBidError: string,
|
|
|
|
createChannelError: ?string,
|
|
|
|
};
|
|
|
|
|
|
|
|
class ChannelSection extends React.PureComponent<Props, State> {
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
newChannelName: '',
|
|
|
|
newChannelBid: 0.1,
|
|
|
|
addingChannel: false,
|
|
|
|
creatingChannel: false,
|
|
|
|
newChannelNameError: '',
|
|
|
|
newChannelBidError: '',
|
|
|
|
createChannelError: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
(this: any).handleChannelChange = this.handleChannelChange.bind(this);
|
|
|
|
(this: any).handleNewChannelNameChange = this.handleNewChannelNameChange.bind(this);
|
|
|
|
(this: any).handleNewChannelBidChange = this.handleNewChannelBidChange.bind(this);
|
|
|
|
(this: any).handleCreateChannelClick = this.handleCreateChannelClick.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { channels, fetchChannelListMine, fetchingChannels } = this.props;
|
|
|
|
if (!channels.length && !fetchingChannels) {
|
|
|
|
fetchChannelListMine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChannelChange(event: SyntheticInputEvent<*>) {
|
|
|
|
const { onChannelChange } = this.props;
|
2018-06-18 08:34:59 +02:00
|
|
|
const { newChannelBid } = this.state;
|
2018-03-26 23:32:43 +02:00
|
|
|
const channel = event.target.value;
|
|
|
|
|
|
|
|
if (channel === CHANNEL_NEW) {
|
|
|
|
this.setState({ addingChannel: true });
|
|
|
|
onChannelChange(channel);
|
2018-06-18 08:34:59 +02:00
|
|
|
this.handleNewChannelBidChange(newChannelBid);
|
2018-03-26 23:32:43 +02:00
|
|
|
} else {
|
|
|
|
this.setState({ addingChannel: false });
|
|
|
|
onChannelChange(channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleNewChannelNameChange(event: SyntheticInputEvent<*>) {
|
|
|
|
let newChannelName = event.target.value;
|
|
|
|
|
|
|
|
if (newChannelName.startsWith('@')) {
|
|
|
|
newChannelName = newChannelName.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let newChannelNameError;
|
|
|
|
if (newChannelName.length > 1 && !isNameValid(newChannelName.substr(1), false)) {
|
|
|
|
newChannelNameError = __('LBRY channel names must contain only letters, numbers and dashes.');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
newChannelNameError,
|
|
|
|
newChannelName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 08:34:59 +02:00
|
|
|
handleNewChannelBidChange(newChannelBid: number) {
|
2018-03-26 23:32:43 +02:00
|
|
|
const { balance } = this.props;
|
|
|
|
let newChannelBidError;
|
2018-06-18 08:34:59 +02:00
|
|
|
if (newChannelBid === 0) {
|
|
|
|
newChannelBidError = __('Your deposit cannot be 0');
|
|
|
|
} else if (newChannelBid === balance) {
|
|
|
|
newChannelBidError = __('Please decrease your deposit to account for transaction fees');
|
2018-03-26 23:32:43 +02:00
|
|
|
} else if (newChannelBid > balance) {
|
2018-06-18 08:34:59 +02:00
|
|
|
newChannelBidError = __('Deposit cannot be higher than your balance');
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
newChannelBid,
|
|
|
|
newChannelBidError,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCreateChannelClick() {
|
|
|
|
const { balance, createChannel, onChannelChange } = this.props;
|
|
|
|
const { newChannelBid, newChannelName } = this.state;
|
|
|
|
|
|
|
|
const channelName = `@${newChannelName}`;
|
|
|
|
|
|
|
|
if (newChannelBid > balance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
creatingChannel: true,
|
|
|
|
createChannelError: undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
const success = () => {
|
|
|
|
this.setState({
|
|
|
|
creatingChannel: false,
|
|
|
|
addingChannel: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
onChannelChange(channelName);
|
|
|
|
};
|
|
|
|
|
|
|
|
const failure = () => {
|
|
|
|
this.setState({
|
|
|
|
creatingChannel: false,
|
|
|
|
createChannelError: __('Unable to create channel due to an internal error.'),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
createChannel(channelName, newChannelBid).then(success, failure);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const channel = this.state.addingChannel ? 'new' : this.props.channel;
|
|
|
|
const { fetchingChannels, channels = [] } = this.props;
|
|
|
|
const {
|
|
|
|
newChannelName,
|
|
|
|
newChannelNameError,
|
|
|
|
newChannelBid,
|
|
|
|
newChannelBidError,
|
|
|
|
creatingChannel,
|
|
|
|
createChannelError,
|
|
|
|
addingChannel,
|
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="card__content">
|
2019-02-13 17:27:20 +01:00
|
|
|
{createChannelError && <div className="error-text">{createChannelError}</div>}
|
2018-03-26 23:32:43 +02:00
|
|
|
{fetchingChannels ? (
|
|
|
|
<BusyIndicator message="Updating channels" />
|
|
|
|
) : (
|
2019-02-13 17:27:20 +01:00
|
|
|
<fieldset-section>
|
2019-05-07 23:38:29 +02:00
|
|
|
<FormField name="channel" type="select" onChange={this.handleChannelChange} value={channel}>
|
2019-02-13 17:27:20 +01:00
|
|
|
<option value={CHANNEL_ANONYMOUS}>{__('Anonymous')}</option>
|
|
|
|
{channels.map(({ name }) => (
|
|
|
|
<option key={name} value={name}>
|
|
|
|
{name}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
<option value={CHANNEL_NEW}>{__('New channel...')}</option>
|
|
|
|
</FormField>
|
|
|
|
</fieldset-section>
|
2018-03-26 23:32:43 +02:00
|
|
|
)}
|
|
|
|
{addingChannel && (
|
|
|
|
<div className="card__content">
|
2019-02-13 17:27:20 +01:00
|
|
|
<FormField
|
|
|
|
label={__('Name')}
|
2019-02-20 06:20:29 +01:00
|
|
|
name="channel-input"
|
2019-02-13 17:27:20 +01:00
|
|
|
type="text"
|
|
|
|
placeholder={__('myChannelName')}
|
|
|
|
error={newChannelNameError}
|
|
|
|
value={newChannelName}
|
|
|
|
onChange={this.handleNewChannelNameChange}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
className="form-field--price-amount"
|
2019-02-20 06:20:29 +01:00
|
|
|
name="channel-deposit"
|
|
|
|
label={__('Deposit (LBC)')}
|
2019-02-13 17:27:20 +01:00
|
|
|
step="any"
|
|
|
|
min="0"
|
|
|
|
type="number"
|
2019-05-07 23:38:29 +02:00
|
|
|
helper={__('This LBC remains yours. It is a deposit to reserve the name and can be undone at any time.')}
|
2019-02-13 17:27:20 +01:00
|
|
|
error={newChannelBidError}
|
|
|
|
value={newChannelBid}
|
|
|
|
onChange={event => this.handleNewChannelBidChange(parseFloat(event.target.value))}
|
|
|
|
/>
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
<div className="card__actions">
|
|
|
|
<Button
|
|
|
|
button="primary"
|
|
|
|
label={!creatingChannel ? __('Create channel') : __('Creating channel...')}
|
|
|
|
onClick={this.handleCreateChannelClick}
|
|
|
|
disabled={
|
2019-05-07 23:38:29 +02:00
|
|
|
!newChannelName || !newChannelBid || creatingChannel || newChannelNameError || newChannelBidError
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ChannelSection;
|