// @flow import * as React from 'react'; import { isNameValid, buildURI, regexInvalidURI } from 'lbryURI'; import { Form, FormField, FormRow, FormFieldPrice, Submit } from 'component/common/form'; import Button from 'component/button'; import ChannelSection from 'component/selectChannel'; import classnames from 'classnames'; import type { PublishParams, UpdatePublishFormData } from 'redux/reducers/publish'; import FileSelector from 'component/common/file-selector'; import { COPYRIGHT, OTHER } from 'constants/licenses'; import { CHANNEL_NEW, CHANNEL_ANONYMOUS, MINIMUM_PUBLISH_BID } from 'constants/claim'; import * as icons from 'constants/icons'; import BidHelpText from './internal/bid-help-text'; import LicenseType from './internal/license-type'; type Props = { publish: PublishParams => void, filePath: ?string, bid: ?number, editingURI: ?string, title: ?string, thumbnail: ?string, description: ?string, language: string, nsfw: boolean, contentIsFree: boolean, price: { amount: number, currency: string, }, channel: string, name: ?string, tosAccepted: boolean, updatePublishForm: UpdatePublishFormData => void, bid: number, nameError: ?string, isResolvingUri: boolean, winningBidForClaimUri: number, myClaimForUri: ?{ amount: number, value: { stream: { source: { source: string }, }, }, }, licenseType: string, otherLicenseDescription: ?string, licenseUrl: ?string, copyrightNotice: ?string, uri: ?string, bidError: ?string, publishing: boolean, balance: number, clearPublish: () => void, resolveUri: string => void, scrollToTop: () => void, prepareEdit: ({}) => void, }; class PublishForm extends React.PureComponent { constructor(props: Props) { super(props); (this: any).handleFileChange = this.handleFileChange.bind(this); (this: any).checkIsFormValid = this.checkIsFormValid.bind(this); (this: any).renderFormErrors = this.renderFormErrors.bind(this); (this: any).handlePublish = this.handlePublish.bind(this); (this: any).handleCancelPublish = this.handleCancelPublish.bind(this); (this: any).handleNameChange = this.handleNameChange.bind(this); (this: any).handleChannelChange = this.handleChannelChange.bind(this); (this: any).editExistingClaim = this.editExistingClaim.bind(this); (this: any).getNewUri = this.getNewUri.bind(this); } // Returns a new uri to be used in the form and begins to resolve that uri for bid help text getNewUri(name: string, channel: string) { const { resolveUri } = this.props; // If they are midway through a channel creation, treat it as anonymous until it completes const channelName = channel === CHANNEL_ANONYMOUS || channel === CHANNEL_NEW ? '' : channel; let uri; try { uri = buildURI({ contentName: name, channelName }); } catch (e) { // something wrong with channel or name } if (uri) { resolveUri(uri); return uri; } return ''; } handleFileChange(filePath: string, fileName: string) { const { updatePublishForm, channel, name } = this.props; const newFileParams: { filePath: string, name?: string, uri?: string, } = { filePath }; if (!name) { const parsedFileName = fileName.replace(regexInvalidURI, ''); const uri = this.getNewUri(parsedFileName, channel); newFileParams.name = parsedFileName; newFileParams.uri = uri; } updatePublishForm(newFileParams); } handleNameChange(name: ?string) { const { channel, updatePublishForm } = this.props; if (!name) { updatePublishForm({ name, nameError: undefined }); return; } if (!isNameValid(name, false)) { updatePublishForm({ name, nameError: __('LBRY names must contain only letters, numbers and dashes.'), }); return; } const uri = this.getNewUri(name, channel); updatePublishForm({ name, uri, nameError: undefined, }); } handleChannelChange(channelName: string) { const { name, updatePublishForm } = this.props; if (name) { const uri = this.getNewUri(name, channelName); updatePublishForm({ channel: channelName, uri }); } else { updatePublishForm({ channel: channelName }); } } handleBidChange(bid: number) { const { balance, updatePublishForm } = this.props; let bidError; if (balance <= bid) { bidError = __('Not enough credits'); } else if (bid <= MINIMUM_PUBLISH_BID) { bidError = __('Your bid must be higher'); } updatePublishForm({ bid, bidError }); } editExistingClaim() { const { myClaimForUri, prepareEdit, scrollToTop } = this.props; if (myClaimForUri) { prepareEdit(myClaimForUri); scrollToTop(); } } handleCancelPublish() { const { clearPublish, scrollToTop } = this.props; scrollToTop(); clearPublish(); } handlePublish() { const { publish, filePath, bid, title, thumbnail, description, language, nsfw, channel, licenseType, licenseUrl, otherLicenseDescription, copyrightNotice, name, contentIsFree, price, uri, myClaimForUri, } = this.props; let publishingLicense; switch (licenseType) { case COPYRIGHT: publishingLicense = copyrightNotice; break; case OTHER: publishingLicense = otherLicenseDescription; break; default: publishingLicense = licenseType; } const publishingLicenseUrl = licenseType === COPYRIGHT ? '' : licenseUrl; const publishParams = { filePath, bid, title, thumbnail, description, language, nsfw, channel, license: publishingLicense, licenseUrl: publishingLicenseUrl, otherLicenseDescription, copyrightNotice, name, contentIsFree, price, uri, }; // Editing a claim if (!filePath && myClaimForUri) { const { source } = myClaimForUri.value.stream; publishParams.sources = source; } publish(publishParams); } checkIsFormValid() { const { name, nameError, title, bid, bidError, tosAccepted } = this.props; return name && !nameError && title && bid && !bidError && tosAccepted; } renderFormErrors() { const { name, nameError, title, bid, bidError, tosAccepted } = this.props; if (nameError || bidError) { // There will be inline errors if either of these exist // These are just extra help at the bottom of the screen // There could be multiple bid errors, so just duplicate it at the bottom return (
{nameError &&
{__('The URL you created is not valid.')}
} {bidError &&
{bidError}
}
); } return (
{!title &&
{__('A title is required')}
} {!name &&
{__('A URL is required')}
} {!bid &&
{__('A bid amount is required')}
} {!tosAccepted &&
{__('You must agree to the terms of service')}
}
); } render() { const { filePath, editingURI, title, thumbnail, description, language, nsfw, contentIsFree, price, channel, name, tosAccepted, updatePublishForm, bid, nameError, isResolvingUri, winningBidForClaimUri, myClaimForUri, licenseType, otherLicenseDescription, licenseUrl, copyrightNotice, uri, bidError, publishing, clearPublish, } = this.props; const formDisabled = (!filePath && !editingURI) || publishing; const formValid = this.checkIsFormValid(); const isStillEditing = editingURI === uri; let submitLabel; if (isStillEditing) { submitLabel = !publishing ? __('Edit') : __('Editing...'); } else { submitLabel = !publishing ? __('Publish') : __('Publishing...'); } return (
{__('Content')}
{editingURI ? __('Editing a claim') : __('What are you publishing?')}
{(filePath || !!editingURI) && (
)} {!!editingURI && (

{__("If you don't choose a file, the file from your existing claim")} {` "${name}" `} {__('will be used.')}

)}
updatePublishForm({ title: e.target.value })} /> updatePublishForm({ thumbnail: e.target.value })} /> updatePublishForm({ description: text })} />
{__('Price')}
{__('How much will this content cost?')}
updatePublishForm({ contentIsFree: true })} /> updatePublishForm({ contentIsFree: false })} /> updatePublishForm({ price: newPrice })} disabled={formDisabled || contentIsFree} /> {price.currency !== 'LBC' && (

{__( 'All content fees are charged in LBC. For non-LBC payment methods, the number of credits charged will be adjusted based on the value of LBRY credits at the time of purchase.' )}

)}
{__('Anonymous or under a channel?')}

{__('This is a username or handle that your content can be found under.')}{' '} {__('Ex. @Marvel, @TheBeatles, @BooksByJoe')}

{__('Where can people find this content?')}

{__( 'The LBRY URL is the exact address where people find your content (ex. lbry://myvideo).' )}{' '}

updatePublishForm({ nsfw: event.target.checked })} /> updatePublishForm({ language: event.target.value })} > updatePublishForm({ licenseType: newLicenseType, licenseUrl: newLicenseUrl, }) } handleLicenseDescriptionChange={event => updatePublishForm({ otherLicenseDescription: event.target.value, }) } handleLicenseUrlChange={event => updatePublishForm({ licenseUrl: event.target.value }) } handleCopyrightNoticeChange={event => updatePublishForm({ copyrightNotice: event.target.value }) } />
{__('Terms of Service')}
{__('I agree to the')}{' '}
{!formDisabled && !formValid && this.renderFormErrors()}
); } } export default PublishForm;