import React from 'react'; function UrlMiddle ({publishToChannel, loggedInChannelName, loggedInChannelShortId}) { if (publishToChannel) { if (loggedInChannelName) { return {loggedInChannelName}:{loggedInChannelShortId} /; } return @channelSelect a channel below /; } return ( xyzThis will be a random id / ); } class UrlInput extends React.Component { constructor (props) { super(props); this.state = { urlError : null, urlBeginning: 'spee.ch', urlMiddle : null, }; this.handleInput = this.handleInput.bind(this); this.validateClaimName = this.validateClaimName.bind(this); this.cleanseClaimName = this.cleanseClaimName.bind(this); this.checkClaimIsValidAndAvailable = this.checkClaimIsValidAndAvailable.bind(this); } handleInput (event) { event.preventDefault(); let value = event.target.value; const name = event.target.name; value = this.cleanseClaimName(value); this.props.updateUploaderState(name, value); this.checkClaimIsValidAndAvailable(value); } validateClaimName (claim) { // ensure a name was entered if (!claim || claim.length < 1) { throw new Error('You must enter a name for your url'); } // validate the characters in the 'name' field const invalidCharacters = /[^A-Za-z0-9,-]/g.exec(claim); if (invalidCharacters) { throw new Error('"' + invalidCharacters + '" characters are not allowed'); } return claim; } cleanseClaimName (name) { name = name.replace(/\s+/g, '-'); // replace spaces with dashes name = name.replace(/[^A-Za-z0-9-]/g, ''); // remove all characters that are not A-Z, a-z, 0-9, or '-' return name; } checkClaimIsValidAndAvailable (claim) { // validationFunctions.checkClaimName(event.target.value) try { claim = this.validateClaimName(claim); } catch (error) { this.setState({urlError: error.message}); return; } const that = this; this.props.makeGetRequest(`/api/claim-is-available/${claim}`) .then(() => { that.setState({urlError: null}); }) .catch((error) => { that.setState({urlError: error.message}); }); } render () { return (
{this.state.urlError}