2019-11-30 20:39:51 +01:00
|
|
|
// @flow
|
2019-12-06 20:42:44 +01:00
|
|
|
import React from 'react';
|
2019-11-30 20:39:51 +01:00
|
|
|
import { isNameValid } from 'lbry-redux';
|
2019-12-06 20:42:44 +01:00
|
|
|
import { Form, FormField } from 'component/common/form';
|
2019-11-30 20:39:51 +01:00
|
|
|
import Button from 'component/button';
|
|
|
|
import analytics from 'analytics';
|
2020-09-02 22:08:37 +02:00
|
|
|
import LbcSymbol from 'component/common/lbc-symbol';
|
2019-12-04 02:02:34 +01:00
|
|
|
import { MINIMUM_PUBLISH_BID, INVALID_NAME_ERROR } from 'constants/claim';
|
2019-11-30 20:39:51 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
balance: number,
|
|
|
|
createChannel: (string, number) => Promise<any>,
|
2019-12-04 02:02:34 +01:00
|
|
|
onSuccess?: ({}) => void,
|
2020-04-23 22:38:09 +02:00
|
|
|
creatingChannel: boolean,
|
|
|
|
createChannelError: ?string,
|
2019-11-30 20:39:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
newChannelName: string,
|
|
|
|
newChannelBid: number,
|
|
|
|
newChannelNameError: string,
|
|
|
|
newChannelBidError: string,
|
|
|
|
};
|
|
|
|
|
|
|
|
class ChannelCreate extends React.PureComponent<Props, State> {
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
newChannelName: '',
|
2020-07-03 16:27:01 +02:00
|
|
|
newChannelBid: 0.001,
|
2019-11-30 20:39:51 +01:00
|
|
|
newChannelNameError: '',
|
|
|
|
newChannelBidError: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
(this: any).handleNewChannelNameChange = this.handleNewChannelNameChange.bind(this);
|
|
|
|
(this: any).handleNewChannelBidChange = this.handleNewChannelBidChange.bind(this);
|
2019-12-06 20:42:44 +01:00
|
|
|
(this: any).handleCreateChannel = this.handleCreateChannel.bind(this);
|
2019-11-30 20:39:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
handleNewChannelNameChange(event: SyntheticInputEvent<*>) {
|
|
|
|
let newChannelName = event.target.value;
|
|
|
|
|
|
|
|
if (newChannelName.startsWith('@')) {
|
|
|
|
newChannelName = newChannelName.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let newChannelNameError;
|
|
|
|
if (newChannelName.length > 0 && !isNameValid(newChannelName, false)) {
|
|
|
|
newChannelNameError = INVALID_NAME_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
newChannelNameError,
|
|
|
|
newChannelName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleNewChannelBidChange(newChannelBid: number) {
|
|
|
|
const { balance } = this.props;
|
|
|
|
let newChannelBidError;
|
|
|
|
if (newChannelBid === 0) {
|
|
|
|
newChannelBidError = __('Your deposit cannot be 0');
|
|
|
|
} else if (newChannelBid === balance) {
|
|
|
|
newChannelBidError = __('Please decrease your deposit to account for transaction fees');
|
|
|
|
} else if (newChannelBid > balance) {
|
2021-01-12 16:25:51 +01:00
|
|
|
newChannelBidError = __('Deposit cannot be higher than your available balance');
|
2019-11-30 20:39:51 +01:00
|
|
|
} else if (newChannelBid < MINIMUM_PUBLISH_BID) {
|
|
|
|
newChannelBidError = __('Your deposit must be higher');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
newChannelBid,
|
|
|
|
newChannelBidError,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:42:44 +01:00
|
|
|
handleCreateChannel() {
|
2020-04-23 22:38:09 +02:00
|
|
|
const { createChannel, onSuccess } = this.props;
|
2019-11-30 20:39:51 +01:00
|
|
|
const { newChannelBid, newChannelName } = this.state;
|
|
|
|
|
|
|
|
const channelName = `@${newChannelName.trim()}`;
|
|
|
|
|
|
|
|
const success = channelClaim => {
|
|
|
|
analytics.apiLogPublish(channelClaim);
|
2019-12-04 02:02:34 +01:00
|
|
|
|
|
|
|
if (onSuccess !== undefined) {
|
|
|
|
onSuccess({ ...this.props, ...this.state });
|
|
|
|
}
|
2019-11-30 20:39:51 +01:00
|
|
|
};
|
|
|
|
|
2020-04-23 22:38:09 +02:00
|
|
|
createChannel(channelName, newChannelBid).then(success);
|
2019-11-30 20:39:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-04-23 22:38:09 +02:00
|
|
|
const { newChannelName, newChannelNameError, newChannelBid, newChannelBidError } = this.state;
|
|
|
|
const { creatingChannel, createChannelError } = this.props;
|
2019-11-30 20:39:51 +01:00
|
|
|
|
|
|
|
return (
|
2019-12-06 20:42:44 +01:00
|
|
|
<Form onSubmit={this.handleCreateChannel}>
|
2020-04-13 21:16:07 +02:00
|
|
|
{createChannelError && <div className="error__text">{createChannelError}</div>}
|
2019-11-30 20:39:51 +01:00
|
|
|
<div>
|
|
|
|
<FormField
|
|
|
|
label={__('Name')}
|
|
|
|
name="channel-input"
|
|
|
|
type="text"
|
|
|
|
placeholder={__('ChannelName')}
|
|
|
|
error={newChannelNameError}
|
|
|
|
value={newChannelName}
|
|
|
|
onChange={this.handleNewChannelNameChange}
|
|
|
|
/>
|
|
|
|
<FormField
|
|
|
|
className="form-field--price-amount"
|
|
|
|
name="channel-deposit"
|
2020-09-10 17:54:41 +02:00
|
|
|
label={<LbcSymbol postfix={__('Deposit')} size={14} />}
|
2019-11-30 20:39:51 +01:00
|
|
|
step="any"
|
|
|
|
min="0"
|
|
|
|
type="number"
|
2020-09-02 22:08:37 +02:00
|
|
|
helper={__(
|
|
|
|
'These LBRY Credits remain yours. It is a deposit to reserve the name and can be undone at any time.'
|
|
|
|
)}
|
2019-11-30 20:39:51 +01:00
|
|
|
error={newChannelBidError}
|
|
|
|
value={newChannelBid}
|
|
|
|
onChange={event => this.handleNewChannelBidChange(parseFloat(event.target.value))}
|
2020-01-28 03:47:48 +01:00
|
|
|
onWheel={e => e.stopPropagation()}
|
2019-11-30 20:39:51 +01:00
|
|
|
/>
|
2020-05-01 19:55:42 +02:00
|
|
|
<div className="section__actions">
|
2019-11-30 20:39:51 +01:00
|
|
|
<Button
|
2019-12-06 20:42:44 +01:00
|
|
|
type="submit"
|
2019-11-30 20:39:51 +01:00
|
|
|
button="primary"
|
|
|
|
label={!creatingChannel ? __('Create channel') : __('Creating channel...')}
|
|
|
|
disabled={
|
|
|
|
!newChannelName || !newChannelBid || creatingChannel || newChannelNameError || newChannelBidError
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-12-06 20:42:44 +01:00
|
|
|
</Form>
|
2019-11-30 20:39:51 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ChannelCreate;
|